@@ -2,6 +2,7 @@ package transformers
22
33import (
44 "net"
5+ "reflect"
56 "testing"
67 "time"
78
@@ -74,6 +75,11 @@ type (
7475 AFunnyLookingField string `json:"OS-EXT:a-funny-looking-field"`
7576 AFieldWithCamelCaseName string `json:"camelCaseName"`
7677 }
78+
79+ testStructWithAny struct {
80+ IntCol int `json:"int_col"`
81+ Properties any
82+ }
7783)
7884
7985var (
@@ -269,6 +275,30 @@ var (
269275 },
270276 },
271277 }
278+
279+ expectedTestTableStructWithAny = schema.Table {
280+ Name : "test_embedded_struct_with_any" ,
281+ Columns : schema.ColumnList {
282+ {
283+ Name : "int_col" ,
284+ Type : arrow .PrimitiveTypes .Int64 ,
285+ },
286+ },
287+ }
288+
289+ expectedTestTableStructWithCustomAny = schema.Table {
290+ Name : "test_embedded_struct_with_custom_any" ,
291+ Columns : schema.ColumnList {
292+ {
293+ Name : "int_col" ,
294+ Type : arrow .PrimitiveTypes .Int64 ,
295+ },
296+ {
297+ Name : "properties" ,
298+ Type : types .ExtensionTypes .JSON ,
299+ },
300+ },
301+ }
272302)
273303
274304func TestTableFromGoStruct (t * testing.T ) {
@@ -389,10 +419,34 @@ func TestTableFromGoStruct(t *testing.T) {
389419 },
390420 want : expectedFunnyTable ,
391421 },
422+ {
423+ name : "Ignore any-type fields by default" ,
424+ args : args {
425+ testStruct : testStructWithAny {},
426+ },
427+ want : expectedTestTableStructWithAny ,
428+ },
429+ {
430+ name : "Should be able to override any-type fields with a type" ,
431+ args : args {
432+ testStruct : testStructWithAny {},
433+ options : []StructTransformerOption {
434+ WithTypeTransformer (func (f reflect.StructField ) (arrow.DataType , error ) {
435+ if f .Type .Kind () == reflect .Interface {
436+ return types .ExtensionTypes .JSON , nil
437+ }
438+ return nil , nil
439+ }),
440+ },
441+ },
442+ want : expectedTestTableStructWithCustomAny ,
443+ },
392444 }
393445
394446 for _ , tt := range tests {
447+ tt := tt
395448 t .Run (tt .name , func (t * testing.T ) {
449+ t .Parallel ()
396450 table := schema.Table {
397451 Name : "test" ,
398452 Columns : schema.ColumnList {},
@@ -405,11 +459,21 @@ func TestTableFromGoStruct(t *testing.T) {
405459 }
406460 t .Fatal (err )
407461 }
462+ if tt .wantErr {
463+ t .Fatal ("expected error, got none" )
464+ }
465+
408466 for i , col := range table .Columns {
409467 if ! arrow .TypeEqual (col .Type , tt .want .Columns [i ].Type ) {
410- t .Fatalf ("column %s does not match expected type. got %v, want %v" , col .Name , col .Type , tt .want .Columns [i ].Type )
468+ t .Fatalf ("column %q does not match expected type. got %v, want %v" , col .Name , col .Type , tt .want .Columns [i ].Type )
469+ }
470+ }
471+ for _ , exc := range tt .want .Columns {
472+ if c := table .Column (exc .Name ); c == nil {
473+ t .Fatalf ("column %q not found. want: %v" , exc .Name , exc .Type )
411474 }
412475 }
476+
413477 if diff := cmp .Diff (table .PrimaryKeys (), tt .want .PrimaryKeys ()); diff != "" {
414478 t .Fatalf ("table does not match expected. diff (-got, +want): %v" , diff )
415479 }
0 commit comments