Skip to content

Commit

Permalink
feat: add map[string]interface{} cases for SetPath and GetPath
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Feb 13, 2024
1 parent 248e3d5 commit 60de4b2
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion data.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func (d Data) GetPath(path ...interface{}) (tree interface{}, err error) {
return nil, fmt.Errorf("key not found: %v", el)
}

case map[string]interface{}:
key, ok := el.(string)
if !ok {
return nil, fmt.Errorf("unexpected string key in map[string]interface '%T' at index %d", el, i)
}
tree, ok = node[key]
if !ok {
return nil, fmt.Errorf("key not found: %v", el)
}

case map[interface{}]interface{}:
var ok bool
tree, ok = node[el]
Expand Down Expand Up @@ -154,6 +164,13 @@ func (d *Data) SetPath(value interface{}, path ...interface{}) (err error) {
}
node[key] = value

case map[string]interface{}:
key, ok := element.(string)
if !ok {
return fmt.Errorf("unexpected string key in map[string]interface '%T' at index %d", element, i)
}
node[key] = value

case []interface{}:
index, ok := element.(int)
if !ok {
Expand Down Expand Up @@ -228,7 +245,6 @@ func (d Data) FindValues(valueFunc FindValueFunc, target *[]interface{}) (err er

var walk func(reflect.Value, []interface{}) error
walk = func(v reflect.Value, path []interface{}) error {

// fix indirects through pointers and interfaces
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
Expand Down

0 comments on commit 60de4b2

Please sign in to comment.