diff --git a/client_with_context_test.go b/client_with_context_test.go index ca41134..ea69c09 100644 --- a/client_with_context_test.go +++ b/client_with_context_test.go @@ -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", @@ -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"}). diff --git a/pkg/models/strategies.go b/pkg/models/strategies.go index 30535a4..568a3d8 100644 --- a/pkg/models/strategies.go +++ b/pkg/models/strategies.go @@ -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") diff --git a/pkg/strategies/field_attributes.go b/pkg/strategies/field_attributes.go index 796b562..820e23f 100644 --- a/pkg/strategies/field_attributes.go +++ b/pkg/strategies/field_attributes.go @@ -4,5 +4,6 @@ const ( FieldNameCountry = "country" FieldNameDevice = "device" FieldNamePlatform = "platform" + FieldNameUserkey = "userkey" FieldNameVersion = "version" )