-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathstruct.go
More file actions
364 lines (328 loc) · 9.64 KB
/
struct.go
File metadata and controls
364 lines (328 loc) · 9.64 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package transformers
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"slices"
"strings"
"github.com/apache/arrow-go/v18/arrow"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/cloudquery/plugin-sdk/v4/types"
"github.com/thoas/go-funk"
)
const DefaultMaxJSONTypeSchemaDepth = 4
type structTransformer struct {
table *schema.Table
skipFields []string
nameTransformer NameTransformer
typeTransformer TypeTransformer
resolverTransformer ResolverTransformer
ignoreInTestsTransformer IgnoreInTestsTransformer
nullableFieldTransformer NullableFieldTransformer
unwrapAllEmbeddedStructFields bool
structFieldsToUnwrap []string
pkFields []string
pkFieldsFound []string
pkComponentFields []string
pkComponentFieldsFound []string
skipPKValidationFields []string
skipPKValidationFieldsFound []string
jsonSchemaNameTransformer NameTransformer
maxJSONTypeSchemaDepth int
}
func isFieldStruct(reflectType reflect.Type) bool {
switch reflectType.Kind() {
case reflect.Struct:
return true
case reflect.Ptr:
return reflectType.Elem().Kind() == reflect.Struct
default:
return false
}
}
func isTypeIgnored(t reflect.Type) bool {
switch t.Kind() {
case reflect.Func,
reflect.Chan,
reflect.UnsafePointer:
return true
default:
return false
}
}
func (t *structTransformer) getUnwrappedFields(field reflect.StructField) []reflect.StructField {
reflectType := field.Type
if reflectType.Kind() == reflect.Ptr {
reflectType = reflectType.Elem()
}
fields := make([]reflect.StructField, 0)
for i := 0; i < reflectType.NumField(); i++ {
sf := reflectType.Field(i)
if t.ignoreField(sf) {
continue
}
fields = append(fields, sf)
}
return fields
}
func (t *structTransformer) unwrapField(field reflect.StructField) error {
unwrappedFields := t.getUnwrappedFields(field)
var parent *reflect.StructField
// For non embedded structs we need to add the parent field name to the path
if !field.Anonymous {
parent = &field
}
for _, f := range unwrappedFields {
if err := t.addColumnFromField(f, parent); err != nil {
return fmt.Errorf("failed to add column from field %s: %w", f.Name, err)
}
}
return nil
}
func (t *structTransformer) shouldUnwrapField(field reflect.StructField) bool {
switch {
case !isFieldStruct(field.Type):
return false
case slices.Contains(t.structFieldsToUnwrap, field.Name):
return true
case !field.Anonymous:
return false
case t.unwrapAllEmbeddedStructFields:
return true
default:
return false
}
}
func (t *structTransformer) ignoreField(field reflect.StructField) bool {
switch {
case len(field.Name) == 0,
slices.Contains(t.skipFields, field.Name),
!field.IsExported(),
isTypeIgnored(field.Type):
return true
default:
return false
}
}
func (t *structTransformer) addColumnFromField(field reflect.StructField, parent *reflect.StructField) error {
if t.ignoreField(field) {
return nil
}
columnType, err := t.getColumnType(field)
if err != nil {
return err
}
if columnType == nil {
return nil // ignored
}
path := field.Name
name, err := t.nameTransformer(field)
if err != nil {
return fmt.Errorf("failed to transform field name for field %s: %w", field.Name, err)
}
// skip field if there is no name
if name == "" {
return nil
}
if parent != nil {
parentName, err := t.nameTransformer(*parent)
if err != nil {
return fmt.Errorf("failed to transform field name for parent field %s: %w", parent.Name, err)
}
name = parentName + "_" + name
path = parent.Name + `.` + path
}
if t.table.Columns.Get(name) != nil {
return nil
}
resolver := t.resolverTransformer(field, path)
if resolver == nil {
resolver = DefaultResolverTransformer(field, path)
}
column := schema.Column{
Name: name,
Type: columnType,
Resolver: resolver,
IgnoreInTests: t.ignoreInTestsTransformer(field),
NotNull: !t.nullableFieldTransformer(field),
}
// Enrich JSON column with detailed schema
if columnType == types.ExtensionTypes.JSON {
column.TypeSchema = structSchemaToJSON(t.fieldToJSONSchema(field, 0))
}
for _, pk := range t.pkFields {
if pk == path {
// use path to allow the following
// 1. Don't duplicate the PK fields if the unwrapped struct contains a fields with the same name
// 2. Allow specifying the nested unwrapped field as part of the PK.
column.PrimaryKey = true
t.pkFieldsFound = append(t.pkFieldsFound, pk)
}
}
for _, pk := range t.pkComponentFields {
if pk == path {
// use path to allow the following
// 1. Don't duplicate the PK fields if the unwrapped struct contains a fields with the same name
// 2. Allow specifying the nested unwrapped field as part of the PK.
column.PrimaryKeyComponent = true
t.pkComponentFieldsFound = append(t.pkComponentFieldsFound, pk)
}
}
for _, skipPK := range t.skipPKValidationFields {
if skipPK == path {
column.SkipPKValidation = true
t.skipPKValidationFieldsFound = append(t.skipPKValidationFieldsFound, skipPK)
}
}
t.table.Columns = append(t.table.Columns, column)
return nil
}
func TransformWithStruct(st any, opts ...StructTransformerOption) schema.Transform {
t := &structTransformer{
nameTransformer: DefaultNameTransformer,
typeTransformer: DefaultTypeTransformer,
resolverTransformer: DefaultResolverTransformer,
ignoreInTestsTransformer: DefaultIgnoreInTestsTransformer,
nullableFieldTransformer: DefaultNullableFieldTransformer,
jsonSchemaNameTransformer: DefaultJSONColumnSchemaNameTransformer,
maxJSONTypeSchemaDepth: DefaultMaxJSONTypeSchemaDepth,
}
for _, opt := range opts {
opt(t)
}
return func(table *schema.Table) error {
t.table = table
e := reflect.ValueOf(st)
if e.Kind() == reflect.Pointer {
e = e.Elem()
}
if e.Kind() == reflect.Slice {
e = reflect.MakeSlice(e.Type(), 1, 1).Index(0)
}
if e.Kind() != reflect.Struct {
return fmt.Errorf("expected struct, got %s", e.Kind())
}
eType := e.Type()
for i := 0; i < e.NumField(); i++ {
field := eType.Field(i)
switch {
case t.shouldUnwrapField(field):
if err := t.unwrapField(field); err != nil {
return err
}
default:
if err := t.addColumnFromField(field, nil); err != nil {
return fmt.Errorf("failed to add column for field %s: %w", field.Name, err)
}
}
}
// Validate that all expected PK fields were found
if diff := funk.SubtractString(t.pkFields, t.pkFieldsFound); len(diff) > 0 {
return fmt.Errorf("failed to create all of the desired primary keys: %v", diff)
}
if diff := funk.SubtractString(t.pkComponentFields, t.pkComponentFieldsFound); len(diff) > 0 {
return fmt.Errorf("failed to find all of the desired primary key components: %v", diff)
}
if diff := funk.SubtractString(t.skipPKValidationFields, t.skipPKValidationFieldsFound); len(diff) > 0 {
return fmt.Errorf("failed to find all of the desired skip primary key validation fields: %v", diff)
}
return nil
}
}
func (t *structTransformer) getColumnType(field reflect.StructField) (arrow.DataType, error) {
columnType, err := t.typeTransformer(field)
if err != nil {
return nil, fmt.Errorf("failed to transform type for field %s: %w", field.Name, err)
}
if columnType == nil {
columnType, err = DefaultTypeTransformer(field)
if err != nil {
return nil, fmt.Errorf("failed to transform type for field %s: %w", field.Name, err)
}
}
return columnType, nil
}
func structSchemaToJSON(s any) string {
b := new(bytes.Buffer)
encoder := json.NewEncoder(b)
encoder.SetEscapeHTML(false)
_ = encoder.Encode(s)
return strings.TrimSpace(b.String())
}
func normalizePointer(field reflect.StructField) reflect.Value {
if field.Type.Kind() == reflect.Ptr {
return reflect.New(field.Type.Elem())
}
return reflect.New(field.Type)
}
func (t *structTransformer) fieldToJSONSchema(field reflect.StructField, depth int) any {
normalizedField := normalizePointer(field)
switch normalizedField.Elem().Kind() {
case reflect.Struct:
fieldsMap := make(map[string]any)
fieldType := normalizedField.Elem().Type()
for i := 0; i < fieldType.NumField(); i++ {
structField := fieldType.Field(i)
if !structField.IsExported() || isTypeIgnored(structField.Type) {
continue
}
name, err := t.jsonSchemaNameTransformer(structField)
if err != nil {
continue
}
columnType, err := t.getColumnType(structField)
if err != nil {
continue
}
if columnType == nil {
fieldsMap[name] = "any"
continue
}
// Avoid infinite recursion
if columnType == types.ExtensionTypes.JSON && depth < t.maxJSONTypeSchemaDepth {
fieldsMap[name] = t.fieldToJSONSchema(structField, depth+1)
continue
}
asList, ok := columnType.(*arrow.ListType)
if ok {
fieldsMap[name] = []any{asList.Elem().String()}
continue
}
fieldsMap[name] = columnType.String()
}
return fieldsMap
case reflect.Map:
keySchema, ok := t.fieldToJSONSchema(reflect.StructField{
Type: normalizedField.Elem().Type().Key(),
}, depth+1).(string)
if keySchema == "" || !ok {
return ""
}
valueSchema := t.fieldToJSONSchema(reflect.StructField{
Type: normalizedField.Elem().Type().Elem(),
}, depth+1)
if valueSchema == "" {
return ""
}
return map[string]any{
keySchema: valueSchema,
}
case reflect.Slice:
valueSchema := t.fieldToJSONSchema(reflect.StructField{
Type: normalizedField.Elem().Type().Elem(),
}, depth+1)
if valueSchema == "" {
return ""
}
return []any{valueSchema}
}
columnType, err := t.getColumnType(field)
if err != nil {
return ""
}
if columnType == nil {
return "any"
}
return columnType.String()
}