-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathevaluate.go
66 lines (57 loc) · 1.67 KB
/
evaluate.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package evaluate
import (
"context"
"encoding/json"
"errors"
"time"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/internal/utils"
"github.com/thomaspoignant/go-feature-flag/model"
"github.com/thomaspoignant/go-feature-flag/retriever/fileretriever"
)
type evaluate struct {
config string
fileFormat string
flag string
evaluationCtx string
}
func (e evaluate) Evaluate() (map[string]model.RawVarResult, error) {
c := ffclient.Config{
PollingInterval: 10 * time.Minute,
DisableNotifierOnInit: true,
Context: context.Background(),
Retriever: &fileretriever.Retriever{Path: e.config},
FileFormat: e.fileFormat,
}
goff, err := ffclient.New(c)
if err != nil {
return nil, err
}
if e.evaluationCtx == "" {
return nil, errors.New("invalid evaluation context (missing targeting key)")
}
var ctxAsMap map[string]interface{}
result := map[string]model.RawVarResult{}
err = json.Unmarshal([]byte(e.evaluationCtx), &ctxAsMap)
if err != nil {
return nil, err
}
if targetingKey, ok := ctxAsMap["targetingKey"].(string); ok {
convertedEvaluationCtx := utils.ConvertEvaluationCtxFromRequest(targetingKey, ctxAsMap)
listFLags := make([]string, 0)
if e.flag != "" {
listFLags = append(listFLags, e.flag)
} else {
flags, _ := goff.GetFlagsFromCache()
for key := range flags {
listFLags = append(listFLags, key)
}
}
for _, flag := range listFLags {
res, _ := goff.RawVariation(flag, convertedEvaluationCtx, nil)
result[flag] = res
}
return result, nil
}
return nil, errors.New("invalid evaluation context (missing targeting key)")
}