Skip to content

Commit 1cc8acd

Browse files
committed
merge prerelease branch
1 parent a4e733b commit 1cc8acd

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

ldcontext/context_easyjson.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ func unmarshalOldUserSchemaEasyJSON(c *Context, in *jlexer.Lexer, usingEventForm
313313
}
314314
in.WantComma()
315315
}
316+
in.Delim('}')
316317
if in.Error() != nil {
317318
return
318319
}

ldcontext/context_easyjson_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
m "github.com/launchdarkly/go-test-helpers/v2/matchers"
10+
1011
"github.com/mailru/easyjson"
1112
"github.com/mailru/easyjson/jlexer"
1213
"github.com/stretchr/testify/assert"
@@ -21,12 +22,25 @@ func easyJSONUnmarshalTestFn(c *Context, data []byte) error {
2122
return easyjson.Unmarshal(data, c)
2223
}
2324

25+
func easyJSONUnmarshalArrayTestFn(cs *[]Context, data []byte) error {
26+
in := jlexer.Lexer{Data: data}
27+
in.Delim('[')
28+
for !in.IsDelim(']') {
29+
var c Context
30+
c.UnmarshalEasyJSON(&in)
31+
*cs = append(*cs, c)
32+
in.WantComma()
33+
}
34+
in.Delim(']')
35+
return in.Error()
36+
}
37+
2438
func TestContextEasyJSONMarshal(t *testing.T) {
2539
contextMarshalingTests(t, easyJSONMarshalTestFn)
2640
}
2741

2842
func TestContextEasyJSONUnmarshal(t *testing.T) {
29-
contextUnmarshalingTests(t, easyJSONUnmarshalTestFn)
43+
contextUnmarshalingTests(t, easyJSONUnmarshalTestFn, easyJSONUnmarshalArrayTestFn)
3044
}
3145

3246
func TestContextEasyJSONMarshalEventOutputFormat(t *testing.T) {

ldcontext/context_unmarshaling_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,16 @@ func translateRegularContextJSONToEventOutputJSONAndViceVersa(s string) string {
240240
return s
241241
}
242242

243-
func contextUnmarshalingTests(t *testing.T, unmarshalFn func(*Context, []byte) error) {
243+
func contextUnmarshalingTests(
244+
t *testing.T,
245+
unmarshalSingleContextFn func(*Context, []byte) error,
246+
unmarshalArrayFn func(*[]Context, []byte) error,
247+
) {
244248
t.Run("valid data", func(t *testing.T) {
245249
for _, p := range makeAllContextUnmarshalingParams() {
246250
t.Run(p.json, func(t *testing.T) {
247251
var c Context
248-
err := unmarshalFn(&c, []byte(p.json))
252+
err := unmarshalSingleContextFn(&c, []byte(p.json))
249253
assert.NoError(t, err)
250254
assert.Equal(t, p.context, c)
251255
})
@@ -256,11 +260,33 @@ func contextUnmarshalingTests(t *testing.T, unmarshalFn func(*Context, []byte) e
256260
for _, badJSON := range makeContextUnmarshalingErrorInputs() {
257261
t.Run(badJSON, func(t *testing.T) {
258262
var c Context
259-
err := unmarshalFn(&c, []byte(badJSON))
263+
err := unmarshalSingleContextFn(&c, []byte(badJSON))
260264
assert.Error(t, err)
261265
})
262266
}
263267
})
268+
269+
if unmarshalArrayFn != nil {
270+
t.Run("within an array", func(t *testing.T) {
271+
// This test shows that in our streaming implementations, the unmarshaler leaves the input
272+
// stream in the proper state at the end of the object, so it will correctly parse a
273+
// subsequent value.
274+
invariantSecondContext := New("simple")
275+
invariantSecondContextJSON := `{"kind": "user", "key": "simple"}`
276+
for _, p := range makeAllContextUnmarshalingParams() {
277+
t.Run(p.json, func(t *testing.T) {
278+
input := `[ ` + p.json + `, ` + invariantSecondContextJSON + ` ]`
279+
var cs []Context
280+
err := unmarshalArrayFn(&cs, []byte(input))
281+
assert.NoError(t, err)
282+
if assert.Len(t, cs, 2) {
283+
assert.Equal(t, p.context, cs[0])
284+
assert.Equal(t, invariantSecondContext, cs[1])
285+
}
286+
})
287+
}
288+
})
289+
}
264290
}
265291

266292
func jsonUnmarshalTestFn(c *Context, data []byte) error {
@@ -273,12 +299,22 @@ func jsonStreamUnmarshalTestFn(c *Context, data []byte) error {
273299
return r.Error()
274300
}
275301

302+
func jsonStreamUnmarshalArrayTestFn(cs *[]Context, data []byte) error {
303+
r := jreader.NewReader(data)
304+
for arr := r.Array(); arr.Next(); {
305+
var c Context
306+
ContextSerialization.UnmarshalFromJSONReader(&r, &c)
307+
*cs = append(*cs, c)
308+
}
309+
return r.Error()
310+
}
311+
276312
func TestContextJSONUnmarshal(t *testing.T) {
277-
contextUnmarshalingTests(t, jsonUnmarshalTestFn)
313+
contextUnmarshalingTests(t, jsonUnmarshalTestFn, nil)
278314
}
279315

280316
func TestContextReadFromJSONReader(t *testing.T) {
281-
contextUnmarshalingTests(t, jsonStreamUnmarshalTestFn)
317+
contextUnmarshalingTests(t, jsonStreamUnmarshalTestFn, jsonStreamUnmarshalArrayTestFn)
282318
}
283319

284320
func TestContextUnmarshalEventOutputFormat(t *testing.T) {

0 commit comments

Comments
 (0)