Skip to content

Commit

Permalink
Adding a check for the userkey attribute (#6)
Browse files Browse the repository at this point in the history
* Adding a check for the userkey attribute

* Added a userkey test

Co-authored-by: chris <[email protected]>
Co-authored-by: Chrusty <>
  • Loading branch information
chrusty and chris authored Sep 5, 2021
1 parent f1c7198 commit d303f73
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions client_with_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ var TestFeature1States = []*models.FeatureState{
},
},
},
{
ID: "s3.1",
Name: "userkey",
Value: "this is for userkey prawn",
Attributes: []*models.StrategyAttribute{
{
ID: "a3.1",
Conditional: strategies.ConditionalEquals,
FieldName: strategies.FieldNameUserkey,
Values: []interface{}{"prawn"},
Type: strategies.TypeString,
},
},
},
{
ID: "s4",
Name: "version-less",
Expand Down Expand Up @@ -267,6 +281,13 @@ func TestClientWithContext(t *testing.T) {
assert.Equal(t, "this is not for mobile users", stringValue)
assert.NoError(t, err)

// See if we can match the "userkey" attribute:
stringValue, err = testClient.
WithContext(&models.Context{Userkey: "prawn"}).
GetString("TestFeature1")
assert.Equal(t, "this is for userkey prawn", stringValue)
assert.NoError(t, err)

// See if we can match the "version-less" attribute:
stringValue, err = testClient.
WithContext(&models.Context{Version: "5.6.7"}).
Expand Down
13 changes: 13 additions & 0 deletions pkg/models/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ func (s Strategy) proceedWithAttributes(clientContext *Context) bool {
logger.Tracef("Didn't match attribute strategy (%s:%s = %v) for platform: %v\n", sa.ID, sa.FieldName, sa.Values, clientContext.Platform)
return false

// Match by userkey:
case strategies.FieldNameUserkey:
logger.Trace("Trying userkey")
matched, err := sa.matchType(sa.Values, fmt.Sprintf("%s", clientContext.Userkey))
if err != nil {
logger.WithError(err).Error("Unable to match type")
}
if matched {
continue
}
logger.Tracef("Didn't match attribute strategy (%s:%s = %v) for userkey: %v\n", sa.ID, sa.FieldName, sa.Values, clientContext.Userkey)
return false

// Match by version:
case strategies.FieldNameVersion:
logger.Trace("Trying version")
Expand Down
1 change: 1 addition & 0 deletions pkg/strategies/field_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ const (
FieldNameCountry = "country"
FieldNameDevice = "device"
FieldNamePlatform = "platform"
FieldNameUserkey = "userkey"
FieldNameVersion = "version"
)

0 comments on commit d303f73

Please sign in to comment.