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

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
}
}