Skip to content

Commit

Permalink
Fix rule with environment (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Apr 18, 2023
1 parent 4539b15 commit 01d6ccc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ffuser/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ func (u *User) IsAnonymous() bool {
func (u *User) GetCustom() map[string]interface{} {
return u.custom
}

// AddCustomAttribute allows to add a custom attribute into the user.
func (u *User) AddCustomAttribute(name string, value interface{}) {
if name != "" {
u.custom[name] = value
}
}
44 changes: 44 additions & 0 deletions ffuser/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ffuser_test

import (
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/ffuser"
"testing"
)

func TestUser_AddCustomAttribute(t *testing.T) {
type args struct {
name string
value interface{}
}
tests := []struct {
name string
user ffuser.User
args args
want map[string]interface{}
}{
{
name: "trying to add nil value",
user: ffuser.NewUser("123"),
args: args{},
want: map[string]interface{}{},
},
{
name: "add valid element",
user: ffuser.NewUser("123"),
args: args{
name: "test",
value: "test",
},
want: map[string]interface{}{
"test": "test",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.user.AddCustomAttribute(tt.args.name, tt.args.value)
assert.Equal(t, tt.want, tt.user.GetCustom())
})
}
}
4 changes: 4 additions & 0 deletions internal/flag/internal_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (f *InternalFlag) Value(
) (interface{}, ResolutionDetails) {
f.applyScheduledRolloutSteps()

if evaluationCtx.Environment != "" {
user.AddCustomAttribute("env", evaluationCtx.Environment)
}

if f.IsDisable() || f.isExperimentationOver() {
return evaluationCtx.DefaultSdkValue, ResolutionDetails{
Variant: VariationSDKDefault,
Expand Down
38 changes: 38 additions & 0 deletions internal/flag/internal_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,44 @@ func TestInternalFlag_Value(t *testing.T) {
Cacheable: true,
},
},
{
name: "Should use the environment as a rule criteria",
flag: flag.InternalFlag{
Variations: &map[string]*interface{}{
"A": testconvert.Interface(false),
"B": testconvert.Interface(true),
},
Rules: &[]flag.Rule{
{
Query: testconvert.String("env == \"development\""),
VariationResult: testconvert.String("A"),
},
{
Query: testconvert.String("(env == \"production\") " +
"or (env == \"staging\") or (env == \"qa\")"),
VariationResult: testconvert.String("B"),
},
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("B"),
},
},
args: args{
flagName: "feature",
user: ffuser.NewUserBuilder("1").Build(),
evaluationCtx: flag.EvaluationContext{
DefaultSdkValue: true,
Environment: "development",
},
},
want: false,
want1: flag.ResolutionDetails{
Variant: "A",
Reason: flag.ReasonTargetingMatch,
Cacheable: true,
RuleIndex: testconvert.Int(0),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 01d6ccc

Please sign in to comment.