/
typebuilder.go
113 lines (94 loc) · 2.38 KB
/
typebuilder.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
// typebuilder.go
package n3gql
import (
"context"
"math"
"github.com/tidwall/gjson"
)
//
// receives SchemData objects post-classification and decomposes
// data into granular types for use in graphql schema generation
//
func typeBuilder(ctx context.Context, in <-chan SchemaData) (
<-chan SchemaData, // emits SchemaData objects with gql types map
<-chan error, // emits errors encountered to the pipeline manager
error) { // any error encountered when creating this component
out := make(chan SchemaData)
errc := make(chan error, 1)
go func() {
defer close(out)
defer close(errc)
for schemadata := range in { // read schema-data from upstream source
sd := schemadata
sd.Types = make(map[string]map[string]string)
result := gjson.ParseBytes(sd.RawData)
assignObject(sd.Types, "", result)
select {
case out <- sd: // pass the data package on to the next stage
case <-ctx.Done(): // listen for pipeline shutdown
return
}
}
}()
return out, errc, nil
}
//
// splits out the parent / child types from the json objects
// to create the necessary graphql schema definitions
//
func assignObject(tm map[string]map[string]string, parentKey string, val gjson.Result) {
if parentKey == "" {
parentKey = "n3-structure"
}
// see if we already have an entry for this outer key
t, ok := tm[parentKey]
if !ok {
tm[parentKey] = make(map[string]string)
t = tm[parentKey]
}
switch {
case val.IsObject():
val.ForEach(func(key, val gjson.Result) bool {
switch {
case val.IsObject():
t[key.String()] = key.String()
assignObject(tm, key.String(), val)
case val.IsArray():
arrayKey := key.String()
t[arrayKey] = "[" + arrayKey + "]"
val.ForEach(func(key, val gjson.Result) bool {
if val.IsObject() {
assignObject(tm, arrayKey, val)
} else {
t[arrayKey] = "[" + deriveScalarType(val) + "]"
}
return true
})
default:
t[key.String()] = deriveScalarType(val)
}
return true
})
default:
t[parentKey] = deriveScalarType(val)
}
tm[parentKey] = t
}
func deriveScalarType(val gjson.Result) string {
switch val.Type {
case gjson.Null:
return "String" // best guess
case gjson.False, gjson.True:
return "Boolean"
case gjson.Number:
if val.Float() == math.Trunc(val.Float()) {
return "Int"
} else {
return "Float"
}
case gjson.String:
return "String"
default:
return "String"
}
}