Skip to content

Commit

Permalink
chore: refactor bucketing key to be a pointer as all other elements f…
Browse files Browse the repository at this point in the history
…rom DTO

Signed-off-by: Thomas Poignant <[email protected]>
  • Loading branch information
thomaspoignant committed Sep 20, 2024
1 parent 3d42409 commit 97cd88f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
24 changes: 18 additions & 6 deletions internal/flag/internal_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type InternalFlag struct {
Rules *[]Rule `json:"targeting,omitempty" yaml:"targeting,omitempty" toml:"targeting,omitempty"`

// BucketingKey defines a source for a dynamic targeting key
BucketingKey string `json:"bucketingKey,omitempty" yaml:"bucketingKey,omitempty" toml:"bucketingKey,omitempty"`
BucketingKey *string `json:"bucketingKey,omitempty" yaml:"bucketingKey,omitempty" toml:"bucketingKey,omitempty"`

// DefaultRule is the originalRule applied after checking that any other rules
// matched the user.
Expand Down Expand Up @@ -80,7 +80,7 @@ func (f *InternalFlag) Value(
maps.Copy(evaluationCtx.GetCustom(), flagContext.EvaluationContextEnrichment)
}

key, keyError := flag.GetBucketingKey(evaluationCtx)
key, keyError := flag.GetBucketingKeyValue(evaluationCtx)

if keyError != nil {
return flagContext.DefaultSdkValue, ResolutionDetails{
Expand Down Expand Up @@ -380,17 +380,29 @@ func (f *InternalFlag) GetVariationValue(name string) interface{} {
return nil
}

func (f *InternalFlag) GetBucketingKey(ctx ffcontext.Context) (string, error) {
if f.BucketingKey != "" {
value := ctx.GetCustom()[f.BucketingKey]
// GetBucketingKey return the name of the custom bucketing key if we are using one.
func (f *InternalFlag) GetBucketingKey() string {
if f.BucketingKey == nil {
return ""
}
return *f.BucketingKey
}

// GetBucketingKeyValue return the value of the bucketing key from the context
func (f *InternalFlag) GetBucketingKeyValue(ctx ffcontext.Context) (string, error) {
if f.BucketingKey != nil {
key := f.GetBucketingKey()
if key == "" {
return ctx.GetKey(), nil
}
value := ctx.GetCustom()[f.GetBucketingKey()]
switch v := value.(type) {
case string:
return v, nil
default:
return "", fmt.Errorf("invalid bucketing key")
}
}

return ctx.GetKey(), nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/flag/internal_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,7 @@ func TestInternalFlag_ValueWithBucketingKey(t *testing.T) {
"variation_B": testconvert.Interface("value_B"),
"variation_C": testconvert.Interface("value_C"),
},
BucketingKey: "teamId",
BucketingKey: testconvert.String("teamId"),
DefaultRule: &flag.Rule{
Percentages: &map[string]float64{
"variation_A": 33,
Expand All @@ -1758,7 +1758,7 @@ func TestInternalFlag_ValueWithBucketingKey(t *testing.T) {
assert.Equal(t, tt.wantForTeamID, got.Variant)

flagWithoutBucketingKey := tt.flag
flagWithoutBucketingKey.BucketingKey = ""
flagWithoutBucketingKey.BucketingKey = testconvert.String("")
_, got = flagWithoutBucketingKey.Value(tt.args.flagName, tt.args.user, tt.args.flagContext)

assert.Equal(t, tt.wantForTargetingKey, got.Variant)
Expand All @@ -1773,7 +1773,7 @@ func TestInternalFlag_ValueWithBucketingKeyNotFound(t *testing.T) {
"variation_B": testconvert.Interface("value_B"),
"variation_C": testconvert.Interface("value_C"),
},
BucketingKey: "teamId",
BucketingKey: testconvert.String("teamId"),
DefaultRule: &flag.Rule{
Percentages: &map[string]float64{
"variation_A": 33,
Expand Down
2 changes: 1 addition & 1 deletion model/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type DTOv1 struct {
Rules *[]flag.Rule `json:"targeting,omitempty" yaml:"targeting,omitempty" toml:"targeting,omitempty" jsonschema:"title=targeting,description=List of rule to target a subset of the users based on the evaluation context."` // nolint: lll

// BucketingKey defines a source for a dynamic targeting key
BucketingKey string `json:"bucketingKey,omitempty" yaml:"bucketingKey,omitempty" toml:"bucketingKey,omitempty"`
BucketingKey *string `json:"bucketingKey,omitempty" yaml:"bucketingKey,omitempty" toml:"bucketingKey,omitempty"`

// DefaultRule is the rule applied after checking that any other rules
// matched the user.
Expand Down

0 comments on commit 97cd88f

Please sign in to comment.