|
| 1 | +package evaluate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/open-feature/go-sdk-contrib/providers/ofrep/internal/outbound" |
| 8 | + of "github.com/open-feature/go-sdk/openfeature" |
| 9 | +) |
| 10 | + |
| 11 | +// Flags is the flag evaluator implementation. It contains domain logic of the OpenFeature flag evaluation. |
| 12 | +type Flags struct { |
| 13 | + resolver resolver |
| 14 | +} |
| 15 | + |
| 16 | +type resolver interface { |
| 17 | + resolveSingle(ctx context.Context, key string, evalCtx map[string]interface{}) (*successDto, *of.ResolutionError) |
| 18 | +} |
| 19 | + |
| 20 | +func NewFlagsEvaluator(cfg outbound.Configuration) *Flags { |
| 21 | + return &Flags{ |
| 22 | + resolver: NewOutboundResolver(cfg), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (h Flags) ResolveBoolean(ctx context.Context, key string, defaultValue bool, evalCtx map[string]interface{}) of.BoolResolutionDetail { |
| 27 | + evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx) |
| 28 | + if resolutionError != nil { |
| 29 | + return of.BoolResolutionDetail{ |
| 30 | + Value: defaultValue, |
| 31 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 32 | + ResolutionError: *resolutionError, |
| 33 | + Reason: of.ErrorReason, |
| 34 | + }, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + b, ok := evalSuccess.Value.(bool) |
| 39 | + if !ok { |
| 40 | + return of.BoolResolutionDetail{ |
| 41 | + Value: defaultValue, |
| 42 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 43 | + ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf( |
| 44 | + "resolved value %v is not of boolean type", evalSuccess.Value)), |
| 45 | + Reason: of.ErrorReason, |
| 46 | + }, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return of.BoolResolutionDetail{ |
| 51 | + Value: b, |
| 52 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 53 | + Reason: of.Reason(evalSuccess.Reason), |
| 54 | + Variant: evalSuccess.Variant, |
| 55 | + FlagMetadata: evalSuccess.Metadata, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (h Flags) ResolveString(ctx context.Context, key string, defaultValue string, evalCtx map[string]interface{}) of.StringResolutionDetail { |
| 61 | + evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx) |
| 62 | + if resolutionError != nil { |
| 63 | + return of.StringResolutionDetail{ |
| 64 | + Value: defaultValue, |
| 65 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 66 | + ResolutionError: *resolutionError, |
| 67 | + Reason: of.ErrorReason, |
| 68 | + }, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + b, ok := evalSuccess.Value.(string) |
| 73 | + if !ok { |
| 74 | + return of.StringResolutionDetail{ |
| 75 | + Value: defaultValue, |
| 76 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 77 | + ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf( |
| 78 | + "resolved value %v is not of string type", evalSuccess.Value)), |
| 79 | + Reason: of.ErrorReason, |
| 80 | + }, |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return of.StringResolutionDetail{ |
| 85 | + Value: b, |
| 86 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 87 | + Reason: of.Reason(evalSuccess.Reason), |
| 88 | + Variant: evalSuccess.Variant, |
| 89 | + FlagMetadata: evalSuccess.Metadata, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (h Flags) ResolveFloat(ctx context.Context, key string, defaultValue float64, evalCtx map[string]interface{}) of.FloatResolutionDetail { |
| 95 | + evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx) |
| 96 | + if resolutionError != nil { |
| 97 | + return of.FloatResolutionDetail{ |
| 98 | + Value: defaultValue, |
| 99 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 100 | + ResolutionError: *resolutionError, |
| 101 | + Reason: of.ErrorReason, |
| 102 | + }, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + var value float64 |
| 107 | + |
| 108 | + switch evalSuccess.Value.(type) { |
| 109 | + case float32: |
| 110 | + value = float64(evalSuccess.Value.(float32)) |
| 111 | + case float64: |
| 112 | + value = evalSuccess.Value.(float64) |
| 113 | + default: |
| 114 | + return of.FloatResolutionDetail{ |
| 115 | + Value: defaultValue, |
| 116 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 117 | + ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf( |
| 118 | + "resolved value %v is not of float type", evalSuccess.Value)), |
| 119 | + Reason: of.ErrorReason, |
| 120 | + }, |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return of.FloatResolutionDetail{ |
| 125 | + Value: value, |
| 126 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 127 | + Reason: of.Reason(evalSuccess.Reason), |
| 128 | + Variant: evalSuccess.Variant, |
| 129 | + FlagMetadata: evalSuccess.Metadata, |
| 130 | + }, |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func (h Flags) ResolveInt(ctx context.Context, key string, defaultValue int64, evalCtx map[string]interface{}) of.IntResolutionDetail { |
| 135 | + evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx) |
| 136 | + if resolutionError != nil { |
| 137 | + return of.IntResolutionDetail{ |
| 138 | + Value: defaultValue, |
| 139 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 140 | + ResolutionError: *resolutionError, |
| 141 | + Reason: of.ErrorReason, |
| 142 | + }, |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + var value int64 |
| 147 | + |
| 148 | + switch evalSuccess.Value.(type) { |
| 149 | + case int: |
| 150 | + value = int64(evalSuccess.Value.(int)) |
| 151 | + case int64: |
| 152 | + value = evalSuccess.Value.(int64) |
| 153 | + default: |
| 154 | + return of.IntResolutionDetail{ |
| 155 | + Value: defaultValue, |
| 156 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 157 | + ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf( |
| 158 | + "resolved value %v is not of integer type", evalSuccess.Value)), |
| 159 | + Reason: of.ErrorReason, |
| 160 | + }, |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return of.IntResolutionDetail{ |
| 165 | + Value: value, |
| 166 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 167 | + Reason: of.Reason(evalSuccess.Reason), |
| 168 | + Variant: evalSuccess.Variant, |
| 169 | + FlagMetadata: evalSuccess.Metadata, |
| 170 | + }, |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func (h Flags) ResolveObject(ctx context.Context, key string, defaultValue interface{}, evalCtx map[string]interface{}) of.InterfaceResolutionDetail { |
| 175 | + evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx) |
| 176 | + if resolutionError != nil { |
| 177 | + return of.InterfaceResolutionDetail{ |
| 178 | + Value: defaultValue, |
| 179 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 180 | + ResolutionError: *resolutionError, |
| 181 | + Reason: of.ErrorReason, |
| 182 | + }, |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return of.InterfaceResolutionDetail{ |
| 187 | + Value: evalSuccess.Value, |
| 188 | + ProviderResolutionDetail: of.ProviderResolutionDetail{ |
| 189 | + Reason: of.Reason(evalSuccess.Reason), |
| 190 | + Variant: evalSuccess.Variant, |
| 191 | + FlagMetadata: evalSuccess.Metadata, |
| 192 | + }, |
| 193 | + } |
| 194 | +} |
0 commit comments