Skip to content

Commit

Permalink
feat: EvaluationContext add constructor without TargetingKey (#204)
Browse files Browse the repository at this point in the history
Add constructor without TargetingKey

Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
  • Loading branch information
thomaspoignant authored Jul 28, 2023
1 parent a2987b8 commit 07f4974
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/openfeature/evaluation_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ func NewEvaluationContext(targetingKey string, attributes map[string]interface{}
attributes: attrs,
}
}

// NewTargetlessEvaluationContext constructs an EvaluationContext with an empty targeting key
//
// attributes - contextual data used in flag evaluation
func NewTargetlessEvaluationContext(attributes map[string]interface{}) EvaluationContext {
return NewEvaluationContext("", attributes)
}
14 changes: 14 additions & 0 deletions pkg/openfeature/evaluation_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,17 @@ func TestEvaluationContext_AttributesFuncNotPassedByReference(t *testing.T) {
t.Error("mutation of map passed to SetAttributes caused a mutation of its attributes field")
}
}

func TestNewTargetlessEvaluationContext(t *testing.T) {
attributes := map[string]interface{}{
"foo": "bar",
}
evalCtx := NewTargetlessEvaluationContext(attributes)
if evalCtx.targetingKey != "" {
t.Error("targeting key should not be set with NewTargetlessEvaluationContext")
}

if !reflect.DeepEqual(evalCtx.Attributes(), attributes) {
t.Errorf("we expect no difference in the attributes")
}
}

0 comments on commit 07f4974

Please sign in to comment.