/
util.go
383 lines (359 loc) · 10.1 KB
/
util.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* util.go: provides useful methods used throughout Kraken
*
* Author: J. Lowell Wofford <lowell@lanl.gov>
*
* This software is open source software available under the BSD-3 license.
* Copyright (c) 2018, Triad National Security, LLC
* See LICENSE file for details.
*/
package util
import (
fmt "fmt"
"reflect"
"strconv"
"strings"
proto "github.com/gogo/protobuf/proto"
ptypes "github.com/gogo/protobuf/types"
"github.com/kraken-hpc/kraken/lib/types"
)
// MessageDiff compares a & b, returns a slice of differing field names
// returns an error if types are different.
// This borrows a lot from the proto.Merge code
func MessageDiff(a, b proto.Message, pre string) (r []string, e error) {
/*
* Sanity checks
*/
va := reflect.ValueOf(a)
vb := reflect.ValueOf(b)
ta := reflect.TypeOf(a)
tb := reflect.TypeOf(b)
// Is either object nil?
if va.IsNil() || vb.IsNil() {
e = fmt.Errorf("cannot diff a nil")
return
}
if ta != tb {
e = fmt.Errorf("refusing to diff objects of different type")
return
}
return diffStruct(va.Elem(), vb.Elem(), pre)
}
func diffStruct(a, b reflect.Value, pre string) (r []string, e error) {
if !a.IsValid() || !b.IsValid() {
e = fmt.Errorf("diffStruct called on invalid value(s)")
return
}
if a.Type() != b.Type() {
e = fmt.Errorf("diffStruct called on mismatched values types: %s vs %s", a.Type(), b.Type())
return
}
for i := 0; i < a.NumField(); i++ {
f := a.Type().Field(i)
if f.Name == "Extensions" || // don't include extensions
f.Name == "Services" || // don't include services
f.Name == "Children" || // don't include children
f.Name == "Parents" || // don't include parents
f.Anonymous || // don't include anonymous fields
f.PkgPath != "" || // don't include unexported fields (see reflect docs)
strings.HasPrefix(f.Name, "XXX_") { // don't include proto XXX_* fields
continue
}
s, e := diffAny(a.Field(i), b.Field(i), URLPush(pre, f.Name))
if e != nil {
return r, e
}
r = append(r, s...)
}
return
}
func diffMap(a, b reflect.Value, pre string) (r []string, e error) {
if !a.IsValid() || !b.IsValid() {
e = fmt.Errorf("diffMap called on invalid value(s)")
return
}
if a.Type() != b.Type() {
e = fmt.Errorf("diffMap called on mismatched values types: %s vs %s", a.Type(), b.Type())
return
}
r = []string{}
for _, k := range a.MapKeys() {
av := a.MapIndex(k)
bv := b.MapIndex(k)
if !bv.IsValid() {
// doesn't exist in b, mark the whole key as a diff
r = append(r, URLPush(pre, ValueToString(k)))
continue
}
// exists in both
var s []string
if s, e = diffAny(av, bv, URLPush(pre, ValueToString(k))); e != nil {
return
}
r = append(r, s...)
}
for _, k := range b.MapKeys() {
if !a.MapIndex(k).IsValid() {
// doesn't exist in a, mark the whole key as a diff
r = append(r, URLPush(pre, ValueToString(k)))
}
}
return
}
func diffSlice(a, b reflect.Value, pre string) (r []string, e error) {
alen := a.Len()
blen := b.Len()
// pad our slices to equal length
if alen > blen {
pad := reflect.MakeSlice(a.Type(), alen-blen, alen-blen)
b = reflect.AppendSlice(b, pad)
} else if blen > alen {
pad := reflect.MakeSlice(a.Type(), blen-alen, blen-alen)
a = reflect.AppendSlice(a, pad)
}
for i := 0; i < a.Len(); i++ {
s, e := diffAny(a.Index(i), b.Index(i), URLPush(pre, strconv.Itoa(i)))
if e != nil {
return r, e
}
r = append(r, s...)
}
return
}
func diffAny(a, b reflect.Value, pre string) (r []string, e error) {
// TODO: we'll deal with this case later
/*
if a.Type() == reflect.TypeOf((*proto.Message)(nil)).Elem() {
}
*/
switch a.Kind() {
case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
reflect.String, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if !a.CanInterface() || !b.CanInterface() {
return []string{}, fmt.Errorf("attempted to diff private struct fields")
}
if a.Interface() != b.Interface() {
return []string{pre}, nil
}
case reflect.Struct:
return diffStruct(a, b, pre)
case reflect.Slice:
return diffSlice(a, b, pre)
case reflect.Ptr:
if a.IsNil() || b.IsNil() {
if a.IsNil() && b.IsNil() {
return
}
r = append(r, pre)
return
}
return diffAny(a.Elem(), b.Elem(), pre)
case reflect.Map:
return diffMap(a, b, pre)
case reflect.Interface:
fallthrough
default:
//log.Printf("not yet implemented: %v", a.Kind())
//log.Printf("unknown type")
}
return
}
// ResolveURL turns a state URL (string) into an interface reference
func ResolveURL(url string, context reflect.Value) (v reflect.Value, e error) {
if url == "" && context.Kind() != reflect.Ptr {
v = context
return
}
root, sub := URLShift(url)
switch context.Kind() {
case reflect.Map: // root should be a map key
k := StringToValue(root, context.Type().Key())
kv := context.MapIndex(k)
if !kv.IsValid() {
// key is not in map
e = fmt.Errorf("no such map key: %s", root)
return
}
v, e = ResolveURL(sub, kv)
case reflect.Ptr: // resolve pointers to their target
v, e = ResolveURL(url, context.Elem())
case reflect.Slice: // root should be a slice index
i, err := strconv.Atoi(root)
if err != nil {
e = fmt.Errorf("non-integer child of slice: %s", root)
return
}
if context.Len()-1 < i {
e = fmt.Errorf("no such slice index: %d", i)
return
}
v, e = ResolveURL(sub, context.Index(i))
case reflect.Struct: // root should be a field name
if context.Type() == reflect.TypeOf(ptypes.Any{}) {
any := context.Interface().(ptypes.Any)
var da ptypes.DynamicAny
if err := ptypes.UnmarshalAny(&any, &da); err != nil {
e = fmt.Errorf("failed to unmarshal ptypes.Any object, %v", err)
return
}
v, e = ResolveURL(root, reflect.ValueOf(da.Message).Elem())
} else if f := context.FieldByName(root); !f.IsValid() {
e = fmt.Errorf("field not found in struct, %s", root)
} else {
v, e = ResolveURL(sub, f)
}
default:
e = fmt.Errorf("cannot resolve property, %s, in simple type, %s", root, context.Kind().String())
}
return
}
// ResolveOrMakeURL is like ResolveURL, except it will create any referenced but non-defined objects
func ResolveOrMakeURL(url string, context reflect.Value) (v reflect.Value, e error) {
if url == "" && context.Kind() != reflect.Ptr {
v = context
return
}
root, sub := URLShift(url)
switch context.Kind() {
case reflect.Map:
if context.IsNil() {
// map has not been allocated
m := reflect.MakeMap(context.Type())
context.Set(m)
}
k := StringToValue(root, context.Type().Key())
kv := context.MapIndex(k)
if !kv.IsValid() {
// key is not in map
// create an empty entry
if context.Type().Elem().Kind() == reflect.Ptr {
// we need to make a real object, not just an empty pointer
kv = reflect.New(context.Type().Elem().Elem())
} else {
kv = reflect.New(context.Type().Elem()).Elem()
}
context.SetMapIndex(k, kv)
}
v, e = ResolveOrMakeURL(sub, kv)
case reflect.Ptr: // resolve pointers to their target
if !context.Elem().IsValid() {
// we need to allocate an object here
new := reflect.New(context.Type().Elem())
context.Set(new)
}
v, e = ResolveOrMakeURL(url, context.Elem())
case reflect.Slice: // root should be a slice index
i, err := strconv.Atoi(root)
if err != nil {
e = fmt.Errorf("non-integer child of slice: %s", root)
return
}
if context.Len()-1 < i {
// grow the slice as needed
len := i + 1 - context.Len()
t := context.Type()
a := reflect.MakeSlice(t, len, len)
context.Set(reflect.AppendSlice(context, a))
}
v, e = ResolveOrMakeURL(sub, context.Index(i))
case reflect.Struct: // root should be a field name
if f := context.FieldByName(root); !f.IsValid() {
e = fmt.Errorf("field not found in struct, %s", root)
} else {
v, e = ResolveOrMakeURL(sub, f)
}
default:
e = fmt.Errorf("cannot resolve property, %s, in simple type, %s", root, context.Kind().String())
}
return
}
// URLShift gives the current root of a url and the remaining url
func URLShift(url string) (root string, sub string) {
ret := strings.SplitN(url, "/", 2)
switch len(ret) {
case 0:
root = ""
sub = ""
case 1:
root = url
sub = ""
case 2:
if ret[0] == "" { // beginning slash
ret[0], ret[1] = URLShift(ret[1])
}
root = ret[0]
sub = ret[1]
}
return
}
func URLPush(url string, elem string) string {
return url + "/" + elem
}
func URLToSlice(url string) (s []string) {
s = strings.Split(url, "/")
return
}
func SliceToURL(s []string) (url string) {
url = strings.Join(s, "/")
return
}
func NodeURLSplit(s string) (node string, url string) {
ret := strings.SplitN(s, ":", 2)
switch len(ret) {
case 0: // empty string
node = ""
url = ""
case 1: // we have just a node?
node = ret[0]
url = ""
case 2:
node = ret[0]
url = ret[1]
break
}
return
}
func NodeURLJoin(node, url string) string {
return node + ":" + url
}
// ValueToString does its best to convert values into sensible strings for printing
func ValueToString(v reflect.Value) (s string) {
switch v.Kind() {
case reflect.String:
s = v.String()
case reflect.Uint:
s = fmt.Sprintf("%d", v.Uint())
case reflect.Int:
s = fmt.Sprintf("%d", v.Int())
case reflect.Bool:
s = fmt.Sprintf("%t", v.Bool())
case reflect.Struct:
s = fmt.Sprintf("%v", v.Interface())
default:
s = fmt.Sprintf("%v", v)
}
return
}
// StringToValue creats values for a type. It returns a zero for the type if it's an unsupported type
// We always return a valid value. If we fail to parse, you get a zero value
func StringToValue(s string, t reflect.Type) (r reflect.Value) {
r = reflect.New(t).Elem()
switch t.Kind() {
case reflect.String:
r.SetString(s)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if ui, e := strconv.ParseUint(s, 10, 64); e == nil {
r.SetUint(ui)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if i, e := strconv.ParseInt(s, 10, 64); e == nil {
r.SetInt(i)
}
default:
fmt.Printf("unsupproted StringToValue type: %v", t.Kind())
}
// get a zero value for unkown types
return
}
func ProtoName(m types.Message) string {
return types.ProtoUrlPrefix + proto.MessageName(m)
}