-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
176 lines (153 loc) · 4.37 KB
/
schema.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
package parquetx
import (
"encoding/json"
"fmt"
"github.com/modfin/henry/slicez"
s1 "github.com/xitongsys/parquet-go/schema"
"reflect"
"regexp"
"strings"
)
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
var typemap = map[string]string{
"bool": "BOOLEAN",
"byte": "INT32",
"int8": "INT32",
"int16": "INT32",
"int32": "INT32",
"int64": "INT64",
"int": "INT64",
"uint8": "INT32",
"uint16": "INT32",
"uint32": "INT32",
"uint64": "INT64",
"uint": "INT64",
"float32": "FLOAT",
"float64": "DOUBLE",
"string": "BYTE_ARRAY",
}
var convtypes = map[string]string{
"string": "UTF8",
"int8": "INT_8",
"int16": "INT_16",
"int32": "INT_32",
"int64": "INT_64",
"int": "INT_64",
"byte": "UINT_8",
"uint8": "UINT_8",
"uint16": "UINT_16",
"uint32": "UINT_32",
"uint64": "UINT_64",
"uint": "UINT_64",
}
func NewJSONSchemaFromStruct(obj any) (str string, err error) {
j := s1.NewJSONSchemaItem()
j.Tag = "name=parquet_go_root, inname=Parquet_go_root repetitiontype=REQUIRED"
var getFields func(obj reflect.Type) []*s1.JSONSchemaItemType
var ensureRequire = func(tag *s1.JSONSchemaItemType) *s1.JSONSchemaItemType {
if !strings.Contains(tag.Tag, "repetitiontype=") {
tag.Tag = fmt.Sprintf("%s, repetitiontype=REQUIRED", tag.Tag)
}
return tag
}
getFields = func(ot reflect.Type) (tags []*s1.JSONSchemaItemType) {
defer func() {
tags = slicez.Map(tags, ensureRequire)
}()
next := func(f reflect.StructField) reflect.Type {
t := f.Type
if f.Type.Kind() == reflect.Ptr {
t = f.Type.Elem()
}
return t
}
if ot.Kind() == reflect.Ptr { // add something about optional in tag?
ot = ot.Elem()
}
if ot.Kind() == reflect.Map {
keytype := typemap[ot.Key().String()]
if len(keytype) > 0 {
tag := &s1.JSONSchemaItemType{Tag: fmt.Sprintf("name=Key, type=%s, repetitiontype=REQUIRED", keytype)}
tags = append(tags, tag)
}
valtype := typemap[ot.Elem().Name()]
if len(valtype) == 0 {
tag := &s1.JSONSchemaItemType{Tag: fmt.Sprintf("name=Value, repetitiontype=REQUIRED")}
tag.Fields = getFields(ot.Elem())
tags = append(tags, tag)
}
if len(valtype) > 0 {
tag := &s1.JSONSchemaItemType{Tag: fmt.Sprintf("name=Value, type=%s, repetitiontype=REQUIRED", valtype)}
tags = append(tags, tag)
}
}
if ot.Kind() == reflect.Slice {
switch ot.Elem().Kind() {
case reflect.Slice, reflect.Struct, reflect.Map:
return getFields(ot.Elem())
}
}
if ot.Kind() == reflect.Struct {
numField := int32(ot.NumField())
for i := 0; i < int(numField); i++ {
f := ot.Field(i)
tagStr := f.Tag.Get("parquet")
if tagStr == "-" {
continue
}
if len(tagStr) > 0 { // todo ensure types
t := &s1.JSONSchemaItemType{Tag: tagStr}
t.Fields = getFields(next(f))
tags = append(tags, t)
continue
}
//ignore item without parquet tag
parts := []string{
fmt.Sprintf("name=%s", ToSnakeCase(f.Name)),
fmt.Sprintf("inname=%s", f.Name),
}
name := f.Type.Kind().String()
_type := typemap[name]
_convtype := convtypes[name]
if name == "ptr" {
name = f.Type.Elem().String()
_type = typemap[name]
_convtype = convtypes[name]
parts = append(parts, "repetitiontype=OPTIONAL")
}
if name == "map" {
parts = append(parts, "type=MAP")
parts = append(parts, "repetitiontype=REQUIRED")
}
if name == "slice" && f.Type.Elem().Name() == "byte" {
_type = "BYTE_ARRAY"
_convtype = ""
} else if name == "slice" {
_type = typemap[f.Type.Elem().Name()]
_convtype = convtypes[f.Type.Elem().Name()]
parts = append(parts, "repetitiontype=REPEATED")
}
if len(_type) > 0 {
parts = append(parts, fmt.Sprintf("type=%s", _type))
}
if len(_convtype) > 0 {
parts = append(parts, fmt.Sprintf("convertedtype=%s", _convtype))
}
tagStr = strings.Join(parts, ", ")
t := &s1.JSONSchemaItemType{Tag: tagStr}
t.Fields = getFields(next(f))
tags = append(tags, t)
}
}
return tags
}
j.Fields = getFields(reflect.TypeOf(obj).Elem())
b, err := json.MarshalIndent(j, "", " ")
return string(b), err
}