2024-04-29 03:23:54 +08:00
|
|
|
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
|
|
|
|
}{},
|
|
|
|
},
|
2024-04-29 04:00:52 +08:00
|
|
|
want: RecordTypeBegin().
|
2024-05-02 23:52:10 +08:00
|
|
|
WithField("Field1", TypeInt32).
|
2024-05-02 23:32:15 +08:00
|
|
|
WithField("Field2", TypeString).
|
2024-04-29 04:00:52 +08:00
|
|
|
RecordTypeEnd(),
|
2024-04-29 03:23:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple list",
|
|
|
|
args: args{
|
|
|
|
instance: struct {
|
|
|
|
Field1 []int
|
|
|
|
Field2 string
|
|
|
|
}{},
|
|
|
|
},
|
2024-04-29 04:00:52 +08:00
|
|
|
want: RecordTypeBegin().
|
2024-05-02 23:52:10 +08:00
|
|
|
WithField("Field1", ListOf(TypeInt32)).
|
2024-05-02 23:32:15 +08:00
|
|
|
WithField("Field2", TypeString).
|
2024-04-29 04:00:52 +08:00
|
|
|
RecordTypeEnd(),
|
2024-04-29 03:23:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple []byte",
|
|
|
|
args: args{
|
|
|
|
instance: struct {
|
|
|
|
Field2 []byte
|
|
|
|
}{},
|
|
|
|
},
|
2024-04-29 04:00:52 +08:00
|
|
|
want: RecordTypeBegin().
|
2024-05-02 23:32:15 +08:00
|
|
|
WithField("Field2", TypeBytes).
|
2024-04-29 04:00:52 +08:00
|
|
|
RecordTypeEnd(),
|
2024-04-29 03:23:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nested simpe structs",
|
|
|
|
args: args{
|
|
|
|
instance: struct {
|
|
|
|
Field1 int
|
|
|
|
Field2 struct {
|
|
|
|
Field3 string
|
|
|
|
Field4 int
|
|
|
|
}
|
|
|
|
}{},
|
|
|
|
},
|
2024-04-29 04:00:52 +08:00
|
|
|
want: RecordTypeBegin().
|
2024-05-02 23:52:10 +08:00
|
|
|
WithField("Field1", TypeInt32).
|
2024-05-02 23:32:15 +08:00
|
|
|
WithRecordField("Field2",
|
2024-04-29 04:00:52 +08:00
|
|
|
RecordTypeBegin().
|
2024-05-02 23:32:15 +08:00
|
|
|
WithField("Field3", TypeString).
|
2024-05-02 23:52:10 +08:00
|
|
|
WithField("Field4", TypeInt32).
|
2024-05-21 02:03:56 +08:00
|
|
|
RecordTypeEnd(),
|
2024-04-29 03:23:54 +08:00
|
|
|
).
|
2024-04-29 04:00:52 +08:00
|
|
|
RecordTypeEnd(),
|
2024-04-29 03:23:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nested struct type",
|
|
|
|
args: args{
|
|
|
|
instance: struct {
|
|
|
|
Field1 int
|
|
|
|
Field2 struct {
|
|
|
|
Field3 string
|
|
|
|
Field4 []int
|
|
|
|
Field5 struct {
|
|
|
|
Field6 string
|
|
|
|
Field7 []byte
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}{},
|
|
|
|
},
|
2024-04-29 04:00:52 +08:00
|
|
|
want: RecordTypeBegin().
|
2024-05-02 23:52:10 +08:00
|
|
|
WithField("Field1", TypeInt32).
|
2024-05-02 23:32:15 +08:00
|
|
|
WithRecordField("Field2", RecordTypeBegin().
|
|
|
|
WithField("Field3", TypeString).
|
2024-05-02 23:52:10 +08:00
|
|
|
WithField("Field4", ListOf(TypeInt32)).
|
2024-05-02 23:32:15 +08:00
|
|
|
WithRecordField("Field5",
|
2024-04-29 04:00:52 +08:00
|
|
|
RecordTypeBegin().
|
2024-05-02 23:32:15 +08:00
|
|
|
WithField("Field6", TypeString).
|
|
|
|
WithField("Field7", TypeBytes).
|
2024-05-21 02:03:56 +08:00
|
|
|
RecordTypeEnd(),
|
2024-04-29 04:00:52 +08:00
|
|
|
).RecordTypeEnd(),
|
2024-04-29 03:23:54 +08:00
|
|
|
).
|
2024-04-29 04:00:52 +08:00
|
|
|
RecordTypeEnd(),
|
2024-04-29 03:23:54 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|