Skip to content

Commit

Permalink
Merge pull request #899 from vbehar/fix-set
Browse files Browse the repository at this point in the history
fix `set` feature when adding a new root hierarchy
  • Loading branch information
hiddeco authored Aug 15, 2023
2 parents 2d92659 + 4250327 commit a0aec47
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sops.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ func set(branch interface{}, path []interface{}, value interface{}) interface{}
}
}
// Not found, need to add the next path entry to the branch
if len(path) == 1 {
return append(branch, TreeItem{Key: path[0], Value: value})
value := valueFromPathAndLeaf(path, value)
if newBranch, ok := value.(TreeBranch); ok && len(newBranch) > 0 {
return append(branch, newBranch[0])
}
return valueFromPathAndLeaf(path, value)
return branch
case []interface{}:
position := path[0].(int)
if len(path) == 1 {
Expand Down
30 changes: 30 additions & 0 deletions sops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,36 @@ func TestSetNewKey(t *testing.T) {
assert.Equal(t, "hello", set[0].Value.(TreeBranch)[0].Value.(TreeBranch)[1].Value)
}

func TestSetNewBranch(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "key",
Value: "value",
},
}
set := branch.Set([]interface{}{"foo", "bar", "baz"}, "hello")
assert.Equal(t, TreeBranch{
TreeItem{
Key: "key",
Value: "value",
},
TreeItem{
Key: "foo",
Value: TreeBranch{
TreeItem{
Key: "bar",
Value: TreeBranch{
TreeItem{
Key: "baz",
Value: "hello",
},
},
},
},
},
}, set)
}

func TestSetArrayDeepNew(t *testing.T) {
branch := TreeBranch{
TreeItem{
Expand Down

0 comments on commit a0aec47

Please # to comment.