-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package operations | ||
|
||
import ( | ||
"github.com/can3p/sackmesser/pkg/traverse/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func Merge(root types.Node, path []string, args ...any) error { | ||
if len(args) != 1 { | ||
return errors.Errorf("set operation expects one argument") | ||
} | ||
|
||
value, ok := args[0].(map[string]any) | ||
if !ok { | ||
return errors.Errorf("Merge expects a json as an argument") | ||
} | ||
|
||
if len(path) == 1 { | ||
fieldName := path[0] | ||
fieldVal, err := root.GetField(fieldName) | ||
|
||
if err == types.ErrFieldMissing { | ||
return root.SetField(fieldName, value) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fieldVal = mergeObject(fieldVal, value) | ||
return root.SetField(fieldName, fieldVal) | ||
} | ||
|
||
node, err := root.Visit(path[0]) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return Merge(node, path[1:], value) | ||
} | ||
|
||
func mergeObject(existingValue any, value map[string]any) any { | ||
typed, ok := existingValue.(map[string]any) | ||
|
||
if !ok { | ||
return value | ||
} | ||
|
||
for fieldName, value := range value { | ||
subfieldValue, exists := typed[fieldName] | ||
|
||
if !exists { | ||
typed[fieldName] = value | ||
continue | ||
} | ||
|
||
typedSubfieldValue, subfieldIsMap := subfieldValue.(map[string]any) | ||
typedNewValue, newValueIsMap := value.(map[string]any) | ||
|
||
if !subfieldIsMap || !newValueIsMap { | ||
typed[fieldName] = value | ||
continue | ||
} | ||
|
||
typed[fieldName] = mergeObject(typedSubfieldValue, typedNewValue) | ||
} | ||
|
||
return typed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package operations | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
"github.com/can3p/sackmesser/pkg/traverse/simplejson" | ||
) | ||
|
||
func TestMergeOperation(t *testing.T) { | ||
jstr := `{ "abc": { "def": { "cfa": [1, 2, 3] } } }` | ||
|
||
examples := []struct { | ||
description string | ||
path []string | ||
arg any | ||
initial string | ||
expected string | ||
isErr bool | ||
}{ | ||
{ | ||
description: "add new field to the object", | ||
path: []string{"abc", "def"}, | ||
arg: map[string]any{"added": true}, | ||
initial: jstr, | ||
expected: `{ "abc": { "def": { "cfa": [ 1, 2, 3 ], "added": true } } }`, | ||
}, | ||
{ | ||
description: "scalar value should produce an error", | ||
path: []string{"abc", "def"}, | ||
arg: true, | ||
initial: jstr, | ||
isErr: true, | ||
}, | ||
{ | ||
description: "non existant field is just set", | ||
path: []string{"abc", "new field"}, | ||
arg: map[string]any{"added": true}, | ||
initial: jstr, | ||
expected: `{ "abc": { "def": { "cfa": [ 1, 2, 3 ] }, "new field": { "added": true } } }`, | ||
}, | ||
{ | ||
description: "non object target field means set", | ||
path: []string{"abc", "def"}, | ||
arg: map[string]any{"added": true}, | ||
initial: `{ "abc": { "def": true } }`, | ||
expected: `{ "abc": { "def": { "added": true } } }`, | ||
}, | ||
} | ||
|
||
for idx, ex := range examples { | ||
node := simplejson.MustParse([]byte(ex.initial)) | ||
|
||
err := Merge(node, ex.path, ex.arg) | ||
|
||
if ex.isErr { | ||
assert.Error(t, err, "[Ex %d - %s]", idx+1, ex.description) | ||
continue | ||
} else { | ||
assert.NoError(t, err, "[Ex %d - %s]", idx+1, ex.description) | ||
} | ||
|
||
expected := simplejson.MustParse([]byte(ex.expected)) | ||
|
||
assert.Equal(t, expected.Value(), node.Value(), "[Ex %d - %s]", idx+1, ex.description) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters