Skip to content

Commit

Permalink
feat: Allow setting JSON type schema max depth (#1844)
Browse files Browse the repository at this point in the history
#### 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.

---
  • Loading branch information
erezrokah authored Aug 1, 2024
1 parent 3a5c90c commit 0b28389
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions transformers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ func WithPrimaryKeyComponents(fields ...string) StructTransformerOption {
t.pkComponentFields = fields
}
}

// WithMaxJSONTypeSchemaDepth allows to specify the maximum depth of JSON type schema
func WithMaxJSONTypeSchemaDepth(maxDepth int) StructTransformerOption {
return func(t *structTransformer) {
t.maxJSONTypeSchemaDepth = maxDepth
}
}
7 changes: 5 additions & 2 deletions transformers/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/thoas/go-funk"
)

const maxJSONTypeSchemaDepth = 4
const DefaultMaxJSONTypeSchemaDepth = 4

type structTransformer struct {
table *schema.Table
Expand All @@ -29,6 +29,8 @@ type structTransformer struct {
pkFieldsFound []string
pkComponentFields []string
pkComponentFieldsFound []string

maxJSONTypeSchemaDepth int
}

func isFieldStruct(reflectType reflect.Type) bool {
Expand Down Expand Up @@ -196,6 +198,7 @@ func TransformWithStruct(st any, opts ...StructTransformerOption) schema.Transfo
typeTransformer: DefaultTypeTransformer,
resolverTransformer: DefaultResolverTransformer,
ignoreInTestsTransformer: DefaultIgnoreInTestsTransformer,
maxJSONTypeSchemaDepth: DefaultMaxJSONTypeSchemaDepth,
}
for _, opt := range opts {
opt(t)
Expand Down Expand Up @@ -294,7 +297,7 @@ func (t *structTransformer) fieldToJSONSchema(field reflect.StructField, depth i
continue
}
// Avoid infinite recursion
if columnType == types.ExtensionTypes.JSON && depth < maxJSONTypeSchemaDepth {
if columnType == types.ExtensionTypes.JSON && depth < t.maxJSONTypeSchemaDepth {
fieldsMap[name] = t.fieldToJSONSchema(structField, depth+1)
continue
}
Expand Down
31 changes: 30 additions & 1 deletion transformers/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ func TestJSONTypeSchema(t *testing.T) {
tests := []struct {
name string
testStruct any
maxDepth int
want map[string]string
}{
{
Expand Down Expand Up @@ -625,6 +626,30 @@ func TestJSONTypeSchema(t *testing.T) {
"level0": `{"level1":{"level2":{"level3":{"level4":{"level5":"json"}}}}}`,
},
},
{
name: "stops at the configured depth",
maxDepth: 2,
testStruct: struct {
Level0 struct {
Level1 struct {
Level2 struct {
Level3 struct {
Level4 struct {
Level5 struct {
Level6 struct {
Name string `json:"name"`
} `json:"level6"`
} `json:"level5"`
} `json:"level4"`
} `json:"level3"`
} `json:"level2"`
} `json:"level1"`
} `json:"level0"`
}{},
want: map[string]string{
"level0": `{"level1":{"level2":{"level3":"json"}}}`,
},
},
{
name: "ignores non exported and ignored types",
testStruct: struct {
Expand All @@ -648,7 +673,11 @@ func TestJSONTypeSchema(t *testing.T) {
table := schema.Table{
Name: "test",
}
transformer := TransformWithStruct(tt.testStruct)
opts := []StructTransformerOption{}
if tt.maxDepth > 0 {
opts = append(opts, WithMaxJSONTypeSchemaDepth(tt.maxDepth))
}
transformer := TransformWithStruct(tt.testStruct, opts...)
err := transformer(&table)
if err != nil {
t.Fatal(err)
Expand Down

2 comments on commit 0b28389

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱️ Benchmark results

  • Glob-8 ns/op: 91.59

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱️ Benchmark results

  • Glob-8 ns/op: 91.59

Please sign in to comment.