-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support of evaluation context enrichment (#1285)
- Loading branch information
1 parent
8ffeb58
commit b3874ea
Showing
15 changed files
with
315 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
package flag | ||
|
||
type Context struct { | ||
// Environment is the name of your current env | ||
// this value will be added to the custom information of your user and, | ||
// it will allow to create rules based on this environment, | ||
Environment string | ||
// EvaluationContextEnrichment will be merged with the evaluation context sent during the evaluation. | ||
// It is useful to add common attributes to all the evaluation, such as a server version, environment, ... | ||
// | ||
// All those fields will be included in the custom attributes of the evaluation context, | ||
// if in the evaluation context you have a field with the same name, it will override the common one. | ||
// Default: nil | ||
EvaluationContextEnrichment map[string]interface{} | ||
|
||
// DefaultSdkValue is the default value of the SDK when calling the variation. | ||
DefaultSdkValue interface{} | ||
} | ||
|
||
func (s *Context) AddIntoEvaluationContextEnrichment(key string, value interface{}) { | ||
if s.EvaluationContextEnrichment == nil { | ||
s.EvaluationContextEnrichment = make(map[string]interface{}) | ||
} | ||
s.EvaluationContextEnrichment[key] = value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package flag_test | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/thomaspoignant/go-feature-flag/internal/flag" | ||
"testing" | ||
) | ||
|
||
func TestContext_AddIntoEvaluationContextEnrichment(t *testing.T) { | ||
type args struct { | ||
key string | ||
value interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
EvaluationContextEnrichment map[string]interface{} | ||
args args | ||
expected interface{} | ||
}{ | ||
{ | ||
name: "Add a new key to a nil map", | ||
EvaluationContextEnrichment: nil, | ||
args: args{ | ||
key: "env", | ||
value: "prod", | ||
}, | ||
expected: "prod", | ||
}, | ||
{ | ||
name: "Add a new key to an existing map", | ||
EvaluationContextEnrichment: map[string]interface{}{"john": "doe"}, | ||
args: args{ | ||
key: "env", | ||
value: "prod", | ||
}, | ||
expected: "prod", | ||
}, | ||
{ | ||
name: "Override an existing key", | ||
EvaluationContextEnrichment: map[string]interface{}{"env": "dev"}, | ||
args: args{ | ||
key: "env", | ||
value: "prod", | ||
}, | ||
expected: "prod", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &flag.Context{ | ||
EvaluationContextEnrichment: tt.EvaluationContextEnrichment, | ||
} | ||
s.AddIntoEvaluationContextEnrichment(tt.args.key, tt.args.value) | ||
assert.Equal(t, tt.expected, s.EvaluationContextEnrichment[tt.args.key]) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.