Skip to content

Commit f016717

Browse files
prepare 2.2.2 release (#16)
1 parent f22d15d commit f016717

6 files changed

+615
-202
lines changed

lduser/user_serialization_easyjson.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
package lduser
44

55
import (
6-
"gopkg.in/launchdarkly/go-jsonstream.v1/jreader"
76
"gopkg.in/launchdarkly/go-jsonstream.v1/jwriter"
7+
"gopkg.in/launchdarkly/go-sdk-common.v2/ldvalue"
88

99
"github.com/mailru/easyjson/jlexer"
1010
ej_jwriter "github.com/mailru/easyjson/jwriter"
@@ -17,8 +17,10 @@ import (
1717
// encoding/json, and will call them if present. But this mechanism is inefficient: when marshaling
1818
// it requires the allocation of intermediate byte slices, and when unmarshaling it causes the
1919
// JSON object to be parsed twice. It is preferable to have our marshal/unmarshal methods write to
20-
// and read from the EasyJSON Writer/Lexer directly. Our go-jsonstream library provides methods for
21-
// doing this, if the launchdarkly_easyjson build tag is set.package ldmodel
20+
// and read from the EasyJSON Writer/Lexer directly. Also, since user deserialization is a
21+
// high-traffic path in some LaunchDarkly code on the service side, the extra overhead of the
22+
// go-jsonstream abstraction is undesirable and we'll instead use an EasyJSON-generated
23+
// deserializer for an intermediate struct type.
2224
//
2325
// For more information, see: https://gopkg.in/launchdarkly/go-jsonstream.v1
2426

@@ -28,7 +30,52 @@ func (u User) MarshalEasyJSON(writer *ej_jwriter.Writer) {
2830
}
2931

3032
func (u *User) UnmarshalEasyJSON(lexer *jlexer.Lexer) {
31-
wrappedReader := jreader.NewReaderFromEasyJSONLexer(lexer)
32-
u.ReadFromJSONReader(&wrappedReader)
33-
lexer.AddError(wrappedReader.Error())
33+
if lexer.IsNull() {
34+
lexer.Delim('{') // to trigger an "expected an object, got null" error
35+
return
36+
}
37+
var fields userEquivalentStruct
38+
fields.UnmarshalEasyJSON(lexer)
39+
if lexer.Error() != nil {
40+
return
41+
}
42+
if !fields.Key.IsDefined() {
43+
lexer.AddError(ErrMissingKey())
44+
return
45+
}
46+
u.key = fields.Key.StringValue()
47+
u.secondary = fields.Secondary
48+
u.ip = fields.Ip
49+
u.country = fields.Country
50+
u.email = fields.Email
51+
u.firstName = fields.FirstName
52+
u.lastName = fields.LastName
53+
u.avatar = fields.Avatar
54+
u.name = fields.Name
55+
u.anonymous = fields.Anonymous
56+
u.custom = fields.Custom
57+
if len(fields.PrivateAttributeNames) != 0 {
58+
u.privateAttributes = make(map[UserAttribute]struct{}, len(fields.PrivateAttributeNames))
59+
for _, a := range fields.PrivateAttributeNames {
60+
u.privateAttributes[UserAttribute(a)] = struct{}{}
61+
}
62+
}
63+
}
64+
65+
//go:generate easyjson -build_tags launchdarkly_easyjson -output_filename user_serialization_easyjson_generated.go user_serialization_easyjson.go
66+
67+
//easyjson:json
68+
type userEquivalentStruct struct {
69+
Key ldvalue.OptionalString `json:"key"`
70+
Secondary ldvalue.OptionalString `json:"secondary,omitempty"`
71+
Ip ldvalue.OptionalString `json:"ip,omitempty"`
72+
Country ldvalue.OptionalString `json:"country,omitempty"`
73+
Email ldvalue.OptionalString `json:"email,omitempty"`
74+
FirstName ldvalue.OptionalString `json:"firstName,omitempty"`
75+
LastName ldvalue.OptionalString `json:"lastName,omitempty"`
76+
Avatar ldvalue.OptionalString `json:"avatar,omitempty"`
77+
Name ldvalue.OptionalString `json:"name,omitempty"`
78+
Anonymous ldvalue.OptionalBool `json:"anonymous,omitempty"`
79+
Custom ldvalue.ValueMap `json:"custom,omitempty"`
80+
PrivateAttributeNames []string `json:"privateAttributeNames,omitempty"`
3481
}

lduser/user_serialization_easyjson_generated.go

Lines changed: 194 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build launchdarkly_easyjson
2+
3+
package lduser
4+
5+
import (
6+
"testing"
7+
8+
easyjson "github.com/mailru/easyjson"
9+
)
10+
11+
func TestEasyJSONMarshal(t *testing.T) {
12+
doUserMarshalingTests(t, func(value interface{}) ([]byte, error) {
13+
return easyjson.Marshal(value.(easyjson.Marshaler))
14+
})
15+
}
16+
17+
func TestEasyJSONUnmarshal(t *testing.T) {
18+
doUserUnmarshalingTests(t, func(data []byte, target interface{}) error {
19+
return easyjson.Unmarshal(data, target.(easyjson.Unmarshaler))
20+
})
21+
}

0 commit comments

Comments
 (0)