First version of obimatrix

Former-commit-id: 6e09eb0dd75bc688a6c83ef40dd88658fb1b296e
This commit is contained in:
2023-11-12 20:40:56 +01:00
parent 677775bf04
commit 8620ea1637
8 changed files with 350 additions and 12 deletions

View File

@ -1,6 +1,10 @@
package obiutils
import "fmt"
import (
"fmt"
"log"
"reflect"
)
// InterfaceToString converts an interface value to a string.
//
@ -64,3 +68,25 @@ func InterfaceToBool(i interface{}) (val bool, err error) {
}
return
}
// MapToMapInterface converts a map to a map of type map[string]interface{}.
//
// It takes an interface{} parameter `m` which represents the map to be converted.
//
// It returns a map[string]interface{} which is the converted map. If the input map is not of type map[string]interface{},
// it panics and logs an error message.
func MapToMapInterface(m interface{}) map[string]interface{} {
if IsAMap(m) {
reflectMap := reflect.ValueOf(m)
keys := reflectMap.MapKeys()
val := make(map[string]interface{}, len(keys))
for k := range keys {
val[keys[k].String()] = reflectMap.MapIndex(keys[k]).Interface()
}
return val
}
log.Panic("Invalid map type")
return make(map[string]interface{})
}