From 3d025a20656cd79517e1466f1d9f684ee4b000a6 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Wed, 18 Dec 2024 21:00:40 +0000 Subject: [PATCH] fix: Use field name in json type schema if json tag is missing --- transformers/name.go | 12 ++++++++++++ transformers/struct.go | 2 +- transformers/struct_test.go | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/transformers/name.go b/transformers/name.go index 93faad20da..2144b99319 100644 --- a/transformers/name.go +++ b/transformers/name.go @@ -26,4 +26,16 @@ func DefaultNameTransformer(field reflect.StructField) (string, error) { return defaultCaser.ToSnake(name), nil } +func JsonTagOrFieldName(field reflect.StructField) (string, error) { + name := field.Name + if jsonTag := strings.Split(field.Tag.Get("json"), ",")[0]; len(jsonTag) > 0 { + // return empty string if the field is not related api response + if jsonTag == "-" { + return "", nil + } + return jsonTag, nil + } + return name, nil +} + var _ NameTransformer = DefaultNameTransformer diff --git a/transformers/struct.go b/transformers/struct.go index a85d22aebf..3e33989d47 100644 --- a/transformers/struct.go +++ b/transformers/struct.go @@ -284,7 +284,7 @@ func (t *structTransformer) fieldToJSONSchema(field reflect.StructField, depth i if !structField.IsExported() || isTypeIgnored(structField.Type) { continue } - name, err := t.nameTransformer(structField) + name, err := JsonTagOrFieldName(structField) if err != nil { continue } diff --git a/transformers/struct_test.go b/transformers/struct_test.go index 81234323ca..281ce34eff 100644 --- a/transformers/struct_test.go +++ b/transformers/struct_test.go @@ -12,6 +12,7 @@ import ( "github.com/cloudquery/plugin-sdk/v4/schema" "github.com/cloudquery/plugin-sdk/v4/types" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/require" ) type ( @@ -665,6 +666,23 @@ func TestJSONTypeSchema(t *testing.T) { "item": `{"exported":"utf8"}`, }, }, + { + name: "no json tags", + testStruct: struct { + Tags map[string]string + Item struct { + Name string + Tags map[string]string + FlatItems []string + ComplexItems []struct { + Name string + } + } + }{}, + want: map[string]string{ + "item": `{"ComplexItems":[{"Name":"utf8"}],"FlatItems":["utf8"],"Name":"utf8","Tags":{"utf8":"utf8"}}`, + }, + }, } for _, tt := range tests { @@ -684,6 +702,7 @@ func TestJSONTypeSchema(t *testing.T) { } for col, schema := range tt.want { column := table.Column(col) + require.NotNil(t, column, "column %q not found", col) if diff := cmp.Diff(column.TypeSchema, schema); diff != "" { t.Fatalf("table does not match expected. diff (-got, +want): %v", diff) }