-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_upgrade_test.go
305 lines (272 loc) · 7.54 KB
/
writer_upgrade_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (C) 2023 by Posit Software, PBC
package rsf
import (
"bufio"
"bytes"
"io"
"testing"
"github.com/stretchr/testify/suite"
)
type WriterUpgradeSuite struct {
suite.Suite
}
func TestWriterUpgradeSuite(t *testing.T) {
suite.Run(t, &WriterUpgradeSuite{})
}
func (s *WriterUpgradeSuite) TestWriteObjectAndUpgrade() {
// Create data with a legacy struct.
type legacySnap struct {
Date string `rsf:"date,skip,fixed:10"`
Name string `rsf:"name"`
Verified bool `rsf:"verified"`
Skip string `rsf:"-"`
}
a := struct {
Skip string `rsf:"-"`
Company string `rsf:"company"`
Ready bool `rsf:"ready"`
List []legacySnap `rsf:"list,index:date"`
Age int `rsf:"age"`
Rating float64 `rsf:"rating"`
}{
Company: "posit",
Ready: true,
Age: 55,
Rating: 92.689,
List: []legacySnap{
{
Date: "2020-10-01",
Name: "From 2020",
Verified: false,
},
{
Date: "2021-03-21",
Name: "From 2021",
Verified: true,
},
{
Date: "2022-12-15",
Name: "this is from 2022",
Verified: true,
},
},
}
buf1 := &bytes.Buffer{}
w1 := NewWriterWithVersion(buf1, Version2)
sz, err := w1.WriteObject(a)
s.Assert().Nil(err)
s.Assert().Equal(249, sz)
// Create some "upgraded" objects that include new fields not present in the
// legacy structs that follow. The structs maintain the original fields and
// values, but add many new fields.
type snap struct {
Guid string `rsf:"guid,fixed:36"` // Not in original struct
Date string `rsf:"date,skip,fixed:10"`
Name string `rsf:"name"`
Project string `rsf:"project"` // Not in original struct
Verified bool `rsf:"verified"`
Skip string `rsf:"-"`
SkipAlso string `rsf:"-"` // Not in original struct
Trust bool `rsf:"trust"` // Not in original struct
}
// Add a new type that was also not included in the original data.
type product struct {
Barcode int32 `rsf:"barcode,skip"`
Name string `rsf:"name"`
Price float32 `rsf:"price"`
}
b := struct {
Location string `rsf:"location"` // Not in original struct
Skip string `rsf:"-"`
Company string `rsf:"company"`
Products []product `rsf:"products,index:barcode"` // Not in original struct
Ready bool `rsf:"ready"`
Portable bool `rsf:"portable"` // Not in original struct
List []snap `rsf:"list,index:date"`
Income float64 `rsf:"income"` // Not in original struct
Age int `rsf:"age"`
Rating float64 `rsf:"rating"`
Zip int `rsf:"zip"` // Not in original struct
}{
Location: "Albuquerque",
Company: "posit",
Ready: true,
Portable: true,
Income: 56999.98,
Age: 55,
Rating: 92.689,
Zip: 75043,
List: []snap{
{
Guid: "199d22ca-719f-40e6-a108-1f2147564168",
Date: "2020-10-01",
Name: "From 2020",
Project: "albatross",
Verified: false,
SkipAlso: "test",
Trust: true,
},
{
Guid: "eba30155-b31c-4287-a7a1-1018010859c1",
Date: "2021-03-21",
Name: "From 2021",
Project: "bluebird",
Verified: true,
Trust: false,
},
{
Guid: "c7f67f5f-7891-42b0-bdbc-82a0e5cd5572",
Date: "2022-12-15",
Name: "this is from 2022",
Project: "none",
Verified: true,
Trust: true,
},
},
Products: []product{
{
Barcode: int32(123456789),
Name: "shovel",
Price: 32.99,
},
{
Barcode: int32(1876543210),
Name: "rake",
Price: 15.44,
},
},
}
buf2 := &bytes.Buffer{}
w2 := NewWriterWithVersion(buf2, Version2)
sz, err = w2.WriteObject(b)
s.Assert().Nil(err)
s.Assert().Equal(656, sz)
// Read the legacy struct with the expected set of fields.
s.validateRead(buf1)
// Read the new struct with all the new fields. The results should
// be identical.
s.validateRead(buf2)
}
func (s *WriterUpgradeSuite) validateRead(b *bytes.Buffer) {
// Read index
r := NewReader()
_, err := r.ReadIndex(b)
s.Assert().Nil(err)
// Read object size.
sz, err := r.ReadSizeField(b)
s.Assert().Nil(err)
// Since we've already read the index, the object size should be the
// remaining buffer bytes, plus 4 for the size field we just read.
s.Assert().Equal(b.Len()+4, sz)
buf := bufio.NewReader(b)
// Advance to company
err = r.AdvanceTo(buf, "company")
s.Assert().Nil(err)
company, err := r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("posit", company)
// Advance to ready
err = r.AdvanceTo(buf, "ready")
s.Assert().Nil(err)
ready, err := r.ReadBoolField(buf)
s.Assert().Nil(err)
s.Assert().True(ready)
// Advance to list
err = r.AdvanceTo(buf, "list")
s.Assert().Nil(err)
// Save start position for array
objectStart := r.Pos()
// Full array size
arraySz, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
// Array should be 3 elements in length
arrayLen, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(3, arrayLen)
// Array index. Read all three index entries
// Entry 1
date, err := r.ReadFixedStringField(10, buf)
s.Assert().Nil(err)
s.Assert().Equal("2020-10-01", date)
_, err = r.ReadSizeField(buf)
s.Assert().Nil(err)
//
// Entry 2
date, err = r.ReadFixedStringField(10, buf)
s.Assert().Nil(err)
s.Assert().Equal("2021-03-21", date)
_, err = r.ReadSizeField(buf)
s.Assert().Nil(err)
//
// Entry 3
date, err = r.ReadFixedStringField(10, buf)
s.Assert().Nil(err)
s.Assert().Equal("2022-12-15", date)
_, err = r.ReadSizeField(buf)
s.Assert().Nil(err)
// Get the first array element's "Name" field
err = r.AdvanceTo(buf, "list", "name")
s.Assert().Nil(err)
name, err := r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("From 2020", name)
// Read the first array element's "Verified" field
err = r.AdvanceTo(buf, "list", "verified")
s.Assert().Nil(err)
verified, err := r.ReadBoolField(buf)
s.Assert().Nil(err)
s.Assert().False(verified)
// Advance to the second array element
err = r.AdvanceToNextElement(buf)
s.Assert().Nil(err)
// Get the second array element's "Name" field
err = r.AdvanceTo(buf, "list", "name")
s.Assert().Nil(err)
name, err = r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("From 2021", name)
// Read the second array element's "Verified" field
err = r.AdvanceTo(buf, "list", "verified")
s.Assert().Nil(err)
verified, err = r.ReadBoolField(buf)
s.Assert().Nil(err)
s.Assert().True(verified)
// Advance to the third array element
err = r.AdvanceToNextElement(buf)
s.Assert().Nil(err)
// Get the third array element's "Name" field
err = r.AdvanceTo(buf, "list", "name")
s.Assert().Nil(err)
name, err = r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("this is from 2022", name)
// Read the third array element's "Verified" field
err = r.AdvanceTo(buf, "list", "verified")
s.Assert().Nil(err)
verified, err = r.ReadBoolField(buf)
s.Assert().Nil(err)
s.Assert().True(verified)
// Advance to the array end
err = r.AdvanceToNextElement(buf)
s.Assert().Nil(err)
// Verify at end of array
s.Assert().Equal(arraySz, r.Pos()-objectStart)
// Advance to age
err = r.AdvanceTo(buf, "age")
s.Assert().Nil(err)
age, err := r.ReadIntField(buf)
s.Assert().Nil(err)
s.Assert().Equal(int64(55), age)
// Advance to rating
err = r.AdvanceTo(buf, "rating")
s.Assert().Nil(err)
rating, err := r.ReadFloatField(buf)
s.Assert().Nil(err)
s.Assert().Equal(92.689, rating)
// Advance to end of struct
err = r.AdvanceToNextElement(buf)
s.Assert().Nil(err)
// Verify at EOF.
_, err = r.ReadSizeField(buf)
s.Assert().ErrorIs(err, io.EOF)
}