Skip to content

Commit c77b08f

Browse files
chore: clean code
Signed-off-by: Thomas Poignant <[email protected]>
1 parent fc1395e commit c77b08f

File tree

10 files changed

+113
-329
lines changed

10 files changed

+113
-329
lines changed

SOLUTION_SUMMARY.md

Lines changed: 0 additions & 202 deletions
This file was deleted.

examples/empty_targeting_key_example.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ my-targeted-feature:
6262
}
6363
defer ffclient.Close()
6464

65-
fmt.Println("=== Empty Targeting Key Examples ===\n")
65+
fmt.Println("=== Empty Targeting Key Examples ===")
66+
fmt.Println()
6667

6768
// Example 1: Static flag (should work with empty context)
6869
fmt.Println("1. Static flag with empty evaluation context:")

ffcontext/context.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ func NewEvaluationContext(key string) EvaluationContext {
2323
return EvaluationContext{targetingKey: key, attributes: map[string]interface{}{}}
2424
}
2525

26-
// NewEvaluationContextWithoutTargetingKey creates a new evaluation context without a targeting key.
27-
// This should only be used for flags that don't require bucketing (no percentage-based rules).
28-
func NewEvaluationContextWithoutTargetingKey() EvaluationContext {
29-
return EvaluationContext{targetingKey: "", attributes: map[string]interface{}{}}
30-
}
31-
3226
// Deprecated: NewAnonymousEvaluationContext is here for compatibility reason.
3327
// Please use NewEvaluationContext instead and add a attributes attribute to know that it is an anonymous user.
3428
//

ffcontext/context_builder.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ func NewEvaluationContextBuilder(key string) EvaluationContextBuilder {
1111
}
1212
}
1313

14-
// NewEvaluationContextBuilderWithoutTargetingKey constructs a new EvaluationContextBuilder without a targeting key.
15-
// This should only be used for flags that don't require bucketing (no percentage-based rules).
16-
func NewEvaluationContextBuilderWithoutTargetingKey() EvaluationContextBuilder {
17-
return &evaluationContextBuilderImpl{
18-
key: "",
19-
custom: map[string]interface{}{},
20-
}
21-
}
22-
2314
// EvaluationContextBuilder is a builder to create an EvaluationContext.
2415
type EvaluationContextBuilder interface {
2516
// Deprecated: Anonymous is to flag the context for an anonymous context or not.

ffcontext/context_builder_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,21 @@ func TestNewUser(t *testing.T) {
9292
})
9393
}
9494
}
95+
96+
func TestNewEvaluationContextWithoutTargetingKey(t *testing.T) {
97+
ctx := NewEvaluationContext("")
98+
assert.Equal(t, "", ctx.GetKey(), "Should have empty targeting key")
99+
assert.Equal(t, map[string]interface{}{}, ctx.GetCustom(), "Should have empty custom attributes")
100+
assert.False(t, ctx.IsAnonymous(), "Should not be anonymous by default")
101+
}
102+
103+
func TestNewEvaluationContextBuilderWithoutTargetingKey(t *testing.T) {
104+
ctx := NewEvaluationContextBuilder("").
105+
AddCustom("role", "admin").
106+
AddCustom("anonymous", true).
107+
Build()
108+
109+
assert.Equal(t, "", ctx.GetKey(), "Should have empty targeting key")
110+
assert.Equal(t, "admin", ctx.GetCustom()["role"], "Should have custom attributes")
111+
assert.True(t, ctx.IsAnonymous(), "Should be anonymous when set")
112+
}

ffcontext/context_empty_targeting_test.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

internal/flag/internal_flag_empty_context_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestInternalFlag_Value_EmptyContext(t *testing.T) {
258258
VariationResult: testconvert.String("disabled"),
259259
},
260260
},
261-
evaluationCtx: ffcontext.NewEvaluationContextWithoutTargetingKey(),
261+
evaluationCtx: ffcontext.NewEvaluationContext(""),
262262
expectedValue: false,
263263
expectedReason: flag.ReasonStatic,
264264
shouldSucceed: true,
@@ -278,7 +278,7 @@ func TestInternalFlag_Value_EmptyContext(t *testing.T) {
278278
},
279279
},
280280
},
281-
evaluationCtx: ffcontext.NewEvaluationContextWithoutTargetingKey(),
281+
evaluationCtx: ffcontext.NewEvaluationContext(""),
282282
expectedErrorCode: flag.ErrorCodeTargetingKeyMissing,
283283
expectedReason: flag.ReasonError,
284284
shouldSucceed: false,
@@ -321,7 +321,7 @@ func TestInternalFlag_Value_EmptyContext(t *testing.T) {
321321
},
322322
},
323323
evaluationCtx: func() ffcontext.Context {
324-
ctx := ffcontext.NewEvaluationContextWithoutTargetingKey()
324+
ctx := ffcontext.NewEvaluationContext("")
325325
ctx.AddCustomAttribute("anonymous", true)
326326
return ctx
327327
}(),
@@ -371,7 +371,7 @@ func TestInternalFlag_GetBucketingKeyValue_EmptyContext(t *testing.T) {
371371
VariationResult: testconvert.String("disabled"),
372372
},
373373
},
374-
evaluationCtx: ffcontext.NewEvaluationContextWithoutTargetingKey(),
374+
evaluationCtx: ffcontext.NewEvaluationContext(""),
375375
expectedKey: "",
376376
expectedError: false,
377377
description: "Non-bucketing flag should allow empty targeting key",
@@ -390,7 +390,7 @@ func TestInternalFlag_GetBucketingKeyValue_EmptyContext(t *testing.T) {
390390
},
391391
},
392392
},
393-
evaluationCtx: ffcontext.NewEvaluationContextWithoutTargetingKey(),
393+
evaluationCtx: ffcontext.NewEvaluationContext(""),
394394
expectedKey: "",
395395
expectedError: true,
396396
description: "Bucketing flag should require targeting key",
@@ -411,7 +411,7 @@ func TestInternalFlag_GetBucketingKeyValue_EmptyContext(t *testing.T) {
411411
},
412412
},
413413
evaluationCtx: func() ffcontext.Context {
414-
ctx := ffcontext.NewEvaluationContextWithoutTargetingKey()
414+
ctx := ffcontext.NewEvaluationContext("")
415415
ctx.AddCustomAttribute("teamId", "team-123")
416416
return ctx
417417
}(),
@@ -432,7 +432,7 @@ func TestInternalFlag_GetBucketingKeyValue_EmptyContext(t *testing.T) {
432432
},
433433
},
434434
evaluationCtx: func() ffcontext.Context {
435-
ctx := ffcontext.NewEvaluationContextWithoutTargetingKey()
435+
ctx := ffcontext.NewEvaluationContext("")
436436
ctx.AddCustomAttribute("teamId", "")
437437
return ctx
438438
}(),

0 commit comments

Comments
 (0)