-
Notifications
You must be signed in to change notification settings - Fork 71
/
models.go
117 lines (98 loc) · 2.9 KB
/
models.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ofrep
import (
"fmt"
"github.com/open-feature/flagd/core/pkg/evaluator"
"github.com/open-feature/flagd/core/pkg/model"
)
type Request struct {
Context interface{} `json:"context"`
}
type EvaluationSuccess struct {
Value interface{} `json:"value"`
Key string `json:"key"`
Reason string `json:"reason"`
Variant string `json:"variant"`
Metadata interface{} `json:"metadata"`
}
type BulkEvaluationResponse struct {
Flags []interface{} `json:"flags"`
}
type EvaluationError struct {
Key string `json:"key"`
ErrorCode string `json:"errorCode"`
ErrorDetails string `json:"errorDetails"`
}
type BulkEvaluationError struct {
ErrorCode string `json:"errorCode"`
ErrorDetails string `json:"errorDetails"`
}
type InternalError struct {
ErrorDetails string `json:"errorDetails"`
}
func BulkEvaluationResponseFrom(values []evaluator.AnyValue) BulkEvaluationResponse {
evaluations := make([]interface{}, 0)
for _, value := range values {
if value.Error != nil {
_, evaluationError := EvaluationErrorResponseFrom(value)
evaluations = append(evaluations, evaluationError)
} else {
evaluations = append(evaluations, SuccessResponseFrom(value))
}
}
return BulkEvaluationResponse{
evaluations,
}
}
func SuccessResponseFrom(result evaluator.AnyValue) EvaluationSuccess {
return EvaluationSuccess{
Value: result.Value,
Key: result.FlagKey,
Reason: result.Reason,
Variant: result.Variant,
Metadata: result.Metadata,
}
}
func ContextErrorResponseFrom(key string) EvaluationError {
return EvaluationError{
Key: key,
ErrorCode: model.InvalidContextCode,
ErrorDetails: "Provider context is not valid",
}
}
func BulkEvaluationContextError() BulkEvaluationError {
return BulkEvaluationError{
ErrorCode: model.InvalidContextCode,
ErrorDetails: "Provider context is not valid",
}
}
func BulkEvaluationContextErrorFrom(code string, details string) BulkEvaluationError {
return BulkEvaluationError{
ErrorCode: code,
ErrorDetails: details,
}
}
func EvaluationErrorResponseFrom(result evaluator.AnyValue) (int, EvaluationError) {
payload := EvaluationError{
Key: result.FlagKey,
}
status := 400
switch result.Error.Error() {
case model.FlagNotFoundErrorCode:
status = 404
payload.ErrorCode = model.FlagNotFoundErrorCode
payload.ErrorDetails = fmt.Sprintf("flag `%s` does not exist", result.FlagKey)
case model.FlagDisabledErrorCode:
status = 404
payload.ErrorCode = model.FlagNotFoundErrorCode
payload.ErrorDetails = fmt.Sprintf("flag `%s` is disabled", result.FlagKey)
case model.ParseErrorCode:
payload.ErrorCode = model.ParseErrorCode
payload.ErrorDetails = fmt.Sprintf("error parsing the flag `%s`", result.FlagKey)
case model.GeneralErrorCode:
fallthrough
default:
payload.ErrorCode = model.GeneralErrorCode
payload.ErrorDetails = "error processing the flag for evaluation"
}
return status, payload
}