chrislu
8 months ago
3 changed files with 152 additions and 20 deletions
-
20weed/mq/schema/schema_builder.go
-
37weed/mq/schema/struct_to_schema.go
-
115weed/mq/schema/struct_to_schema_test.go
@ -0,0 +1,115 @@ |
|||||
|
package schema |
||||
|
|
||||
|
import ( |
||||
|
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
func TestStructToSchema(t *testing.T) { |
||||
|
type args struct { |
||||
|
instance any |
||||
|
} |
||||
|
tests := []struct { |
||||
|
name string |
||||
|
args args |
||||
|
want *schema_pb.RecordType |
||||
|
}{ |
||||
|
{ |
||||
|
name: "scalar type", |
||||
|
args: args{ |
||||
|
instance: 1, |
||||
|
}, |
||||
|
want: nil, |
||||
|
}, |
||||
|
{ |
||||
|
name: "simple struct type", |
||||
|
args: args{ |
||||
|
instance: struct { |
||||
|
Field1 int |
||||
|
Field2 string |
||||
|
}{}, |
||||
|
}, |
||||
|
want: NewRecordTypeBuilder(). |
||||
|
SetField("Field1", TypeInteger). |
||||
|
SetField("Field2", TypeString). |
||||
|
Build(), |
||||
|
}, |
||||
|
{ |
||||
|
name: "simple list", |
||||
|
args: args{ |
||||
|
instance: struct { |
||||
|
Field1 []int |
||||
|
Field2 string |
||||
|
}{}, |
||||
|
}, |
||||
|
want: NewRecordTypeBuilder(). |
||||
|
SetField("Field1", List(TypeInteger)). |
||||
|
SetField("Field2", TypeString). |
||||
|
Build(), |
||||
|
}, |
||||
|
{ |
||||
|
name: "simple []byte", |
||||
|
args: args{ |
||||
|
instance: struct { |
||||
|
Field2 []byte |
||||
|
}{}, |
||||
|
}, |
||||
|
want: NewRecordTypeBuilder(). |
||||
|
SetField("Field2", TypeBytes). |
||||
|
Build(), |
||||
|
}, |
||||
|
{ |
||||
|
name: "nested simpe structs", |
||||
|
args: args{ |
||||
|
instance: struct { |
||||
|
Field1 int |
||||
|
Field2 struct { |
||||
|
Field3 string |
||||
|
Field4 int |
||||
|
} |
||||
|
}{}, |
||||
|
}, |
||||
|
want: NewRecordTypeBuilder(). |
||||
|
SetField("Field1", TypeInteger). |
||||
|
SetRecordField("Field2", NewRecordTypeBuilder(). |
||||
|
SetField("Field3", TypeString). |
||||
|
SetField("Field4", TypeInteger), |
||||
|
). |
||||
|
Build(), |
||||
|
}, |
||||
|
{ |
||||
|
name: "nested struct type", |
||||
|
args: args{ |
||||
|
instance: struct { |
||||
|
Field1 int |
||||
|
Field2 struct { |
||||
|
Field3 string |
||||
|
Field4 []int |
||||
|
Field5 struct { |
||||
|
Field6 string |
||||
|
Field7 []byte |
||||
|
} |
||||
|
} |
||||
|
}{}, |
||||
|
}, |
||||
|
want: NewRecordTypeBuilder(). |
||||
|
SetField("Field1", TypeInteger). |
||||
|
SetRecordField("Field2", NewRecordTypeBuilder(). |
||||
|
SetField("Field3", TypeString). |
||||
|
SetField("Field4", List(TypeInteger)). |
||||
|
SetRecordField("Field5", NewRecordTypeBuilder(). |
||||
|
SetField("Field6", TypeString). |
||||
|
SetField("Field7", TypeBytes), |
||||
|
), |
||||
|
). |
||||
|
Build(), |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
for _, tt := range tests { |
||||
|
t.Run(tt.name, func(t *testing.T) { |
||||
|
assert.Equalf(t, tt.want, StructToSchema(tt.args.instance), "StructToSchema(%v)", tt.args.instance) |
||||
|
}) |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue