diff --git a/core/pkg/evaluator/json.go b/core/pkg/evaluator/json.go index 655eaf75c..bf9a23e9c 100644 --- a/core/pkg/evaluator/json.go +++ b/core/pkg/evaluator/json.go @@ -327,6 +327,13 @@ func (je *Resolver) evaluateVariant(ctx context.Context, reqID string, flagKey s metadata[SelectorMetadataKey] = selector } + for key, value := range flag.Metadata { + // If value is not nil or empty, copy to metadata + if value != nil { + metadata[key] = value + } + } + if flag.State == Disabled { je.Logger.DebugWithID(reqID, fmt.Sprintf("requested flag is disabled: %s", flagKey)) return "", flag.Variants, model.ErrorReason, metadata, errors.New(model.FlagDisabledErrorCode) @@ -460,11 +467,26 @@ func configToFlags(log *logger.Logger, config string, newFlags *Flags) error { return fmt.Errorf("transposing evaluators: %w", err) } - err = json.Unmarshal([]byte(transposedConfig), &newFlags) + var configData ConfigWithMetadata + err = json.Unmarshal([]byte(transposedConfig), &configData) if err != nil { return fmt.Errorf("unmarshalling provided configurations: %w", err) } + // Assign the flags from the unmarshalled config to the newFlags struct + newFlags.Flags = configData.Flags + + // Assign metadata as a map to each flag's metadata + for key, flag := range newFlags.Flags { + if flag.Metadata == nil { + flag.Metadata = make(map[string]interface{}) + } + for metaKey, metaValue := range configData.Metadata { + flag.Metadata[metaKey] = metaValue + } + newFlags.Flags[key] = flag + } + return validateDefaultVariants(newFlags) } diff --git a/core/pkg/evaluator/json_model.go b/core/pkg/evaluator/json_model.go index 00a7105ef..8b9c17114 100644 --- a/core/pkg/evaluator/json_model.go +++ b/core/pkg/evaluator/json_model.go @@ -10,6 +10,11 @@ type Evaluators struct { Evaluators map[string]json.RawMessage `json:"$evaluators"` } +type ConfigWithMetadata struct { + Flags map[string]model.Flag `json:"flags"` + Metadata map[string]interface{} `json:"metadata"` +} + type Flags struct { Flags map[string]model.Flag `json:"flags"` } diff --git a/core/pkg/model/flag.go b/core/pkg/model/flag.go index 342b0488b..6ea0988f0 100644 --- a/core/pkg/model/flag.go +++ b/core/pkg/model/flag.go @@ -3,12 +3,13 @@ package model import "encoding/json" type Flag struct { - State string `json:"state"` - DefaultVariant string `json:"defaultVariant"` - Variants map[string]any `json:"variants"` - Targeting json.RawMessage `json:"targeting,omitempty"` - Source string `json:"source"` - Selector string `json:"selector"` + State string `json:"state"` + DefaultVariant string `json:"defaultVariant"` + Variants map[string]any `json:"variants"` + Targeting json.RawMessage `json:"targeting,omitempty"` + Source string `json:"source"` + Selector string `json:"selector"` + Metadata map[string]interface{} `json:"metadata"` } type Evaluators struct { diff --git a/core/pkg/store/flags.go b/core/pkg/store/flags.go index 626e2940f..00d27c57c 100644 --- a/core/pkg/store/flags.go +++ b/core/pkg/store/flags.go @@ -22,6 +22,7 @@ type Flags struct { Flags map[string]model.Flag `json:"flags"` FlagSources []string SourceMetadata map[string]SourceDetails + Metadata map[string]interface{} `json:"metadata"` } type SourceDetails struct {