-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathevaluation_enrichment_hook.go
31 lines (27 loc) · 1.08 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
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 {
openfeature.UnimplementedHook
exporterMetadata map[string]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
}