From f75d5b5b8df9d21f7a7c54c371d0020bf4a18e1a Mon Sep 17 00:00:00 2001 From: Aasif Khan Date: Wed, 18 Dec 2024 16:58:06 +0530 Subject: [PATCH] fix: start returning flagSetId, flagSetVersion as well in flag metadata --- core/pkg/evaluator/json.go | 21 ++++++++++++++++++++- core/pkg/evaluator/json_model.go | 10 ++++++++++ core/pkg/model/flag.go | 6 ++++-- core/pkg/store/flags.go | 10 +++++----- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/core/pkg/evaluator/json.go b/core/pkg/evaluator/json.go index 0a8100c54..f2a3b1028 100644 --- a/core/pkg/evaluator/json.go +++ b/core/pkg/evaluator/json.go @@ -35,6 +35,8 @@ const ( Disabled = "DISABLED" ID = "id" VERSION = "version" + FLAGSETVERSION = "flagSetVersion" + FLAGSETID = "flagSetId" ) var regBrace *regexp.Regexp @@ -336,6 +338,12 @@ func (je *Resolver) evaluateVariant(ctx context.Context, reqID string, flagKey s if flagMetadata.Version != "" { metadata[VERSION] = flagMetadata.Version } + if flagMetadata.FlagSetID != "" { + metadata[FLAGSETID] = flagMetadata.FlagSetID + } + if flagMetadata.FlagSetVersion != "" { + metadata[FLAGSETVERSION] = flagMetadata.FlagSetVersion + } if flag.State == Disabled { je.Logger.DebugWithID(reqID, fmt.Sprintf("requested flag is disabled: %s", flagKey)) @@ -470,11 +478,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) } diff --git a/core/pkg/evaluator/json_model.go b/core/pkg/evaluator/json_model.go index 00a7105ef..3e06d9b23 100644 --- a/core/pkg/evaluator/json_model.go +++ b/core/pkg/evaluator/json_model.go @@ -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"` } diff --git a/core/pkg/model/flag.go b/core/pkg/model/flag.go index 0b6e12802..532935adb 100644 --- a/core/pkg/model/flag.go +++ b/core/pkg/model/flag.go @@ -13,8 +13,10 @@ type Flag struct { } type Metadata struct { - ID string `json:"id"` - Version string `json:"version"` + ID string `json:"id"` + Version string `json:"version"` + FlagSetID string `json:"flagSetId"` + FlagSetVersion string `json:"flagSetVersion"` } type Evaluators struct { diff --git a/core/pkg/store/flags.go b/core/pkg/store/flags.go index 35b60d3a6..8e95c1c40 100644 --- a/core/pkg/store/flags.go +++ b/core/pkg/store/flags.go @@ -19,11 +19,11 @@ type IStore interface { } type Flags struct { - mx sync.RWMutex - Flags map[string]model.Flag `json:"flags"` - FlagSources []string - SourceMetadata map[string]SourceDetails - FlagSetMetadata model.Metadata `json:"metadata"` + mx sync.RWMutex + Flags map[string]model.Flag `json:"flags"` + FlagSources []string + SourceMetadata map[string]SourceDetails + Metadata model.Metadata `json:"metadata"` } type SourceDetails struct {