forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_schema.go
77 lines (75 loc) · 1.97 KB
/
json_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
package docs
// JSONSchema serializes a field spec into a JSON schema structure.
func (f FieldSpec) JSONSchema() interface{} {
spec := map[string]interface{}{}
switch f.Kind {
case Kind2DArray:
innerField := f
innerField.Kind = KindArray
spec["type"] = "array"
spec["items"] = innerField.JSONSchema()
case KindArray:
innerField := f
innerField.Kind = KindScalar
spec["type"] = "array"
spec["items"] = innerField.JSONSchema()
case KindMap:
innerField := f
innerField.Kind = KindScalar
spec["type"] = "object"
spec["patternProperties"] = map[string]interface{}{
".": innerField.JSONSchema(),
}
default:
switch f.Type {
case FieldTypeBool:
spec["type"] = "boolean"
case FieldTypeString:
spec["type"] = "string"
case FieldTypeInt:
spec["type"] = "number"
case FieldTypeFloat:
spec["type"] = "number"
case FieldTypeObject:
spec["type"] = "object"
spec["properties"] = f.Children.JSONSchema()
var required []string
for _, child := range f.Children {
if !child.IsOptional && child.Default == nil {
required = append(required, child.Name)
}
}
if len(required) > 0 {
spec["required"] = required
}
spec["additionalProperties"] = false
case FieldTypeInput:
spec["$ref"] = "#/$defs/input"
case FieldTypeBuffer:
spec["$ref"] = "#/$defs/buffer"
case FieldTypeCache:
spec["$ref"] = "#/$defs/cache"
case FieldTypeCondition:
return true
case FieldTypeProcessor:
spec["$ref"] = "#/$defs/processor"
case FieldTypeRateLimit:
spec["$ref"] = "#/$defs/rate_limit"
case FieldTypeOutput:
spec["$ref"] = "#/$defs/output"
case FieldTypeMetrics:
spec["$ref"] = "#/$defs/metrics"
case FieldTypeTracer:
spec["$ref"] = "#/$defs/tracer"
}
}
return spec
}
// JSONSchema serializes a field spec into a JSON schema structure.
func (f FieldSpecs) JSONSchema() map[string]interface{} {
spec := map[string]interface{}{}
for _, field := range f {
spec[field.Name] = field.JSONSchema()
}
return spec
}