-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathevaluation_enrichment_hook.go
45 lines (38 loc) · 1.59 KB
/
evaluation_enrichment_hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package hook
import (
"context"
"github.com/open-feature/go-sdk/openfeature"
)
func NewEvaluationEnrichmentHook(exporterMetadata map[string]interface{}) openfeature.Hook {
return &evaluationEnrichmentHook{exporterMetadata: exporterMetadata}
}
type evaluationEnrichmentHook struct {
exporterMetadata map[string]interface{}
}
func (d *evaluationEnrichmentHook) After(_ context.Context, _ openfeature.HookContext,
_ openfeature.InterfaceEvaluationDetails, _ openfeature.HookHints) error {
// Do nothing, needed to satisfy the interface
return nil
}
func (d *evaluationEnrichmentHook) Error(_ context.Context, _ openfeature.HookContext,
_ error, _ openfeature.HookHints) {
// Do nothing, needed to satisfy the interface
}
func (d *evaluationEnrichmentHook) Before(_ context.Context, hookCtx openfeature.HookContext, _ openfeature.HookHints) (*openfeature.EvaluationContext, error) {
attributes := hookCtx.EvaluationContext().Attributes()
if goffSpecific, ok := attributes["gofeatureflag"]; ok {
switch typed := goffSpecific.(type) {
case map[string]interface{}:
typed["exporterMetadata"] = d.exporterMetadata
default:
attributes["gofeatureflag"] = map[string]interface{}{"exporterMetadata": d.exporterMetadata}
}
} else {
attributes["gofeatureflag"] = map[string]interface{}{"exporterMetadata": d.exporterMetadata}
}
newCtx := openfeature.NewEvaluationContext(hookCtx.EvaluationContext().TargetingKey(), attributes)
return &newCtx, nil
}
func (d *evaluationEnrichmentHook) Finally(context.Context, openfeature.HookContext, openfeature.HookHints) {
// Do nothing, needed to satisfy the interface
}