Skip to content

Commit 0b28389

Browse files
authored
feat: Allow setting JSON type schema max depth (#1844)
#### Summary #1819 fixed it for the gRPC message, but it seems our backend [can't handle](https://github.com/cloudquery/cloudquery-private/actions/runs/10164552583/job/28110217028#step:13:10) the size of the Stripe `tables.json` (see https://stackoverflow.com/questions/59209884/cloud-run-request-limit). With the current default max depth of 4 it's ~40MB. Setting it to 3 lowers it to ~11MB. ---
1 parent 3a5c90c commit 0b28389

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

transformers/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ func WithPrimaryKeyComponents(fields ...string) StructTransformerOption {
6868
t.pkComponentFields = fields
6969
}
7070
}
71+
72+
// WithMaxJSONTypeSchemaDepth allows to specify the maximum depth of JSON type schema
73+
func WithMaxJSONTypeSchemaDepth(maxDepth int) StructTransformerOption {
74+
return func(t *structTransformer) {
75+
t.maxJSONTypeSchemaDepth = maxDepth
76+
}
77+
}

transformers/struct.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/thoas/go-funk"
1515
)
1616

17-
const maxJSONTypeSchemaDepth = 4
17+
const DefaultMaxJSONTypeSchemaDepth = 4
1818

1919
type structTransformer struct {
2020
table *schema.Table
@@ -29,6 +29,8 @@ type structTransformer struct {
2929
pkFieldsFound []string
3030
pkComponentFields []string
3131
pkComponentFieldsFound []string
32+
33+
maxJSONTypeSchemaDepth int
3234
}
3335

3436
func isFieldStruct(reflectType reflect.Type) bool {
@@ -196,6 +198,7 @@ func TransformWithStruct(st any, opts ...StructTransformerOption) schema.Transfo
196198
typeTransformer: DefaultTypeTransformer,
197199
resolverTransformer: DefaultResolverTransformer,
198200
ignoreInTestsTransformer: DefaultIgnoreInTestsTransformer,
201+
maxJSONTypeSchemaDepth: DefaultMaxJSONTypeSchemaDepth,
199202
}
200203
for _, opt := range opts {
201204
opt(t)
@@ -294,7 +297,7 @@ func (t *structTransformer) fieldToJSONSchema(field reflect.StructField, depth i
294297
continue
295298
}
296299
// Avoid infinite recursion
297-
if columnType == types.ExtensionTypes.JSON && depth < maxJSONTypeSchemaDepth {
300+
if columnType == types.ExtensionTypes.JSON && depth < t.maxJSONTypeSchemaDepth {
298301
fieldsMap[name] = t.fieldToJSONSchema(structField, depth+1)
299302
continue
300303
}

transformers/struct_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ func TestJSONTypeSchema(t *testing.T) {
486486
tests := []struct {
487487
name string
488488
testStruct any
489+
maxDepth int
489490
want map[string]string
490491
}{
491492
{
@@ -625,6 +626,30 @@ func TestJSONTypeSchema(t *testing.T) {
625626
"level0": `{"level1":{"level2":{"level3":{"level4":{"level5":"json"}}}}}`,
626627
},
627628
},
629+
{
630+
name: "stops at the configured depth",
631+
maxDepth: 2,
632+
testStruct: struct {
633+
Level0 struct {
634+
Level1 struct {
635+
Level2 struct {
636+
Level3 struct {
637+
Level4 struct {
638+
Level5 struct {
639+
Level6 struct {
640+
Name string `json:"name"`
641+
} `json:"level6"`
642+
} `json:"level5"`
643+
} `json:"level4"`
644+
} `json:"level3"`
645+
} `json:"level2"`
646+
} `json:"level1"`
647+
} `json:"level0"`
648+
}{},
649+
want: map[string]string{
650+
"level0": `{"level1":{"level2":{"level3":"json"}}}`,
651+
},
652+
},
628653
{
629654
name: "ignores non exported and ignored types",
630655
testStruct: struct {
@@ -648,7 +673,11 @@ func TestJSONTypeSchema(t *testing.T) {
648673
table := schema.Table{
649674
Name: "test",
650675
}
651-
transformer := TransformWithStruct(tt.testStruct)
676+
opts := []StructTransformerOption{}
677+
if tt.maxDepth > 0 {
678+
opts = append(opts, WithMaxJSONTypeSchemaDepth(tt.maxDepth))
679+
}
680+
transformer := TransformWithStruct(tt.testStruct, opts...)
652681
err := transformer(&table)
653682
if err != nil {
654683
t.Fatal(err)

0 commit comments

Comments
 (0)