-
Notifications
You must be signed in to change notification settings - Fork 1
/
delta.go
312 lines (285 loc) · 9.02 KB
/
delta.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package delta
import (
"fmt"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/sergi/go-diff/diffmatchpatch"
proto2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/anypb"
)
func Apply(delta *Delta, input proto2.Message) error {
var locPath []*Locator
if len(delta.Location) > 1 {
locPath = delta.Location[0 : len(delta.Location)-1]
}
locLast := delta.Location[len(delta.Location)-1]
val := getLocation(input.ProtoReflect(), locPath)
switch val := val.(type) {
case protoreflect.Message:
field := val.Descriptor().Fields().ByNumber(protoreflect.FieldNumber(locLast.V.(*Locator_Field).Field.Number))
if string(field.Name()) != locLast.V.(*Locator_Field).Field.Name {
panic("field name / number mismatch")
}
newValue, err := getValue(val.Get(field), delta.Value)
if err != nil {
return fmt.Errorf("getting value: %w", err)
}
val.Set(field, newValue)
case protoreflect.List:
idx := int(locLast.V.(*Locator_Index).Index)
newVaue, err := getValue(val.Get(idx), delta.Value)
if err != nil {
return fmt.Errorf("getting value: %w", err)
}
val.Set(idx, newVaue)
case protoreflect.Map:
key := getMapKey(locLast.V.(*Locator_Key).Key)
newValue, err := getValue(val.Get(key), delta.Value)
if err != nil {
return fmt.Errorf("getting value: %w", err)
}
val.Set(key, newValue)
default:
panic(fmt.Sprintf("unknown val type %T", val))
}
return nil
}
func getMapKey(key *Key) protoreflect.MapKey {
var mapKey protoreflect.MapKey
switch key := key.V.(type) {
case *Key_Bool:
mapKey = protoreflect.ValueOfBool(key.Bool).MapKey()
case *Key_Int32:
mapKey = protoreflect.ValueOfInt32(key.Int32).MapKey()
case *Key_Int64:
mapKey = protoreflect.ValueOfInt64(key.Int64).MapKey()
case *Key_Uint32:
mapKey = protoreflect.ValueOfUint32(key.Uint32).MapKey()
case *Key_Uint64:
mapKey = protoreflect.ValueOfUint64(key.Uint64).MapKey()
case *Key_Str:
mapKey = protoreflect.ValueOfString(key.Str).MapKey()
}
return mapKey
}
func getLoc(location LocatorInterface) []*Locator {
var loc []*Locator
for _, indexer := range location.Location_get() {
switch indexer := indexer.(type) {
case FieldIndexerStruct:
loc = append(loc, &Locator{V: &Locator_Field{Field: &Field{Name: indexer.Name, Number: int32(indexer.Number)}}})
case IndexIndexer:
loc = append(loc, &Locator{V: &Locator_Index{Index: int64(indexer)}})
case BoolKeyIndexer:
loc = append(loc, &Locator{V: &Locator_Key{Key: &Key{V: &Key_Bool{Bool: bool(indexer)}}}})
case Int32KeyIndexer:
loc = append(loc, &Locator{V: &Locator_Key{Key: &Key{V: &Key_Int32{Int32: int32(indexer)}}}})
case Int64KeyIndexer:
loc = append(loc, &Locator{V: &Locator_Key{Key: &Key{V: &Key_Int64{Int64: int64(indexer)}}}})
case Uint32KeyIndexer:
loc = append(loc, &Locator{V: &Locator_Key{Key: &Key{V: &Key_Uint32{Uint32: uint32(indexer)}}}})
case Uint64KeyIndexer:
loc = append(loc, &Locator{V: &Locator_Key{Key: &Key{V: &Key_Uint64{Uint64: uint64(indexer)}}}})
case StringKeyIndexer:
loc = append(loc, &Locator{V: &Locator_Key{Key: &Key{V: &Key_Str{Str: string(indexer)}}}})
}
}
return loc
}
func EditDiff(from, to string, location LocatorInterface) *Delta {
return &Delta{
Type: Delta_EditValue,
Location: getLoc(location),
Value: NewAnyDiff(from, to),
}
}
func EditValue(value interface{}, location LocatorInterface) *Delta {
return &Delta{
Type: Delta_EditValue,
Location: getLoc(location),
Value: NewAny(value),
}
}
func NewAnyDiff(from, to string) *anypb.Any {
dmp := diffmatchpatch.New()
dmp.DiffTimeout = time.Millisecond * 100
dlt := dmp.DiffToDelta(dmp.DiffCleanupSemantic(dmp.DiffMain(from, to, false)))
return mustAny(&Scalar{V: &Scalar_Delta{Delta: dlt}})
}
func NewAny(value interface{}) *anypb.Any {
// TODO: create separate functions for sint32, sint64, fixed32, fixed64, sfixed32, sfixed64 if needed
switch value := value.(type) {
case string:
return mustAny(&Scalar{V: &Scalar_Str{Str: value}})
case float64:
return mustAny(&Scalar{V: &Scalar_Double{Double: value}})
case float32:
return mustAny(&Scalar{V: &Scalar_Float{Float: value}})
case int64:
return mustAny(&Scalar{V: &Scalar_Int64{Int64: value}})
case int32:
return mustAny(&Scalar{V: &Scalar_Int32{Int32: value}})
case uint64:
return mustAny(&Scalar{V: &Scalar_Uint64{Uint64: value}})
case uint32:
return mustAny(&Scalar{V: &Scalar_Uint32{Uint32: value}})
case bool:
return mustAny(&Scalar{V: &Scalar_Bool{Bool: value}})
case []byte:
return mustAny(&Scalar{V: &Scalar_Bytes{Bytes: value}})
case proto.Message:
return mustAny(value)
default:
panic("unknown type in changeValue")
}
}
func Location(m proto2.Message, name string) int32 {
return int32(m.ProtoReflect().Descriptor().Fields().ByName(protoreflect.Name(name)).Number())
}
func getLocation(m protoreflect.Message, loc []*Locator) interface{} {
var current interface{} = m
for _, sel := range loc {
var value protoreflect.Value
switch c := current.(type) {
case protoreflect.Message:
fieldDescriptor := c.Descriptor().Fields().ByNumber(protoreflect.FieldNumber(sel.V.(*Locator_Field).Field.Number))
if string(fieldDescriptor.Name()) != sel.V.(*Locator_Field).Field.Name {
panic("field name / number mismatch")
}
value = c.Get(fieldDescriptor)
case protoreflect.List:
value = c.Get(int(sel.V.(*Locator_Index).Index))
case protoreflect.Map:
value = c.Get(getMapKey(sel.V.(*Locator_Key).Key))
default:
panic(fmt.Sprintf("unknown locator %T", current))
}
switch valueInterface := value.Interface().(type) {
case proto2.Message:
current = valueInterface.ProtoReflect()
case protoreflect.Message:
current = valueInterface
case protoreflect.List:
current = valueInterface
case protoreflect.Map:
current = valueInterface
default:
current = value
}
}
return current
}
func MustUnmarshalAny(any *anypb.Any) *ptypes.DynamicAny {
da := &ptypes.DynamicAny{}
err := ptypes.UnmarshalAny(any.ProtoReflect().Interface().(*anypb.Any), da)
if err != nil {
panic(err)
}
return da
}
func getValue(previous protoreflect.Value, a *anypb.Any) (protoreflect.Value, error) {
da := MustUnmarshalAny(a)
switch message := da.Message.(type) {
case *Scalar:
switch value := message.V.(type) {
case *Scalar_Float:
return protoreflect.ValueOfFloat32(value.Float), nil
case *Scalar_Double:
return protoreflect.ValueOfFloat64(value.Double), nil
case *Scalar_Int32:
return protoreflect.ValueOfInt32(value.Int32), nil
case *Scalar_Int64:
return protoreflect.ValueOfInt64(value.Int64), nil
case *Scalar_Uint32:
return protoreflect.ValueOfUint32(value.Uint32), nil
case *Scalar_Uint64:
return protoreflect.ValueOfUint64(value.Uint64), nil
case *Scalar_Bool:
return protoreflect.ValueOfBool(value.Bool), nil
case *Scalar_Str:
return protoreflect.ValueOfString(value.Str), nil
case *Scalar_Bytes:
return protoreflect.ValueOfBytes(value.Bytes), nil
case *Scalar_Delta:
prevString := previous.String()
dmp := diffmatchpatch.New()
diffs, err := dmp.DiffFromDelta(prevString, value.Delta)
if err != nil {
return protoreflect.Value{}, fmt.Errorf("error parsing delta: %w", err)
}
newString := dmp.DiffText2(diffs)
return protoreflect.ValueOfString(newString), nil
case *Scalar_Sint32, *Scalar_Sint64:
panic("unsupported type in value")
case *Scalar_Fixed32, *Scalar_Fixed64:
panic("unsupported type in value")
case *Scalar_Sfixed32, *Scalar_Sfixed64:
panic("unsupported type in value")
default:
panic("unknown type")
}
default:
value := protoreflect.ValueOfMessage(proto.MessageReflect(da.Message))
return value, nil
}
}
func mustAny(m proto.Message) *anypb.Any {
a, err := ptypes.MarshalAny(m)
if err != nil {
panic(err)
}
return a
}
type LocatorInterface interface {
Location_get() []Indexer
}
type Indexer interface {
isIndexer()
}
func FieldIndexer(name string, number int) FieldIndexerStruct {
return FieldIndexerStruct{name, number}
}
type FieldIndexerStruct struct {
Name string
Number int
}
func (FieldIndexerStruct) isIndexer() {}
type IndexIndexer int
func (IndexIndexer) isIndexer() {}
func KeyIndexer(key interface{}) Indexer {
switch key := key.(type) {
case bool:
return BoolKeyIndexer(key)
case int32:
return Int32KeyIndexer(key)
case int64:
return Int64KeyIndexer(key)
case uint32:
return Uint32KeyIndexer(key)
case uint64:
return Uint64KeyIndexer(key)
case string:
return StringKeyIndexer(key)
}
panic("unknown type")
}
type BoolKeyIndexer bool
type Int32KeyIndexer int32
type Int64KeyIndexer int64
type Uint32KeyIndexer uint32
type Uint64KeyIndexer uint64
type StringKeyIndexer string
func (BoolKeyIndexer) isIndexer() {}
func (Int32KeyIndexer) isIndexer() {}
func (Int64KeyIndexer) isIndexer() {}
func (Uint32KeyIndexer) isIndexer() {}
func (Uint64KeyIndexer) isIndexer() {}
func (StringKeyIndexer) isIndexer() {}
func CopyAndAppend(in []Indexer, v Indexer) []Indexer {
out := make([]Indexer, len(in)+1)
copy(out, in)
out[len(out)-1] = v
return out
}