Skip to content

Commit

Permalink
fix(shadow): when showdow's field is array, merge error
Browse files Browse the repository at this point in the history
  • Loading branch information
ogofly committed Jan 3, 2024
1 parent 4401ed4 commit 1a3a513
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions shadow/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shadow
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -228,6 +229,15 @@ func deltaDiff(key string, target, source any, meta map[string]any) (delta any,
}
}
}
case []interface{}:
if reflect.DeepEqual(t, source) {
return
} else {
delta = target
if m, ok := meta[key]; ok {
deltaMeta = m
}
}
default:
log.Errorf("unexpected value for shadow, key %q value %v", key, target)
}
Expand Down
27 changes: 27 additions & 0 deletions shadow/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,33 @@ func TestMerge_DeltaState(t *testing.T) {
}
}`,
},

// Array field test. An array is treated as a field
{
desired: `{
"arr1": [1,2,3],
"arr2": [{"a": 1}, {"b": 2}],
"arr3": ["a", "b", "c"]
}`,
reported: `{
"arr": [1,2,4],
"arr2": [{"a": 1}, {"b": 3}],
"arr3": ["a", "b", "c"]
}`,
desiredMeta: `{
"arr1": { "timestamp": 1665555014139 },
"arr2": {"timestamp": 1665555014220},
"arr3": {"timestamp": 1665555023110}
}`,
delta: `{
"arr1": [1,2,3],
"arr2": [{"a": 1}, {"b": 2}]
}`,
deltaMeta: `{
"arr1": {"timestamp": 1665555014139},
"arr2": {"timestamp": 1665555014220}
}`,
},
}

s2m := func(s string) map[string]any {
Expand Down

0 comments on commit 1a3a513

Please sign in to comment.