-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearize_merge.go
96 lines (90 loc) · 2.94 KB
/
linearize_merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package linearize
// Merge applies the UpdateMask operations (ADD, UPDATE, REMOVE) to the current LinearizedObject
// directly modifying it using the diff and the UpdateMask.
func Merge(mask *UpdateMask, current LinearizedObject, diff LinearizedObject) error {
// Apply operations based on the mask
for pos, maskValue := range mask.Values {
switch maskValue.Op {
case UpdateMaskOperation_ADD, UpdateMaskOperation_UPDATE:
// If there's a nested mask, merge recursively for nested structures
if maskValue.Masks != nil {
if nestedVal, exists := current[pos]; exists {
// Handle nested structures: LinearizedObject, LinearizedSlice, LinearizedMap
switch nestedVal := nestedVal.(type) {
case LinearizedObject:
// Recursively merge LinearizedObjects
err := Merge(maskValue.Masks, nestedVal, diff[pos].(LinearizedObject))
if err != nil {
return err
}
case LinearizedSlice:
// Handle merging of LinearizedSlice (slices)
err := mergeSlices(maskValue.Masks, nestedVal, diff[pos].(LinearizedSlice))
if err != nil {
return err
}
case LinearizedMap:
// Handle merging of LinearizedMap
err := mergeMaps(maskValue.Masks, nestedVal, diff[pos].(LinearizedMap))
if err != nil {
return err
}
}
}
} else {
if diffVal, exists := diff[pos]; exists {
// Update the current object with the value from the diff
current[pos] = diffVal
}
}
case UpdateMaskOperation_REMOVE:
// For REMOVE, delete the key from the current object
delete(current, pos)
}
}
return nil
}
// mergeSlices merges two LinearizedSlice types using the update mask
func mergeSlices(mask *UpdateMask, current, diff LinearizedSlice) error {
// Apply operations based on the mask
for pos, maskValue := range mask.Values {
switch maskValue.Op {
case UpdateMaskOperation_ADD:
// For ADD, apply the diff if it exists
if diffVal, exists := diff[pos]; exists {
current[pos] = diffVal
}
case UpdateMaskOperation_REMOVE:
// For REMOVE, delete the value at the specified position
delete(current, pos)
case UpdateMaskOperation_UPDATE:
// For UPDATE, apply the diff if it exists
if diffVal, exists := diff[pos]; exists {
current[pos] = diffVal
}
}
}
return nil
}
// mergeMaps merges two LinearizedMap types using the update mask
func mergeMaps(mask *UpdateMask, current, diff LinearizedMap) error {
// Apply operations based on the mask
for pos, maskValue := range mask.Values {
switch maskValue.Op {
case UpdateMaskOperation_ADD:
// For ADD, apply the diff if it exists
if diffVal, exists := diff[pos]; exists {
current[pos] = diffVal
}
case UpdateMaskOperation_REMOVE:
// For REMOVE, delete the key from the current map
delete(current, pos)
case UpdateMaskOperation_UPDATE:
// For UPDATE, apply the diff if it exists
if diffVal, exists := diff[pos]; exists {
current[pos] = diffVal
}
}
}
return nil
}