Documentation
¶
Overview ¶
Package flagstate builds the state of a flag evaluation returned by the all flags and flag evaluation APIs.
Index ¶
Constants ¶
const MetadataSegmentStatus = "semaphore_segmentStatus"
MetadataSegmentStatus is the response metadata key of the health of the segment lookups of an evaluation.
Variables ¶
var ErrNoEncoding = errors.New("the value holds no encoding")
ErrNoEncoding is the error of encoding the zero EncodedValue: it holds nothing, so it must not be put in a response.
Functions ¶
func WithSegmentStatus ¶ added in v0.2.0
WithSegmentStatus adds the MetadataSegmentStatus key to the metadata of an evaluation whose segment lookups were not healthy (STALE, NOT_CONFIGURED or STORE_ERROR). A healthy evaluation, and one with no segment, get the metadata back untouched: no copy, no allocation. The map belongs to the evaluation, so the key is added to it.
Types ¶
type AllFlags ¶
type AllFlags struct {
// contains filtered or unexported fields
}
AllFlags is a snapshot of the state of multiple feature flags with regard to a specific user. This is the return type of the AllFlagsState method of the client. Serializing this object to JSON MarshalJSON() will produce a JSON you can sent to your front-end.
func NewAllFlags ¶
func NewAllFlags() AllFlags
NewAllFlags will create a new AllFlags for a specific user. It sets valid to true, because until we have an error everything is valid.
func (AllFlags) MarshalJSON ¶
MarshalJSON is serializing the object to JSON, it will produce a JSON you can sent to your front-end.
func (AllFlags) MarshalJSONTo ¶ added in v0.3.0
MarshalJSONTo is the streaming form of MarshalJSON, which the encoder of encoding/json calls in the Go versions that build it on encoding/json/v2: the flags are written to the response as they are encoded, with no buffer of the whole answer and no copy of it. The bytes are those of MarshalJSON.
type EncodedValue ¶ added in v0.3.0
type EncodedValue struct {
// contains filtered or unexported fields
}
EncodedValue is the canonical JSON of a variation value, encoded once when the flag is loaded. A response that holds it in a value field writes the stored bytes instead of encoding the value again, and the bytes are those that the encoder writes for the value (the same key order, number form and escaping). The bytes are immutable and shared by every request: nothing writes into them after EncodeValue returned. The zero value holds nothing.
It is a single pointer on purpose: a value of this type goes into an interface without an allocation, and the encoder calls its method without making a copy to take the address of.
func EncodeValue ¶ added in v0.3.0
func EncodeValue(value any) (EncodedValue, error)
EncodeValue encodes a variation value the way a response does. It fails for a value that has no JSON encoding: the caller then keeps encoding that value per request, and the request gets the error it always got.
func (EncodedValue) Held ¶ added in v0.3.0
func (e EncodedValue) Held() bool
Held tells whether the value holds an encoding.
func (EncodedValue) Len ¶ added in v0.3.0
func (e EncodedValue) Len() int
Len is the number of bytes the encoding holds.
func (EncodedValue) MarshalJSON ¶ added in v0.3.0
func (e EncodedValue) MarshalJSON() ([]byte, error)
MarshalJSON returns the stored encoding. The slice is shared: the encoder only reads it.
func (EncodedValue) Raw ¶ added in v0.3.0
func (e EncodedValue) Raw() json.RawMessage
Raw returns the stored encoding as the raw JSON that a wire struct holds in its value field: the slice is shared and read only, and its capacity is its length so that nothing appended to it can write into the stored bytes. It is nil for the zero value.
type FlagState ¶
type FlagState struct {
Value any `json:"value"`
Timestamp int64 `json:"timestamp"`
VariationType string `json:"variationType"`
TrackEvents bool `json:"trackEvents"`
Failed bool `json:"-"`
ErrorCode flagdef.ErrorCode `json:"errorCode"`
ErrorDetails string `json:"errorDetails,omitempty"`
Reason flagdef.ResolutionReason `json:"reason"`
Metadata map[string]any `json:"metadata,omitempty"`
// SegmentStatus is the health of the segment lookups; it is not part of the JSON, see [WithSegmentStatus].
SegmentStatus segment.Status `json:"-"`
}
FlagState represents the state of an individual feature flag, with regard to a specific user, when it was called.
func FromFlagEvaluation ¶
func FromFlagEvaluation(key string, evaluationCtx ffcontext.Context, flagCtx flagdef.Context, currentFlag flagdef.Flag) FlagState
FromFlagEvaluation builds the state of a flag evaluated now.
func FromFlagEvaluationAt ¶
func FromFlagEvaluationAt(evaluatedAt time.Time, key string, evaluationCtx ffcontext.Context, flagCtx flagdef.Context, currentFlag flagdef.Flag) FlagState
FromFlagEvaluationAt builds the state of a flag evaluated at the given instant.
func (*FlagState) MetadataWithStatus ¶ added in v0.2.0
MetadataWithStatus is the metadata of the state as it is sent: the metadata itself when the segment lookups were healthy or there was none, else the metadata with MetadataSegmentStatus set. The map is the one that the evaluation made for this state (the engine clones the metadata of the flag for each evaluation, see constructMetadata), so the key is written into it: no second copy. A state that is built from a map that is shared must not be given a status.
type ObjectWriter ¶ added in v0.3.0
type ObjectWriter struct {
// contains filtered or unexported fields
}
ObjectWriter writes a JSON object to an encoder, key by key, in the order of the calls. It is how a response writes a stored encoding in place of a value without a mirror struct: the keys, their order and the omission of empty fields are the caller's, and a value of a plain type goes through the token methods, which do not allocate. The first error is kept and every later call does nothing; ObjectWriter.End returns it.
func NewObjectWriter ¶ added in v0.3.0
func NewObjectWriter(enc *jsontext.Encoder) *ObjectWriter
NewObjectWriter starts an object on the encoder.
func (*ObjectWriter) Any ¶ added in v0.3.0
func (w *ObjectWriter) Any(value any)
Any encodes a value with the default encoding.
func (*ObjectWriter) Bool ¶ added in v0.3.0
func (w *ObjectWriter) Bool(value bool)
Bool writes a boolean value.
func (*ObjectWriter) End ¶ added in v0.3.0
func (w *ObjectWriter) End() error
End closes the object and returns the first error.
func (*ObjectWriter) Int ¶ added in v0.3.0
func (w *ObjectWriter) Int(value int64)
Int writes an integer value.
func (*ObjectWriter) Key ¶ added in v0.3.0
func (w *ObjectWriter) Key(name string)
Key writes the name of the next field.
func (*ObjectWriter) Raw ¶ added in v0.3.0
func (w *ObjectWriter) Raw(value json.RawMessage)
Raw writes bytes that are a valid JSON value as they are.
func (*ObjectWriter) String ¶ added in v0.3.0
func (w *ObjectWriter) String(value string)
String writes a string value.