Skip to content
Snippets Groups Projects
Commit d2f57a88 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

Merge branch 'feature/PRXS-1670-FixKeepSrc' into 'master'

Исправлена ошибка в функции keepSource пакета schema - всегда возвращался флаг изменено

See merge request perxis/perxis-go!99
parents b35ccff4 007226a4
No related branches found
No related tags found
No related merge requests found
package walk package walk
import "reflect"
func GenericMerge(c *WalkContext) (err error) { func GenericMerge(c *WalkContext) (err error) {
return return
} }
func KeepSrc(c *WalkContext) (err error) { func KeepSrc(c *WalkContext) (err error) {
if reflect.DeepEqual(c.Src, c.Dst) {
return
}
c.Dst = c.Src c.Dst = c.Src
c.Changed = true c.Changed = true
return return
......
...@@ -46,6 +46,54 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -46,6 +46,54 @@ func TestWalker_DataWalk(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{"generic", {"generic",
&WalkConfig{
Fields: map[string]FieldConfig{},
},
map[string]interface{}{
"a": "src_a",
"b": "src_b",
"obj1": map[string]interface{}{
"a": "src_obj1_a",
"b": "src_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_1": "src_inline_1",
"inline_str_2": "src_inline_2",
"slice": []interface{}{"src_s1", "src_s2"},
},
map[string]interface{}{
"a": "dst_a",
"field_not_extists": "remove",
"obj1": map[string]interface{}{
"a": "dst_obj1_a",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
},
"inline_str_1": "dst_inline_1",
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"dst_s1", "dst_s2", "dst_s3"},
},
map[string]interface{}{
"a": "dst_a",
"obj1": map[string]interface{}{
"a": "dst_obj1_a",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
},
"inline_str_1": "dst_inline_1",
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"dst_s1", "dst_s2", "dst_s3"},
},
false, false,
},
{"keep src changed",
&WalkConfig{ &WalkConfig{
Fields: map[string]FieldConfig{ Fields: map[string]FieldConfig{
"obj1.a": {Fn: KeepSrc}, "obj1.a": {Fn: KeepSrc},
...@@ -96,6 +144,68 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -96,6 +144,68 @@ func TestWalker_DataWalk(t *testing.T) {
"inline_str_2": "src_inline_2", "inline_str_2": "src_inline_2",
"slice": []interface{}{"dst_s1", "src_s2", "dst_s3"}, "slice": []interface{}{"dst_s1", "src_s2", "dst_s3"},
}, },
true, false,
},
{"keep src not changed",
&WalkConfig{
Fields: map[string]FieldConfig{
"obj1.a": {Fn: KeepSrc},
"slice.1": {Fn: KeepSrc},
"inline_str_1": {Fn: KeepSrc},
"inline_str_2": {Fn: KeepSrc},
},
},
map[string]interface{}{
"a": "src_a",
"b": "src_b",
"obj1": map[string]interface{}{
"a": "src_obj1_a",
"b": "src_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_1": "src_inline_1",
"inline_str_2": "src_inline_2",
"slice": []interface{}{"src_s1", "src_s2"},
},
map[string]interface{}{
"a": "src_a",
"b": "src_b",
"obj1": map[string]interface{}{
"a": "src_obj1_a",
"b": "src_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_1": "src_inline_1",
"inline_str_2": "src_inline_2",
"slice": []interface{}{"src_s1", "src_s2"},
},
map[string]interface{}{
"a": "src_a",
"b": "src_b",
"obj1": map[string]interface{}{
"a": "src_obj1_a",
"b": "src_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_1": "src_inline_1",
"inline_str_2": "src_inline_2",
"slice": []interface{}{"src_s1", "src_s2"},
},
false, false, false, false,
}, },
} }
...@@ -103,11 +213,12 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -103,11 +213,12 @@ func TestWalker_DataWalk(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m := NewWalker(s, tt.config) m := NewWalker(s, tt.config)
dst := tt.dst dst := tt.dst
res, _, err := m.DataWalk(context.Background(), dst, tt.src) res, chg, err := m.DataWalk(context.Background(), dst, tt.src)
assert.Equal(t, tt.res, res)
if tt.wantErr { if tt.wantErr {
require.Error(t, err) require.Error(t, err)
} else { } else {
assert.Equal(t, tt.res, res)
assert.Equal(t, tt.wantChanged, chg)
require.NoError(t, err) require.NoError(t, err)
} }
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment