-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixedobject_test.go
227 lines (172 loc) · 4.8 KB
/
fixedobject_test.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package schemer
import (
"bytes"
"fmt"
"log"
"reflect"
"testing"
)
func TestDecodeFixedObject1(t *testing.T) {
type TestStruct struct {
A string
}
var testStruct TestStruct
// setup an example schema
s, err := SchemaOf(testStruct)
if err != nil {
t.Error(err)
return
}
fixedObjectSchema, ok := s.(*FixedObjectSchema)
if !ok {
t.Error("FixedObjectSchema expected")
return
}
// encode it
b, err := fixedObjectSchema.MarshalSchemer()
if err != nil {
t.Error(err)
}
tmp, err := DecodeSchema(bytes.NewReader(b))
if err != nil {
t.Error("cannot encode binary encoded FixedObjectSchema")
}
decodedSchema := tmp.(*FixedObjectSchema)
// and then check the actual contents of the decoded schema
// to make sure it contains the correct values
if decodedSchema.Nullable() != fixedObjectSchema.Nullable() {
t.Error("unexpected values when decoding binary FixedObjectSchema")
}
}
// TestDecodeFixedObject2 tests encoding a nil
func TestDecodeFixedObject2(t *testing.T) {
type StructToEncode struct {
XX *int `schemer:""`
}
var structToEncode *StructToEncode = &(StructToEncode{})
var buf bytes.Buffer
var err error
buf.Reset()
s, err := SchemaOf(&structToEncode)
if err != nil {
t.Error(err)
}
fixedObjectSchema, ok := s.(*FixedObjectSchema)
if !ok {
t.Error("FixedObjectSchema expected")
return
}
// test overririding the nullability of the string...
//fixedObjectSchema.(*FixedObjectSchema).Fields[0].Schema.(*VarLenStringSchema).IsNullable = false
err = fixedObjectSchema.Encode(&buf, structToEncode)
if err != nil {
t.Error(err)
}
r := bytes.NewReader(buf.Bytes())
type StructToDecode struct {
YY *int `schemer:"[XX]"`
}
var structToDecode = StructToDecode{}
fmt.Println(structToEncode)
json, _ := fixedObjectSchema.MarshalJSON()
fmt.Println(string(json))
fmt.Println(buf.Bytes())
err = fixedObjectSchema.Decode(r, &structToDecode)
if err != nil {
t.Error(err)
}
if structToDecode.YY == nil {
fmt.Println("YY decoded as null value...")
} else {
fmt.Println(*structToDecode.YY)
}
}
// TestDecodeFixedObject5 tests our ability to decode objects to other objects, using struct tags....
func TestDecodeFixedObject5(t *testing.T) {
type SourceStruct struct {
FName string `schemer:"[A,B,C,FirstName]"`
LName string `schemer:"[D,E,F,LastName]"`
AgeInLife int `schemer:"Age"`
}
var structToEncode = SourceStruct{FName: "ben", LName: "pritchard", AgeInLife: 42}
tmp, err := SchemaOf(&structToEncode)
if err != nil {
t.Error(err)
}
writerSchema, ok := tmp.(*FixedObjectSchema)
if !ok {
t.Error("FixedObjectSchema expected")
return
}
var encodedData bytes.Buffer
err = writerSchema.Encode(&encodedData, structToEncode)
if err != nil {
t.Error(err)
return
}
// write out our schema as JSON
binarywriterSchema, err := writerSchema.MarshalJSON()
if err != nil {
t.Error("cannot create ", err)
}
// recreate the schmer from the JSON
readerSchema, err := DecodeSchemaJSON(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw JSON data", err)
return
}
type DestinationStruct struct {
FirstName string //`schemer:"FName"`
LastName string //`schemer:"LName"`
Age int //`schemer:"AgeInLife"`
}
var structToDecode = DestinationStruct{}
r := bytes.NewReader(encodedData.Bytes())
err = readerSchema.Decode(r, &structToDecode)
if err != nil {
t.Error(err)
return
}
// and now make sure that the structs match!
decodeOK := true
decodeOK = (structToDecode.FirstName == structToEncode.FName)
decodeOK = decodeOK && (structToDecode.LastName == structToEncode.LName)
decodeOK = decodeOK && (structToDecode.Age == structToEncode.AgeInLife)
log.Print(structToDecode)
if !decodeOK {
t.Error("unexpected struct to struct decode")
}
}
// TestDecodeFixedObject6 tests our ability to decode to an emptry interface
func TestDecodeFixedObject6(t *testing.T) {
type SourceStruct struct {
FName string
LName string
AgeInLife int
}
var structToEncode = SourceStruct{FName: "ben", LName: "pritchard", AgeInLife: 42}
writerSchema, err := SchemaOf(&structToEncode)
if err != nil {
t.Error(err)
}
var encodedData bytes.Buffer
err = writerSchema.Encode(&encodedData, structToEncode)
if err != nil {
t.Error(err)
}
var decodeDestination interface{}
r := bytes.NewReader(encodedData.Bytes())
err = writerSchema.Decode(r, &decodeDestination)
if err != nil {
t.Error(err)
}
v := reflect.ValueOf(decodeDestination).Elem()
// and now make sure that the structs match!
decodeOK := true
decodeOK = (v.Field(0).String() == structToEncode.FName)
decodeOK = decodeOK && (v.Field(1).String() == structToEncode.LName)
decodeOK = decodeOK && (v.Field(2).Int() == int64(structToEncode.AgeInLife))
if !decodeOK {
t.Error("unexpected struct to struct decode")
}
}