From e86e78e0a5b836d10d807f6cc000e009efdb8651 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Tue, 13 Feb 2024 18:02:19 +0100 Subject: [PATCH 1/2] fix: targeting key serialization fix --- demo/GoDemoApp.go | 3 +-- pkg/provider/provider.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/demo/GoDemoApp.go b/demo/GoDemoApp.go index 0c5d7bc..49e6d5d 100644 --- a/demo/GoDemoApp.go +++ b/demo/GoDemoApp.go @@ -24,11 +24,10 @@ func main() { attributes := make(map[string]interface{}) targetingKey := uuid.New().String() - attributes["targeting_key"] = targetingKey fmt.Println(" Random UUID -> " + targetingKey) - of := openfeature.NewEvaluationContext("", attributes) + of := openfeature.NewEvaluationContext(targetingKey, attributes) colorValue, _ := client.StringValue(context.Background(), "hawkflag.color", "defaultValue", of) messageValue, _ := client.StringValue(context.Background(), "hawkflag.message", "defaultValue", of) diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index e634d8f..f3291aa 100755 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -69,7 +69,7 @@ func (e FlagProvider) resolveFlag(ctx context.Context, flag string, defaultValue requestFlagName := fmt.Sprintf("flags/%s", flagName) resp, err := e.ResolveClient.sendResolveRequest(ctx, resolveRequest{ClientSecret: e.Config.APIKey, - Flags: []string{requestFlagName}, Apply: true, EvaluationContext: evalCtx, + Flags: []string{requestFlagName}, Apply: true, EvaluationContext: processTargetingKey(evalCtx), Sdk: sdk{Id: SDK_ID, Version: SDK_VERSION}}) if err != nil { @@ -96,6 +96,15 @@ func (e FlagProvider) resolveFlag(ctx context.Context, flag string, defaultValue return processResolvedFlag(resolvedFlag, defaultValue, expectedKind, propertyPath) } +func processTargetingKey(evalCtx openfeature.FlattenedContext) openfeature.FlattenedContext { + newEvalContext := openfeature.FlattenedContext{} + newEvalContext = evalCtx + if targetingKey, exists := evalCtx["targetingKey"]; exists { + newEvalContext["targeting_key"] = targetingKey + } + return newEvalContext +} + func (e FlagProvider) Hooks() []openfeature.Hook { return []openfeature.Hook{} } From 6de02002d9ce96f8d71e4ca5ac23875155283aab Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Wed, 14 Feb 2024 15:44:36 +0100 Subject: [PATCH 2/2] test: Test targeting_key field --- pkg/provider/provider_internal_test.go | 36 ++++++++++++++------------ 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/pkg/provider/provider_internal_test.go b/pkg/provider/provider_internal_test.go index 6a2ef3d..9dd5a54 100644 --- a/pkg/provider/provider_internal_test.go +++ b/pkg/provider/provider_internal_test.go @@ -15,15 +15,17 @@ import ( type MockResolveClient struct { MockedResponse resolveResponse MockedError error + TestingT *testing.T } func (r MockResolveClient) sendResolveRequest(_ context.Context, - _ resolveRequest) (resolveResponse, error) { - return r.MockedResponse, r.MockedError + request resolveRequest) (resolveResponse, error) { + assert.Equal(r.TestingT, "user1", request.EvaluationContext["targeting_key"]) + return r.MockedResponse, r.MockedError } func TestResolveBoolValue(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.BooleanValueDetails( @@ -38,7 +40,7 @@ func TestResolveBoolValue(t *testing.T) { } func TestResolveIntValue(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.IntValueDetails( @@ -50,7 +52,7 @@ func TestResolveIntValue(t *testing.T) { } func TestResolveDoubleValue(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.FloatValueDetails( @@ -62,7 +64,7 @@ func TestResolveDoubleValue(t *testing.T) { } func TestResolveStringValue(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.StringValueDetails( @@ -74,7 +76,7 @@ func TestResolveStringValue(t *testing.T) { } func TestResolveObjectValue(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.ObjectValueDetails( @@ -87,7 +89,7 @@ func TestResolveObjectValue(t *testing.T) { } func TestResolveNestedValue(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.BooleanValueDetails( @@ -99,19 +101,19 @@ func TestResolveNestedValue(t *testing.T) { } func TestResolveDoubleNestedValue(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.BooleanValueDetails( context.Background(), "test-flag.struct-key.nested-struct-key.nested-boolean-key", true, openfeature.NewEvaluationContext( - "dennis", + "user1", attributes)) assert.Equal(t, false, evalDetails.Value) } func TestResolveWholeFlagAsObject(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.ObjectValueDetails( @@ -124,7 +126,7 @@ func TestResolveWholeFlagAsObject(t *testing.T) { } func TestResolveWholeFlagAsObjectWithInts(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.ObjectValueDetails( @@ -145,7 +147,7 @@ func TestResolveWholeFlagAsObjectWithInts(t *testing.T) { } func TestResolveWithWrongType(t *testing.T) { - client := client(templateResponse(), nil) + client := client(t, templateResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.BooleanValueDetails( @@ -159,7 +161,7 @@ func TestResolveWithWrongType(t *testing.T) { } func TestResolveWithUnexpectedFlag(t *testing.T) { - client := client(templateResponseWithFlagName("wrong-flag"), nil) + client := client(t, templateResponseWithFlagName("wrong-flag"), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.BooleanValueDetails( @@ -174,7 +176,7 @@ func TestResolveWithUnexpectedFlag(t *testing.T) { } func TestResolveWithNonExistingFlag(t *testing.T) { - client := client(emptyResponse(), nil) + client := client(t, emptyResponse(), nil) attributes := make(map[string]interface{}) evalDetails, _ := client.BooleanValueDetails( @@ -188,9 +190,9 @@ func TestResolveWithNonExistingFlag(t *testing.T) { assert.Equal(t, "no active flag 'test-flag' was found", evalDetails.ErrorMessage) } -func client(response resolveResponse, errorToReturn error) *openfeature.Client { +func client(t *testing.T, response resolveResponse, errorToReturn error) *openfeature.Client { provider := FlagProvider{Config: APIConfig{APIKey: "apikey", - Region: APIRegionGlobal}, ResolveClient: MockResolveClient{MockedResponse: response, MockedError: errorToReturn}} + Region: APIRegionGlobal}, ResolveClient: MockResolveClient{MockedResponse: response, MockedError: errorToReturn, TestingT: t}} openfeature.SetProvider(provider) return openfeature.NewClient("testApp") }