From e86e78e0a5b836d10d807f6cc000e009efdb8651 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Tue, 13 Feb 2024 18:02:19 +0100 Subject: [PATCH] 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{} }