Replace the CopyMap function by the MustFillMap and adds a InterfaceToIntMap function

This commit is contained in:
2022-10-12 22:56:38 +02:00
parent 58dbdd8698
commit aae3398701
3 changed files with 52 additions and 10 deletions

1
go.mod
View File

@ -14,6 +14,7 @@ require (
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/akualab/gjoa v0.0.0-20150423185904-0953495dbcc7 // indirect
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect

2
go.sum
View File

@ -10,6 +10,8 @@ github.com/PaesslerAG/jsonpath v0.1.0 h1:gADYeifvlqK3R3i2cR5B4DGgxLXIPb3TRTH1mGi
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
github.com/akualab/gjoa v0.0.0-20150423185904-0953495dbcc7 h1:v8r5rCWpphm5f+yaKDQvVKM46jj0SB7iDUTVS1MHSAg=
github.com/akualab/gjoa v0.0.0-20150423185904-0953495dbcc7/go.mod h1:zhxCkmvf40lxgYc6aJuF6AOwfzuvFIANF9OQaMz5xNs=
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df h1:GSoSVRLoBaFpOOds6QyY1L8AX7uoY+Ln3BHc22W40X0=
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df/go.mod h1:hiVxq5OP2bUGBRNS3Z/bt/reCLFNbdcST6gISi1fiOM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@ -3,12 +3,13 @@ package goutils
import (
"bufio"
"bytes"
"encoding/gob"
"encoding/json"
"io"
"os"
"reflect"
"sync"
"github.com/barkimedes/go-deepcopy"
)
// NotAnInteger defines a new type of Error : "NotAnInteger"
@ -60,6 +61,44 @@ func InterfaceToInt(i interface{}) (val int, err error) {
return
}
// NotABoolean defines a new type of Error : "NotAMapInt"
type NotAMapInt struct {
message string
}
// Error() retreives the error message associated to the "NotAnInteger"
// error. Tha addition of that Error message make the "NotAnInteger"
// complying with the error interface
func (m *NotAMapInt) Error() string {
return m.message
}
func InterfaceToIntMap(i interface{}) (val map[string]int, err error) {
err = nil
switch i := i.(type) {
case map[string]int:
val = i
case map[string]interface{}:
val = make(map[string]int, len(i))
for k, v := range i {
val[k], err = InterfaceToInt(v)
if err != nil {
return
}
}
case map[string]float64:
val = make(map[string]int, len(i))
for k, v := range i {
val[k] = int(v)
}
default:
err = &NotAMapInt{"value attribute cannot be casted to a map[string]int"}
}
return
}
// NotABoolean defines a new type of Error : "NotABoolean"
type NotABoolean struct {
message string
@ -122,14 +161,14 @@ func CastableToInt(i interface{}) bool {
}
}
// "CopyMap copies the contents of a map[string]interface{} to another map[string]interface{}."
//
// The function uses the gob package to encode the source map into a buffer and then decode the buffer
// into the destination map
func CopyMap(dest, src map[string]interface{}) {
buf := new(bytes.Buffer)
gob.NewEncoder(buf).Encode(src)
gob.NewDecoder(buf).Decode(&dest)
func MustFillMap(dest, src map[string]interface{}) {
for k, v := range src {
if IsAMap(v) || IsASlice(v) || IsAnArray(v) {
v = deepcopy.MustAnything(v)
}
dest[k] = v
}
}
// Read a whole file into the memory and store it as array of lines
@ -228,4 +267,4 @@ func Length(value interface{}) int {
}
return l
}
}