kubo/repo/common/common.go
Andrew Gillis 9425421114
Some checks failed
CodeQL / codeql (push) Has been cancelled
Docker Build / docker-build (push) Has been cancelled
Gateway Conformance / gateway-conformance (push) Has been cancelled
Gateway Conformance / gateway-conformance-libp2p-experiment (push) Has been cancelled
Go Build / go-build (push) Has been cancelled
Go Check / go-check (push) Has been cancelled
Go Lint / go-lint (push) Has been cancelled
Go Test / go-test (push) Has been cancelled
Interop / interop-prep (push) Has been cancelled
Sharness / sharness-test (push) Has been cancelled
Interop / helia-interop (push) Has been cancelled
Interop / ipfs-webui (push) Has been cancelled
collection of typo fixes (#10647)
2024-12-29 21:27:41 -08:00

93 lines
2.1 KiB
Go

package common
import (
"fmt"
"strings"
)
func MapGetKV(v map[string]interface{}, key string) (interface{}, error) {
var ok bool
var mcursor map[string]interface{}
var cursor interface{} = v
parts := strings.Split(key, ".")
for i, part := range parts {
sofar := strings.Join(parts[:i], ".")
mcursor, ok = cursor.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("%s key is not a map", sofar)
}
cursor, ok = mcursor[part]
if !ok {
// Construct the current path traversed to print a nice error message
var path string
if len(sofar) > 0 {
path += sofar + "."
}
path += part
return nil, fmt.Errorf("%s not found", path)
}
}
return cursor, nil
}
func MapSetKV(v map[string]interface{}, key string, value interface{}) error {
var ok bool
var mcursor map[string]interface{}
var cursor interface{} = v
parts := strings.Split(key, ".")
for i, part := range parts {
mcursor, ok = cursor.(map[string]interface{})
if !ok {
sofar := strings.Join(parts[:i], ".")
return fmt.Errorf("%s key is not a map", sofar)
}
// last part? set here
if i == (len(parts) - 1) {
mcursor[part] = value
break
}
cursor, ok = mcursor[part]
if !ok || cursor == nil { // create map if this is empty or is null
mcursor[part] = map[string]interface{}{}
cursor = mcursor[part]
}
}
return nil
}
// MapMergeDeep merges the right map into the left map, recursively traversing
// child maps until a non-map value is found.
func MapMergeDeep(left, right map[string]interface{}) map[string]interface{} {
// We want to alter a copy of the map, not the original
result := make(map[string]interface{})
for k, v := range left {
result[k] = v
}
for key, rightVal := range right {
// If right value is a map
if rightMap, ok := rightVal.(map[string]interface{}); ok {
// If key is in left
if leftVal, found := result[key]; found {
// If left value is also a map
if leftMap, ok := leftVal.(map[string]interface{}); ok {
// Merge nested map
result[key] = MapMergeDeep(leftMap, rightMap)
continue
}
}
}
// Otherwise set new value to result
result[key] = rightVal
}
return result
}