-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
227 lines (206 loc) · 5.33 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/jessevdk/go-flags"
"os"
"path/filepath"
"strings"
)
type Cli struct {
File string `short:"i" long:"input" description:"Path to the JSON file. Can be relative or absolute." required:"true"`
Output string `short:"o" long:"output" description:"Output file name."`
}
type TokenType string
const (
TokenTypeField TokenType = "field"
TokenTypeObject = "object"
TokenTypeArray = "array"
)
type Token struct {
Id string
Type TokenType
Depth int
Name string
Count int
fields []*Token
}
func (c *Token) Find(name string) (*Token, bool) {
for _, t := range c.fields {
if t.Name == name {
return t, true
}
}
return nil, false
}
func (c *Token) AddField(token *Token) {
c.fields = append(c.fields, token)
}
func (c *Token) Merge(other *Token) {
combined := make([]*Token, 0)
for _, ct := range c.fields {
combined = append(combined, ct)
if ot, contains := other.Find(ct.Name); contains {
ct.Count++
if ct.Type == TokenTypeObject {
ct.Merge(ot)
}
}
}
for _, ot := range other.fields {
if _, contains := c.Find(ot.Name); !contains {
combined = append(combined, ot)
}
}
c.fields = combined
}
type TokenSet struct {
root *Token
}
func printTokenSet(t *TokenSet) {
printObject(t.root, 0)
}
func printObject(object *Token, depth int) {
padding := strings.Repeat(" ", depth)
if object.Name == "" {
fmt.Println(fmt.Sprintf("%s{", padding))
} else {
fmt.Println(fmt.Sprintf("%s%s: { %d", padding, object.Name, object.Count))
}
for _, field := range object.fields {
if field.Type == TokenTypeField {
fmt.Println(fmt.Sprintf("%s%s: %d", strings.Repeat(" ", depth+1), field.Name, field.Count))
} else if field.Type == TokenTypeObject {
printObject(field, depth+1)
} else {
printArray(field, depth+1)
}
}
fmt.Println(fmt.Sprintf("%s}", padding))
}
func printArray(array *Token, depth int) {
padding := strings.Repeat(" ", depth)
if array.Name == "" {
fmt.Println(fmt.Sprintf("%s[ %d", padding, array.Count))
} else {
fmt.Println(fmt.Sprintf("%s%s: [ %d", padding, array.Name, array.Count))
}
for _, field := range array.fields {
if field.Type == TokenTypeField {
fmt.Println(fmt.Sprintf("%s%s: %d", strings.Repeat(" ", depth+1), field.Name, field.Count))
} else if field.Type == TokenTypeObject {
printObject(field, depth+1)
} else {
printArray(field, depth+1)
}
}
fmt.Println(fmt.Sprintf("%s]", padding))
}
func main() {
var cli Cli
_, err := flags.Parse(&cli)
if err != nil {
panic(err)
}
fmt.Printf("Parsing file %s\n", cli.File)
if filepath.Ext(cli.File) != ".json" {
fmt.Printf("WARN: file extension of %s is not standard\n", filepath.Ext(cli.File))
}
info, statErr := os.Stat(cli.File)
if statErr != nil {
panic(statErr)
}
if info.Size() == 0 {
panic(errors.New("file is empty\n"))
}
fileBytes, readErr := os.ReadFile(cli.File)
if readErr != nil {
panic(fileBytes)
}
var jsonObj map[string]interface{}
if err = json.Unmarshal(fileBytes, &jsonObj); err != nil {
panic(err)
}
tokenSet := &TokenSet{
root: &Token{
Id: uuid.New().String(),
Type: TokenTypeObject,
Depth: 0,
Name: "",
Count: 1,
fields: make([]*Token, 0),
},
}
processObject(jsonObj, tokenSet.root)
printTokenSet(tokenSet)
}
func processObject(jsonObject map[string]interface{}, container *Token) {
for key, value := range jsonObject {
if childObject, ok := value.(map[string]interface{}); ok {
childContainer, found := container.Find(key)
if !found {
childContainer = &Token{
Id: uuid.New().String(),
Name: key,
Depth: container.Depth + 1,
Count: 0,
Type: TokenTypeObject,
fields: make([]*Token, 0),
}
}
childContainer.Count++
container.AddField(childContainer)
processObject(childObject, childContainer)
} else if childArray, ok := value.([]interface{}); ok {
arrayContainer, found := container.Find(key)
if !found {
arrayContainer = &Token{
Id: uuid.New().String(),
Name: key,
Depth: container.Depth + 1,
Count: 0,
Type: TokenTypeArray,
fields: make([]*Token, 0),
}
}
arrayContainer.Count++
container.AddField(arrayContainer)
processArray(childArray, arrayContainer)
} else {
field, fieldFound := container.Find(key)
if !fieldFound {
field = &Token{
Name: key,
Type: TokenTypeField,
Count: 0,
}
container.AddField(field)
}
field.Count++
}
}
}
func processArray(jsonArray []interface{}, container *Token) {
for _, item := range jsonArray { // For each item in array
if object, ok := item.(map[string]interface{}); ok { // and that item is an object
objectContainer := &Token{
Id: uuid.New().String(),
fields: make([]*Token, 0),
}
processObject(object, objectContainer) // process the object
for _, objectField := range objectContainer.fields { // iterate over the processed fields
field, fieldFound := container.Find(objectField.Name) // determine if field exists in current container
if !fieldFound { // if not
container.AddField(objectField) // add field
} else { // otherwise
field.Count++ // increment
if field.Type == TokenTypeObject {
field.Merge(objectField)
}
}
}
}
}
}