Add GetInheritedValue()
This commit is contained in:
parent
4454bd800e
commit
b2301041a8
74
dict/dict.go
74
dict/dict.go
@ -29,7 +29,7 @@ func (d Dict) Print() {
|
||||
fmt.Println(pp)
|
||||
}
|
||||
|
||||
func (d Dict) GetValue(path string) Dict {
|
||||
func (d Dict) GetInheritedValueAll(path string) Dict {
|
||||
var value Dict
|
||||
current := make(Dict)
|
||||
g := make(Dict)
|
||||
@ -42,7 +42,6 @@ func (d Dict) GetValue(path string) Dict {
|
||||
current.SoftMergeWith(g)
|
||||
}
|
||||
if !current.HasKey(key) {
|
||||
value = nil
|
||||
break
|
||||
}
|
||||
v := current[key]
|
||||
@ -57,6 +56,77 @@ func (d Dict) GetValue(path string) Dict {
|
||||
}
|
||||
if IsDict(v) {
|
||||
current = v.(Dict)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (d Dict) GetInheritedValue(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) {
|
||||
break
|
||||
}
|
||||
v := current[key]
|
||||
if i == lastIndex {
|
||||
if vv, ok := v.(Dict); ok {
|
||||
if g.HasKey(key) {
|
||||
if gv, ok := g[key].(Dict); ok {
|
||||
vv.SoftMergeWith(gv)
|
||||
value = vv
|
||||
}
|
||||
} else {
|
||||
vv.SoftMergeWith(g)
|
||||
value = vv
|
||||
}
|
||||
} else {
|
||||
value = Dict{"value": v}
|
||||
}
|
||||
break
|
||||
}
|
||||
if IsDict(v) {
|
||||
current = v.(Dict)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (d Dict) GetDirectValue(path string) Dict {
|
||||
var value Dict
|
||||
current := make(Dict)
|
||||
current.CopyFrom(d)
|
||||
keys := strings.Split(path, ".")
|
||||
lastIndex := len(keys) - 1
|
||||
for i, key := range keys {
|
||||
if !current.HasKey(key) {
|
||||
break
|
||||
}
|
||||
v := current[key]
|
||||
if i == lastIndex {
|
||||
if vv, ok := v.(Dict); ok {
|
||||
value = vv
|
||||
} else {
|
||||
value = Dict{"value": v}
|
||||
}
|
||||
break
|
||||
}
|
||||
if IsDict(v) {
|
||||
current = v.(Dict)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return value
|
||||
|
@ -54,13 +54,13 @@ func TestIsDict(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeValue(t *testing.T) {
|
||||
func TestGetInheritedValue(t *testing.T) {
|
||||
testCases := []dictStrToDictTestCase{}
|
||||
loadYamlFile("test_data/getvalue_cases.yaml", &testCases)
|
||||
for _, testCase := range testCases {
|
||||
copy := make(Dict)
|
||||
copy.CopyFrom(testCase.Arg1)
|
||||
result := testCase.Arg1.GetValue(testCase.Arg2)
|
||||
result := testCase.Arg1.GetInheritedValue(testCase.Arg2)
|
||||
if !reflect.DeepEqual(testCase.Result, result) {
|
||||
t.Errorf("Expected dict to be %v, got %v", testCase.Result, result)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user