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

chore: refactor bucketing key to be a pointer as all other elements from DTO #2395

Merged
merged 3 commits into from
Sep 20, 2024
Merged
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
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 @@
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 @@
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 @@
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 ""

Check warning on line 386 in internal/flag/internal_flag.go

View check run for this annotation

Codecov / codecov/patch

internal/flag/internal_flag.go#L386

Added line #L386 was not covered by tests
}
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()[key]
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
Loading