-
Notifications
You must be signed in to change notification settings - Fork 0
/
builders.go
78 lines (73 loc) · 1.81 KB
/
builders.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
package k6avrogen
func toMap(schema any) map[string]any {
return schema.(map[string]any)
}
func copyMap(source map[string]any) map[string]any {
buf := make(map[string]any)
for k, v := range source {
buf[k] = v
}
return buf
}
func makeNullable(schema map[string]any, def any) map[string]any {
return map[string]any{
"name": schema["name"],
"type": []any{"null", copyMap(schema)},
"default": def,
}
}
func PrimitiveBuilder(schema map[string]any, t string, isNullable bool) any {
def, ok := schema["default"]
var typ []string
if ok {
typ = []string{t, "null"}
} else {
def = nil
typ = []string{"null", t}
}
if isNullable {
schema["type"] = typ
schema["default"] = def
}
return schema
}
func RecordBuilder(schema map[string]any, t string, isNullable bool) any {
schema["type"] = t
sFields := schema["fields"].([]any)
fields := make([]any, len(sFields))
for i, field := range sFields {
fields[i] = toAvroSchema(toMap(field))
}
schema["fields"] = fields
return schema
}
func ArrayBuilder(schema map[string]any, t string, isNullable bool) any {
schema["type"] = t
schema["items"] = toAvroSchema(toMap(schema["items"]))
if isNullable {
return makeNullable(schema, []any{})
}
return schema
}
func MapBuilder(schema map[string]any, t string, isNullable bool) any {
schema["type"] = t
schema["values"] = toAvroSchema(toMap(schema["values"]))
if isNullable {
return makeNullable(schema, map[string]any{})
}
return schema
}
func UnionBuilder(schema map[string]any, t string, isNullable bool) any {
sVariants := schema["variants"].([]any)
variants := make([]any, len(sVariants))
for _, variant := range sVariants {
switch variant.(type) {
case string:
variant = map[string]any{
"type": variant,
}
}
variants = append(variants, toAvroSchema(toMap(variant)))
}
return schema
}