123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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: RecordTypeBegin().
- WithField("Field1", TypeInt32).
- WithField("Field2", TypeString).
- RecordTypeEnd(),
- },
- {
- name: "simple list",
- args: args{
- instance: struct {
- Field1 []int
- Field2 string
- }{},
- },
- want: RecordTypeBegin().
- WithField("Field1", ListOf(TypeInt32)).
- WithField("Field2", TypeString).
- RecordTypeEnd(),
- },
- {
- name: "simple []byte",
- args: args{
- instance: struct {
- Field2 []byte
- }{},
- },
- want: RecordTypeBegin().
- WithField("Field2", TypeBytes).
- RecordTypeEnd(),
- },
- {
- name: "nested simpe structs",
- args: args{
- instance: struct {
- Field1 int
- Field2 struct {
- Field3 string
- Field4 int
- }
- }{},
- },
- want: RecordTypeBegin().
- WithField("Field1", TypeInt32).
- WithRecordField("Field2",
- RecordTypeBegin().
- WithField("Field3", TypeString).
- WithField("Field4", TypeInt32).
- RecordTypeEnd(),
- ).
- RecordTypeEnd(),
- },
- {
- name: "nested struct type",
- args: args{
- instance: struct {
- Field1 int
- Field2 struct {
- Field3 string
- Field4 []int
- Field5 struct {
- Field6 string
- Field7 []byte
- }
- }
- }{},
- },
- want: RecordTypeBegin().
- WithField("Field1", TypeInt32).
- WithRecordField("Field2", RecordTypeBegin().
- WithField("Field3", TypeString).
- WithField("Field4", ListOf(TypeInt32)).
- WithRecordField("Field5",
- RecordTypeBegin().
- WithField("Field6", TypeString).
- WithField("Field7", TypeBytes).
- RecordTypeEnd(),
- ).RecordTypeEnd(),
- ).
- RecordTypeEnd(),
- },
- }
- 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)
- })
- }
- }
|