-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_exported.go
More file actions
151 lines (126 loc) · 3.1 KB
/
node_exported.go
File metadata and controls
151 lines (126 loc) · 3.1 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
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
package map2struct
import (
"fmt"
"strconv"
)
// Node - object used to ingest map[string]interface{}
type Node struct {
Name string // the name of the key field [the top level is always 'root']
Root *Node // the root of the key field [the level above - if it is root, it's nil]
sub map[string][]*Node // the array of keys underneath this level
field map[string]string // the fields of this level
array map[string][]string // the arrays of this level
}
// Ingest - grab a map[string]interface{} ready to be investigated into more
func (e *Node) Ingest(m map[string]interface{}) {
e.field = make(map[string]string)
e.array = make(map[string][]string)
e.sub = make(map[string][]*Node)
if e.Root == nil {
e.Name = "root"
}
for key, v := range m {
switch vv := v.(type) {
case string:
value := vv
e.field[key] = value
case float64:
value := strconv.FormatFloat(vv, 'f', -1, 64)
e.field[key] = value
case bool:
value := strconv.FormatBool(vv)
e.field[key] = value
case []interface{}:
contents := e.ingestInterfaceSlice(vv, key)
e.array[key] = contents
case map[string]interface{}:
sub := &Node{}
sub.Name = key
sub.Root = e
sub.Ingest(vv)
e.sub[key] = append(e.sub[key], sub)
case nil:
value := "NULL"
e.field[key] = value
default:
e.field[key] = vv.(string) + "_type_not_supported"
}
}
}
// Get - grab an array of struct subentities
func (e *Node) Get(sub string) []*Node {
if _, ok := e.sub[sub]; ok {
return e.sub[sub]
}
fmt.Println(sub, "- is not a subentity")
return []*Node{e}
}
// Field - return the field [by name] of a struct
func (e *Node) Field(field string) string {
if _, ok := e.field[field]; ok {
return e.field[field]
}
fmt.Println(field, "- is not a field of this entity")
return ""
}
// Fields - return the field names of a struct as an array
func (e *Node) Fields() []string {
fields := []string{}
if len(e.field) != 0 {
for key := range e.field {
fields = append(fields, key)
}
}
return fields
}
// Arrays - return the array names of a struct
func (e *Node) Arrays() []string {
arrays := []string{}
if len(e.array) != 0 {
for key := range e.array {
if len(e.array[key]) != 0 {
arrays = append(arrays, key)
}
}
}
return arrays
}
// Array - return the array [by name] of a struct
func (e *Node) Array(array string) []string {
if _, ok := e.array[array]; ok {
return e.array[array]
}
fmt.Println(array, "- is not an array of this entity")
return []string{}
}
// Print - print out a struct
func (e *Node) Print() {
var (
rootName string
entities []string
fields []string
arrays []string
)
if e.Root != nil {
rootName = e.Root.Name
}
if len(e.field) != 0 {
for key := range e.field {
fields = append(fields, key)
}
}
if len(e.array) != 0 {
for key := range e.array {
if len(e.array[key]) != 0 {
arrays = append(arrays, key)
}
}
}
if len(e.sub) != 0 {
for key := range e.sub {
entities = append(entities, key)
}
}
fmt.Printf("name: %s\nroot: %s\nfield: %s\narray: %v\nsub: %v\n",
e.Name, rootName, fields, arrays, entities)
}