GoDict/dict/dict.go

64 lines
981 B
Go

package dict
import (
"fmt"
"strings"
)
type Dict map[string]interface{}
func New() Dict {
return make(Dict)
}
func IsDict(a interface{}) bool {
_, ok := a.(Dict)
return ok
}
func (d Dict) HasKey(key string) bool {
_, ok := d[key]
return ok
}
func (d Dict) Print() {
pp, err := d.DumpJsonStrIndent("", " ")
if err != nil {
panic(err)
}
fmt.Println(pp)
}
func (d Dict) GetValue(path string) Dict {
var value Dict
current := make(Dict)
g := make(Dict)
current.CopyFrom(d)
keys := strings.Split(path, ".")
lastIndex := len(keys) - 1
for i, key := range keys {
if current.HasKey("globals") {
g.MergeWith(current["globals"].(Dict))
current.SoftMergeWith(g)
}
if !current.HasKey(key) {
value = nil
break
}
v := current[key]
if i == lastIndex {
if vv, ok := v.(Dict); ok {
vv.SoftMergeWith(g)
value = vv
} else {
value = Dict{"value": v}
}
break
}
if IsDict(v) {
current = v.(Dict)
}
}
return value
}