Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return flag metadata as well #1476

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
24 changes: 23 additions & 1 deletion core/pkg/evaluator/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Comment on lines +480 to +488
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configData.Metadata should have a lower merge priority than flag.Metadata.


return validateDefaultVariants(newFlags)
}

Expand Down
5 changes: 5 additions & 0 deletions core/pkg/evaluator/json_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
13 changes: 7 additions & 6 deletions core/pkg/model/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions core/pkg/store/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading