Repository navigation
Expand file tree
/
Copy pathdiff.go
More file actions
54 lines (49 loc) · 1.5 KB
/
Copy pathdiff.go
File metadata and controls
54 lines (49 loc) · 1.5 KB
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
package parse
import (
"fmt"
"reflect"
"strings"
"github.com/sourcegraph/sourcegraph/schema"
)
// diff returns names of the Go fields that have different values between the
// two configurations.
func diff(before, after *schema.SiteConfiguration) (fields map[string]struct{}) {
fields = make(map[string]struct{})
beforeFields := getJSONFields(before)
afterFields := getJSONFields(after)
for fieldName, beforeField := range beforeFields {
afterField := afterFields[fieldName]
if !reflect.DeepEqual(beforeField, afterField) {
fields[fieldName] = struct{}{}
}
}
return fields
}
func getJSONFields(vv interface{}) (fields map[string]interface{}) {
fields = make(map[string]interface{})
v := reflect.ValueOf(vv).Elem()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
tag := v.Type().Field(i).Tag.Get("json")
if tag == "" {
// should never happen, and if it does this func cannot work.
panic(fmt.Sprintf("missing json struct field tag on %T field %q", v.Interface(), v.Type().Field(i).Name))
}
if ef, ok := f.Interface().(*schema.ExperimentalFeatures); ok && ef != nil {
for fieldName, fieldValue := range getJSONFields(ef) {
fields["experimentalFeatures::"+fieldName] = fieldValue
}
continue
}
fieldName := parseJSONTag(tag)
fields[fieldName] = f.Interface()
}
return fields
}
// parseJSONTag parses a JSON struct field tag to return the JSON field name.
func parseJSONTag(tag string) string {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx]
}
return tag
}