1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
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)
})
}
}
|