-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschema.go
110 lines (104 loc) · 3.18 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
package filetypes
import (
"reflect"
cqjsonschema "github.com/cloudquery/codegen/jsonschema"
"github.com/cloudquery/filetypes/v4/csv"
jsonfile "github.com/cloudquery/filetypes/v4/json"
"github.com/cloudquery/filetypes/v4/parquet"
"github.com/invopop/jsonschema"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
// JSONSchemaOptions should be used when generating schema to add the nested spec info
func (FileSpec) JSONSchemaOptions() []cqjsonschema.Option {
fileSpecType := reflect.TypeOf(FileSpec{})
return []cqjsonschema.Option{func(r *jsonschema.Reflector) {
fileSpecFields := func(t reflect.Type) []reflect.StructField {
if t != fileSpecType {
return nil
}
return reflect.VisibleFields(reflect.TypeOf(struct {
CSVSpec csv.CSVSpec
JSONSpec jsonfile.JSONSpec
ParquetSpec parquet.ParquetSpec
}{}))
}
if r.AdditionalFields == nil {
r.AdditionalFields = fileSpecFields
} else {
old := r.AdditionalFields
r.AdditionalFields = func(r reflect.Type) []reflect.StructField {
if extra := fileSpecFields(r); len(extra) > 0 {
return extra
}
return old(r)
}
}
}}
}
func (FileSpec) JSONSchemaExtend(sc *jsonschema.Schema) {
// now we need to remove extra fields
refCSVSpec := sc.Properties.Value("CSVSpec").Ref
refJSONSpec := sc.Properties.Value("JSONSpec").Ref
refParquetSpec := sc.Properties.Value("ParquetSpec").Ref
sc.Properties.Delete("CSVSpec")
sc.Properties.Delete("JSONSpec")
sc.Properties.Delete("ParquetSpec")
sc.Properties.Set("format_spec", &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
AnyOf: []*jsonschema.Schema{
{Ref: refCSVSpec},
{Ref: refJSONSpec},
{Ref: refParquetSpec},
},
},
{Type: "null"},
},
})
// now we need to enforce format -> specific type
formatSpecOneOf := []*jsonschema.Schema{
// CSV
{
Properties: func() *orderedmap.OrderedMap[string, *jsonschema.Schema] {
properties := jsonschema.NewProperties()
properties.Set("format", &jsonschema.Schema{Type: "string", Const: FormatTypeCSV})
properties.Set("format_spec", &jsonschema.Schema{
OneOf: []*jsonschema.Schema{{Ref: refCSVSpec}, {Type: "null"}},
})
return properties
}(),
},
// JSON
{
Properties: func() *orderedmap.OrderedMap[string, *jsonschema.Schema] {
properties := jsonschema.NewProperties()
properties.Set("format", &jsonschema.Schema{Type: "string", Const: FormatTypeJSON})
properties.Set("format_spec", &jsonschema.Schema{
OneOf: []*jsonschema.Schema{{Ref: refJSONSpec}, {Type: "null"}},
})
return properties
}(),
},
// Parquet
{
Properties: func() *orderedmap.OrderedMap[string, *jsonschema.Schema] {
properties := jsonschema.NewProperties()
properties.Set("format", &jsonschema.Schema{Type: "string", Const: FormatTypeParquet})
properties.Set("format_spec", &jsonschema.Schema{
OneOf: []*jsonschema.Schema{{Ref: refParquetSpec}, {Type: "null"}},
})
return properties
}(),
},
}
if sc.OneOf == nil {
sc.OneOf = formatSpecOneOf
} else {
// may happen when embedding, so move to all_of{{one_of},{one_of}}
sc.AllOf = []*jsonschema.Schema{
{OneOf: sc.OneOf},
{OneOf: formatSpecOneOf},
}
sc.OneOf = nil
}
}