-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfieldinfo.go
114 lines (89 loc) · 2.71 KB
/
fieldinfo.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
package main
import (
"reflect"
"strings"
)
// FieldInfo holds information about a field being processed
type FieldInfo struct {
name string
// extName is the last two words of the field name in a CamelCase string
extName string
// typ is the type of the BPMN element
typ string
element processElement
// hash is the current hash value of the BPMN element
hash string
hashBefore string
nextHash string
}
// extractFieldInfo gathers all necessary field information.
func extractFieldInfo(field reflect.Value, idx int) FieldInfo {
return FieldInfo{
name: field.Type().Field(idx).Name,
extName: extractLastTwoWords(field.Type().Field(idx).Name),
typ: field.Field(idx).FieldByName("Type").String(),
element: matchElementType(field.Type().Field(idx).Name),
hash: field.Field(idx).FieldByName("Hash").String(),
hashBefore: field.Field(idx - 1).FieldByName("Hash").String(),
nextHash: getNextHash(field, idx, field.NumField()),
}
}
// isValidField checks if a field is valid
func isValidField(info FieldInfo) bool {
if info.name == "" {
return false
}
if info.typ == "" {
return false
}
if info.element == "" {
return false
}
return true
}
// collectFromFieldsWithNeighbors collects information from fields with neighbors
// by recursively traversing the data structure.
func collectFromFieldsWithNeighbors(data interface{}) map[string]map[string]interface{} {
results := make(map[string]map[string]interface{})
val := reflect.ValueOf(data)
// pointer dereferencing
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
switch val.Kind() {
case reflect.Struct:
var sourceRefFieldName string
var sourceRefFieldValue interface{}
var targetRefFieldName string
var targetRefFieldValue interface{}
// iterate over all fields
for i := 0; i < val.NumField(); i++ {
field := val.Type().Field(i)
fieldValue := val.Field(i)
fieldName := field.Name
// find "From" fields
if strings.HasPrefix(fieldName, "From") {
fieldInfo := make(map[string]interface{})
fieldInfo["Value"] = fieldValue.Interface()
// get previous field (if any)
if i > 0 {
sourceRefFieldName = val.Type().Field(i - 1).Name
sourceRefFieldValue = val.Field(i - 1).Interface()
fieldInfo["SourceRef"] = map[string]interface{}{
"FieldName": sourceRefFieldName,
"Value": sourceRefFieldValue,
}
targetRefFieldName = val.Type().Field(i + 1).Name
targetRefFieldValue = val.Field(i + 1).Interface()
fieldInfo["TargetRef"] = map[string]interface{}{
"FieldName": targetRefFieldName,
"Value": targetRefFieldValue,
}
}
// save field information
results[fieldName] = fieldInfo
}
}
}
return results
}