-
Notifications
You must be signed in to change notification settings - Fork 4
/
context_serialization.go
69 lines (59 loc) · 2.88 KB
/
context_serialization.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
package ldcontext
import (
"github.com/launchdarkly/go-jsonstream/v3/jreader"
"github.com/launchdarkly/go-jsonstream/v3/jwriter"
)
// Note: other ContextSerialization methods are in the conditionally-compiled file
// context_easyjson.go.
// ContextSerializationMethods contains JSON marshaling and unmarshaling methods that are not
// normally used directly by applications. These methods are exported because they are used in
// LaunchDarkly service code and the Relay Proxy.
type ContextSerializationMethods struct{}
// ContextSerialization is the global entry point for ContextSerializationMethods.
var ContextSerialization ContextSerializationMethods //nolint:gochecknoglobals
// UnmarshalFromJSONReader unmarshals a Context with the jsonstream Reader API.
//
// In case of failure, the error is both returned from the method and stored as a failure state in
// the Reader.
func (s ContextSerializationMethods) UnmarshalFromJSONReader(r *jreader.Reader, c *Context) error {
unmarshalFromJSONReader(r, c, false)
return r.Error()
}
// UnmarshalFromJSONReaderEventOutput unmarshals an EventContext with the jsonstream Reader API.
//
// In case of failure, the error is both returned from the method and stored as a failure state in
// the Reader.
func (s ContextSerializationMethods) UnmarshalFromJSONReaderEventOutput(r *jreader.Reader, c *EventOutputContext) {
unmarshalFromJSONReader(r, &c.Context, true)
}
// UnmarshalWithKindAndKeyOnly is a special unmarshaling mode where all properties except kind and
// key are discarded. It works for both single and multi-contexts. This is more efficient
// than the regular unmarshaling logic in situations where contexts need to be indexed by Key or
// FullyQualifiedKey.
//
// Because most properties are discarded immediately without checking their value, some error
// conditions (for instance, a "name" property whose value is not a string) will not be detected by
// this method. It will fail only if validation related to the kind or key fails.
func (s ContextSerializationMethods) UnmarshalWithKindAndKeyOnly(r *jreader.Reader, c *Context) error {
unmarshalWithKindAndKeyOnly(r, c)
return r.Error()
}
// MarshalToJSONWriter marshals a Context with the jsonstream Writer API.
func (s ContextSerializationMethods) MarshalToJSONWriter(w *jwriter.Writer, c *Context) {
writeToJSONWriterInternal(w, c, false)
}
// MarshalToJSONWriterEventOutput marshals an EventOutputContext with the jsonstream Writer API.
func (s ContextSerializationMethods) MarshalToJSONWriterEventOutput(w *jwriter.Writer, c *EventOutputContext) {
writeToJSONWriterInternal(w, &c.Context, true)
}
func writeToJSONWriterInternal(w *jwriter.Writer, c *Context, usingEventFormat bool) {
if err := c.Err(); err != nil {
w.AddError(err)
return
}
if c.multiContexts == nil {
c.writeToJSONWriterInternalSingle(w, "", usingEventFormat)
} else {
c.writeToJSONWriterInternalMulti(w, usingEventFormat)
}
}