Skip to content
Snippets Groups Projects
Commit 8a7f62fc authored by Anton Sattarov's avatar Anton Sattarov :cucumber: Committed by Pavel Antonov
Browse files

feat(core): Изменено поведение обхода схемы schema.Walk, считаем что объект...

feat(core): Изменено поведение обхода схемы schema.Walk, считаем что объект изменился при установке значения Nil

Close #PRXS-2648
parent 217f3c06
No related branches found
No related tags found
No related merge requests found
Subproject commit 2e4728f6f3d5d63dcb9d5548b17af9efce605280 Subproject commit d05d75325479800baef608bcde55c46b42b019e0
...@@ -762,6 +762,7 @@ func TestLocalizer_ExtractTranslations(t *testing.T) { ...@@ -762,6 +762,7 @@ func TestLocalizer_ExtractTranslations(t *testing.T) {
s := schema.New( s := schema.New(
"a", field.String(), "a", field.String(),
"b", field.String(),
) )
tests := []struct { tests := []struct {
...@@ -877,7 +878,7 @@ func TestLocalizer_ExtractTranslations(t *testing.T) { ...@@ -877,7 +878,7 @@ func TestLocalizer_ExtractTranslations(t *testing.T) {
{ {
name: "Extract fallback with same", name: "Extract fallback with same",
data: map[string]interface{}{"a": "aaa"}, data: map[string]interface{}{"a": "aaa"},
translations: map[string]map[string]interface{}{"ru": {"a": "aaa"}, "en": {"a": "aaa"}}, translations: map[string]map[string]interface{}{"ru": {"a": "aaa"}, "en": {"a": "aaa", "b": "bbb"}},
cfg: Config{ cfg: Config{
LocaleID: "ru", LocaleID: "ru",
Schema: s, Schema: s,
......
...@@ -120,6 +120,7 @@ func (m *Walker) datawalk(w *WalkContext) (err error) { ...@@ -120,6 +120,7 @@ func (m *Walker) datawalk(w *WalkContext) (err error) {
fields := p.GetFields(true) fields := p.GetFields(true)
var objChanged bool
for k := range keys { for k := range keys {
f, ok := fields[k] f, ok := fields[k]
if !ok { if !ok {
...@@ -144,13 +145,16 @@ func (m *Walker) datawalk(w *WalkContext) (err error) { ...@@ -144,13 +145,16 @@ func (m *Walker) datawalk(w *WalkContext) (err error) {
} }
if wc.Changed { if wc.Changed {
objChanged = true
w.Changed = true w.Changed = true
} }
} }
if len(res) > 0 {
if objChanged || len(res) != len(d) { // in generic merge unknown fields are removed but change is not set
w.Dst = res w.Dst = res
} }
case *field.ArrayParameters: case *field.ArrayParameters:
d, _ := w.Dst.([]interface{}) d, _ := w.Dst.([]interface{})
s, _ := w.Src.([]interface{}) s, _ := w.Src.([]interface{})
...@@ -175,7 +179,6 @@ func (m *Walker) datawalk(w *WalkContext) (err error) { ...@@ -175,7 +179,6 @@ func (m *Walker) datawalk(w *WalkContext) (err error) {
w.Changed = true w.Changed = true
} }
} }
} }
return return
......
...@@ -56,10 +56,10 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -56,10 +56,10 @@ func TestWalker_DataWalk(t *testing.T) {
"a": "src_obj1_a", "a": "src_obj1_a",
"b": "src_obj1_b", "b": "src_obj1_b",
"obj2": map[string]interface{}{ "obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a", "a": "src_obj1_obj2_a",
}, },
"obj3": map[string]interface{}{ "obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e", "e": "src_obj1_obj3_e",
}, },
}, },
"inline_str_1": "src_inline_1", "inline_str_1": "src_inline_1",
...@@ -109,10 +109,10 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -109,10 +109,10 @@ func TestWalker_DataWalk(t *testing.T) {
"a": "src_obj1_a", "a": "src_obj1_a",
"b": "src_obj1_b", "b": "src_obj1_b",
"obj2": map[string]interface{}{ "obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a", "a": "src_obj1_obj2_a",
}, },
"obj3": map[string]interface{}{ "obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e", "e": "src_obj1_obj3_e",
}, },
}, },
"inline_str_1": "src_inline_1", "inline_str_1": "src_inline_1",
...@@ -162,10 +162,10 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -162,10 +162,10 @@ func TestWalker_DataWalk(t *testing.T) {
"a": "src_obj1_a", "a": "src_obj1_a",
"b": "src_obj1_b", "b": "src_obj1_b",
"obj2": map[string]interface{}{ "obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a", "a": "src_obj1_obj2_a",
}, },
"obj3": map[string]interface{}{ "obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e", "e": "src_obj1_obj3_e",
}, },
}, },
"inline_str_1": "src_inline_1", "inline_str_1": "src_inline_1",
...@@ -179,10 +179,10 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -179,10 +179,10 @@ func TestWalker_DataWalk(t *testing.T) {
"a": "src_obj1_a", "a": "src_obj1_a",
"b": "src_obj1_b", "b": "src_obj1_b",
"obj2": map[string]interface{}{ "obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a", "a": "src_obj1_obj2_a",
}, },
"obj3": map[string]interface{}{ "obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e", "e": "src_obj1_obj3_e",
}, },
}, },
"inline_str_1": "src_inline_1", "inline_str_1": "src_inline_1",
...@@ -196,10 +196,10 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -196,10 +196,10 @@ func TestWalker_DataWalk(t *testing.T) {
"a": "src_obj1_a", "a": "src_obj1_a",
"b": "src_obj1_b", "b": "src_obj1_b",
"obj2": map[string]interface{}{ "obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a", "a": "src_obj1_obj2_a",
}, },
"obj3": map[string]interface{}{ "obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e", "e": "src_obj1_obj3_e",
}, },
}, },
"inline_str_1": "src_inline_1", "inline_str_1": "src_inline_1",
...@@ -208,6 +208,220 @@ func TestWalker_DataWalk(t *testing.T) { ...@@ -208,6 +208,220 @@ func TestWalker_DataWalk(t *testing.T) {
}, },
false, false, false, false,
}, },
{"keep src nil",
&WalkConfig{
Fields: map[string]FieldConfig{
"a": {Fn: KeepSrc},
"obj1.a": {Fn: KeepSrc},
"inline_str_1": {Fn: KeepSrc},
"slice.1": {Fn: KeepSrc},
},
},
map[string]interface{}{
"b": "src_b",
"obj1": map[string]interface{}{
"b": "src_obj1_b",
"obj2": map[string]interface{}{
"a": "scr_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "src_obj1_obj3_e",
},
},
"slice": []interface{}{"src_s1"},
},
map[string]interface{}{
"a": "dst_a",
"b": "dst_b",
"obj1": map[string]interface{}{
"a": "dst_obj1_a",
"b": "dst_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_1": "dst_inline_1",
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"dst_s1", "dst_s2"},
},
map[string]interface{}{
"b": "dst_b",
"obj1": map[string]interface{}{
"b": "dst_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"dst_s1", nil},
},
true, false,
},
{"keep src nil #2",
&WalkConfig{
Fields: map[string]FieldConfig{
"a": {Fn: KeepSrc},
},
},
map[string]interface{}{},
map[string]interface{}{
"a": "dst_a",
},
map[string]interface{}{},
true, false,
},
{"keep src ni #3",
&WalkConfig{
Fields: map[string]FieldConfig{
"obj1": {Fn: KeepSrc},
"slice": {Fn: KeepSrc},
},
},
map[string]interface{}{
"b": "src_b",
},
map[string]interface{}{
"a": "dst_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": "dst_inline_1",
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"src_s1", "src_s2"},
},
map[string]interface{}{
"a": "dst_a",
"b": "src_b",
"inline_str_1": "dst_inline_1",
"inline_str_2": "dst_inline_2",
},
true, false,
},
{"keep src ni #3",
&WalkConfig{
Fields: map[string]FieldConfig{
"obj1.a": {Fn: KeepSrc},
"obj1.b": {Fn: KeepSrc},
"obj1.obj2": {Fn: KeepSrc},
"obj1.obj3": {Fn: KeepSrc},
},
},
map[string]interface{}{
"b": "src_b",
},
map[string]interface{}{
"a": "dst_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",
},
},
},
map[string]interface{}{
"a": "dst_a",
"b": "src_b",
"obj1": map[string]interface{}{},
},
true, false,
},
{"remove fields",
&WalkConfig{
Fields: map[string]FieldConfig{
"a": {Fn: RemoveValue},
"obj1.a": {Fn: RemoveValue},
"inline_str_1": {Fn: RemoveValue},
"slice.1": {Fn: RemoveValue},
},
},
map[string]interface{}{},
map[string]interface{}{
"a": "dst_a",
"b": "dst_b",
"obj1": map[string]interface{}{
"a": "dst_obj1_a",
"b": "dst_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_1": "dst_inline_1",
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"dst_s1", "dts_s2"},
},
map[string]interface{}{
"b": "dst_b",
"obj1": map[string]interface{}{
"b": "dst_obj1_b",
"obj2": map[string]interface{}{
"a": "dst_obj1_obj2_a",
},
"obj3": map[string]interface{}{
"e": "dst_obj1_obj3_e",
},
},
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"dst_s1", nil},
},
true, false,
},
{"remove fields #2",
&WalkConfig{
Fields: map[string]FieldConfig{
"obj1": {Fn: RemoveValue},
"slice": {Fn: RemoveValue},
},
},
map[string]interface{}{
"b": "src_b",
},
map[string]interface{}{
"a": "dst_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": "dst_inline_1",
"inline_str_2": "dst_inline_2",
"slice": []interface{}{"src_s1", "src_s2"},
},
map[string]interface{}{
"a": "dst_a",
"b": "src_b",
"inline_str_1": "dst_inline_1",
"inline_str_2": "dst_inline_2",
},
true, false,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
......
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