Repository: open-feature/go-sdk
Pull request #204 (2023-07-27 20:39:34)

feat: EvaluationContext add constructor without TargetingKey
## This PR
Based on this [discussion](https://github.com/open-feature/go-sdk/pull/193#discussion_r1276491478) we have discovered that in some cases we want to create an EvaluationContext without Targeting key.

With`NewEvaluationContext` it was mandatory to set a `targetingKey` forcing us to set an empty `targetingKey` sometimes.

In this PR we add a new constructor called `NewTargetlessEvaluationContext` that is doing exactly the same things as `NewEvaluationContext` but does not force you to add a `targetingKey`.



codecov[bot]: ## [Codecov](https://app.codecov.io/gh/open-feature/go-sdk/pull/204?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=open-feature) Report
> Merging [#204](https://app.codecov.io/gh/open-feature/go-sdk/pull/204?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=open-feature) (40ffb01) into [main](https://app.codecov.io/gh/open-feature/go-sdk/commit/a2987b80569c78441b2068f5683d53feef014a1a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=open-feature) (a2987b8) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main     #204      +/-   ##
==========================================
+ Coverage   78.95%   78.99%   +0.03%     
==========================================
  Files           9        9              
  Lines        1074     1076       +2     
==========================================
+ Hits          848      850       +2     
  Misses        204      204              
  Partials       22       22              
```


| [Files Changed](https://app.codecov.io/gh/open-feature/go-sdk/pull/204?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=open-feature) | Coverage Δ | |
|---|---|---|
| [pkg/openfeature/evaluation\_context.go](https://app.codecov.io/gh/open-feature/go-sdk/pull/204?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=open-feature#diff-cGtnL29wZW5mZWF0dXJlL2V2YWx1YXRpb25fY29udGV4dC5nbw==) | `82.60% <100.00%> (+1.65%)` | :arrow_up: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=open-feature)


Patch:
diff --git a/pkg/openfeature/evaluation_context.go b/pkg/openfeature/evaluation_context.go
index 8490abd..1913555 100644
--- a/pkg/openfeature/evaluation_context.go
+++ b/pkg/openfeature/evaluation_context.go
@@ -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)
+}


Test patch:
diff --git a/pkg/openfeature/evaluation_context_test.go b/pkg/openfeature/evaluation_context_test.go
index 6a9e626..a6ccce5 100644
--- a/pkg/openfeature/evaluation_context_test.go
+++ b/pkg/openfeature/evaluation_context_test.go
@@ -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")
+	}
+}