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 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion core/pkg/evaluator/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const (
// evaluation if the user did not supply the optional bucketing property.
targetingKeyKey = "targetingKey"
Disabled = "DISABLED"
ID = "id"
VERSION = "version"
FLAGSETVERSION = "flagSetVersion"
FLAGSETID = "flagSetId"
)

var regBrace *regexp.Regexp
Expand Down Expand Up @@ -327,6 +331,19 @@ func (je *Resolver) evaluateVariant(ctx context.Context, reqID string, flagKey s
metadata[SelectorMetadataKey] = selector
}

if flag.Metadata.ID != "" {
metadata[ID] = flag.Metadata.ID
}
if flag.Metadata.Version != "" {
metadata[VERSION] = flag.Metadata.Version
}
if flag.Metadata.FlagSetID != "" {
metadata[FLAGSETID] = flag.Metadata.FlagSetID
}
if flag.Metadata.FlagSetVersion != "" {
metadata[FLAGSETVERSION] = flag.Metadata.FlagSetVersion
}

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 +477,22 @@ 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 version and id from metadata to the flags
for key, flag := range newFlags.Flags {
flag.Metadata.FlagSetID = configData.MetaData.ID
flag.Metadata.FlagSetVersion = configData.MetaData.Version
newFlags.Flags[key] = flag
}

return validateDefaultVariants(newFlags)
}

Expand Down
10 changes: 10 additions & 0 deletions core/pkg/evaluator/json_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ type Evaluators struct {
Evaluators map[string]json.RawMessage `json:"$evaluators"`
}

type Metadata struct {
ID string `json:"id"`
Version string `json:"version"`
}

type ConfigWithMetadata struct {
Flags map[string]model.Flag `json:"flags"`
MetaData Metadata `json:"metadata"`
}

type Flags struct {
Flags map[string]model.Flag `json:"flags"`
}
8 changes: 8 additions & 0 deletions core/pkg/model/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ type Flag struct {
Targeting json.RawMessage `json:"targeting,omitempty"`
Source string `json:"source"`
Selector string `json:"selector"`
Metadata Metadata `json:"metadata"`
}

type Metadata struct {
ID string `json:"id"`
Version string `json:"version"`
FlagSetID string `json:"flagSetId"`
FlagSetVersion string `json:"flagSetVersion"`
}

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 model.Metadata `json:"metadata"`
}

type SourceDetails struct {
Expand Down
Loading