Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: targeting key serialization fix #46

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions demo/GoDemoApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Comment on lines +99 to +106
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't say much about the Go specifics but looks to do the right thing.
Would love to see a unit test for this!


func (e FlagProvider) Hooks() []openfeature.Hook {
return []openfeature.Hook{}
}
36 changes: 19 additions & 17 deletions pkg/provider/provider_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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")
}
Expand Down
Loading