1 package ldcontext 2 3 import ( 4 "github.com/launchdarkly/go-jsonstream/v3/jreader" 5 "github.com/launchdarkly/go-jsonstream/v3/jwriter" 6 ) 7 8 // Note: other ContextSerialization methods are in the conditionally-compiled file 9 // context_easyjson.go. 10 11 // ContextSerializationMethods contains JSON marshaling and unmarshaling methods that are not 12 // normally used directly by applications. These methods are exported because they are used in 13 // LaunchDarkly service code and the Relay Proxy. 14 type ContextSerializationMethods struct{} 15 16 // ContextSerialization is the global entry point for ContextSerializationMethods. 17 var ContextSerialization ContextSerializationMethods //nolint:gochecknoglobals 18 19 // UnmarshalFromJSONReader unmarshals a Context with the jsonstream Reader API. 20 // 21 // In case of failure, the error is both returned from the method and stored as a failure state in 22 // the Reader. 23 func (s ContextSerializationMethods) UnmarshalFromJSONReader(r *jreader.Reader, c *Context) error { 24 unmarshalFromJSONReader(r, c, false) 25 return r.Error() 26 } 27 28 // UnmarshalFromJSONReaderEventOutput unmarshals an EventContext with the jsonstream Reader API. 29 // 30 // In case of failure, the error is both returned from the method and stored as a failure state in 31 // the Reader. 32 func (s ContextSerializationMethods) UnmarshalFromJSONReaderEventOutput(r *jreader.Reader, c *EventOutputContext) { 33 unmarshalFromJSONReader(r, &c.Context, true) 34 } 35 36 // UnmarshalWithKindAndKeyOnly is a special unmarshaling mode where all properties except kind and 37 // key are discarded. It works for both single and multi-contexts. This is more efficient 38 // than the regular unmarshaling logic in situations where contexts need to be indexed by Key or 39 // FullyQualifiedKey. 40 // 41 // Because most properties are discarded immediately without checking their value, some error 42 // conditions (for instance, a "name" property whose value is not a string) will not be detected by 43 // this method. It will fail only if validation related to the kind or key fails. 44 func (s ContextSerializationMethods) UnmarshalWithKindAndKeyOnly(r *jreader.Reader, c *Context) error { 45 unmarshalWithKindAndKeyOnly(r, c) 46 return r.Error() 47 } 48 49 // MarshalToJSONWriter marshals a Context with the jsonstream Writer API. 50 func (s ContextSerializationMethods) MarshalToJSONWriter(w *jwriter.Writer, c *Context) { 51 writeToJSONWriterInternal(w, c, false) 52 } 53 54 // MarshalToJSONWriterEventOutput marshals an EventOutputContext with the jsonstream Writer API. 55 func (s ContextSerializationMethods) MarshalToJSONWriterEventOutput(w *jwriter.Writer, c *EventOutputContext) { 56 writeToJSONWriterInternal(w, &c.Context, true) 57 } 58 59 func writeToJSONWriterInternal(w *jwriter.Writer, c *Context, usingEventFormat bool) { 60 if err := c.Err(); err != nil { 61 w.AddError(err) 62 return 63 } 64 if c.multiContexts == nil { 65 c.writeToJSONWriterInternalSingle(w, "", usingEventFormat) 66 } else { 67 c.writeToJSONWriterInternalMulti(w, usingEventFormat) 68 } 69 } 70