Skip to content

Commit

Permalink
return flag metadata as well
Browse files Browse the repository at this point in the history
Signed-off-by: Aasif Khan <aasifk106@gmail.com>
  • Loading branch information
Aasif Khan authored and Aasif Khan committed Dec 17, 2024
1 parent dd4869f commit c55537f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 10 additions & 0 deletions core/pkg/evaluator/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
// evaluation if the user did not supply the optional bucketing property.
targetingKeyKey = "targetingKey"
Disabled = "DISABLED"
ID = "id"
VERSION = "version"
)

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

flagMetadata := je.store.MetadataForFlag(ctx, flag)
if flagMetadata.ID != "" {
metadata[ID] = flagMetadata.ID
}
if flagMetadata.Version != "" {
metadata[VERSION] = flagMetadata.Version
}

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
6 changes: 6 additions & 0 deletions core/pkg/model/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ 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"`
}

type Evaluators struct {
Expand Down
17 changes: 13 additions & 4 deletions core/pkg/store/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type IStore interface {
GetAll(ctx context.Context) (map[string]model.Flag, error)
Get(ctx context.Context, key string) (model.Flag, bool)
SelectorForFlag(ctx context.Context, flag model.Flag) string
MetadataForFlag(ctx context.Context, flag model.Flag) model.Metadata
}

type Flags struct {
mx sync.RWMutex
Flags map[string]model.Flag `json:"flags"`
FlagSources []string
SourceMetadata map[string]SourceDetails
mx sync.RWMutex
Flags map[string]model.Flag `json:"flags"`
FlagSources []string
SourceMetadata map[string]SourceDetails
FlagSetMetadata model.Metadata
}

type SourceDetails struct {
Expand Down Expand Up @@ -72,6 +74,13 @@ func (f *Flags) SelectorForFlag(_ context.Context, flag model.Flag) string {
return f.SourceMetadata[flag.Source].Selector
}

func (f *Flags) MetadataForFlag(_ context.Context, flag model.Flag) model.Metadata {
f.mx.RLock()
defer f.mx.RUnlock()

return flag.Metadata
}

func (f *Flags) Delete(key string) {
f.mx.Lock()
defer f.mx.Unlock()
Expand Down

0 comments on commit c55537f

Please sign in to comment.