OpenFeature: Use types from Go Feature Flag pkg (#108802)
* Use go feature flag types * Add go-feature-flag module * Update go.mod * Update test * Fix
This commit is contained in:
@@ -7,13 +7,14 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/open-feature/go-sdk/openfeature"
|
||||
goffmodel "github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/model"
|
||||
)
|
||||
|
||||
// StaticFlagEvaluator provides methods for evaluating static feature flags
|
||||
// it is only used when static provider is configured
|
||||
type StaticFlagEvaluator interface {
|
||||
EvalFlag(ctx context.Context, flagKey string) (openfeature.BooleanEvaluationDetails, error)
|
||||
EvalAllFlags(ctx context.Context) (OFREPBulkResponse, error)
|
||||
EvalFlag(ctx context.Context, flagKey string) (goffmodel.OFREPEvaluateSuccessResponse, error)
|
||||
EvalAllFlags(ctx context.Context) (goffmodel.OFREPBulkEvaluateSuccessResponse, error)
|
||||
}
|
||||
|
||||
// CreateStaticEvaluator is a dependency for ofrep APIBuilder
|
||||
@@ -53,22 +54,29 @@ type staticEvaluator struct {
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func (s *staticEvaluator) EvalFlag(ctx context.Context, flagKey string) (openfeature.BooleanEvaluationDetails, error) {
|
||||
func (s *staticEvaluator) EvalFlag(ctx context.Context, flagKey string) (goffmodel.OFREPEvaluateSuccessResponse, error) {
|
||||
result, err := s.client.BooleanValueDetails(ctx, flagKey, false, openfeature.TransactionContext(ctx))
|
||||
if err != nil {
|
||||
return openfeature.BooleanEvaluationDetails{}, fmt.Errorf("failed to evaluate flag %s: %w", flagKey, err)
|
||||
return goffmodel.OFREPEvaluateSuccessResponse{}, fmt.Errorf("failed to evaluate flag %s: %w", flagKey, err)
|
||||
}
|
||||
resp := goffmodel.OFREPEvaluateSuccessResponse{
|
||||
Key: flagKey,
|
||||
Value: result.Value,
|
||||
Reason: "static provider evaluation result",
|
||||
Variant: result.Variant,
|
||||
Metadata: result.FlagMetadata,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *staticEvaluator) EvalAllFlags(ctx context.Context) (OFREPBulkResponse, error) {
|
||||
func (s *staticEvaluator) EvalAllFlags(ctx context.Context) (goffmodel.OFREPBulkEvaluateSuccessResponse, error) {
|
||||
flags, err := s.provider.ListFlags()
|
||||
if err != nil {
|
||||
return OFREPBulkResponse{}, fmt.Errorf("static provider failed to list all flags: %w", err)
|
||||
return goffmodel.OFREPBulkEvaluateSuccessResponse{}, fmt.Errorf("static provider failed to list all flags: %w", err)
|
||||
}
|
||||
|
||||
allFlags := make([]OFREPFlag, 0, len(flags))
|
||||
allFlags := make([]goffmodel.OFREPFlagBulkEvaluateSuccessResponse, 0, len(flags))
|
||||
for _, flagKey := range flags {
|
||||
result, err := s.client.BooleanValueDetails(ctx, flagKey, false, openfeature.TransactionContext(ctx))
|
||||
if err != nil {
|
||||
@@ -76,32 +84,18 @@ func (s *staticEvaluator) EvalAllFlags(ctx context.Context) (OFREPBulkResponse,
|
||||
continue
|
||||
}
|
||||
|
||||
allFlags = append(allFlags, OFREPFlag{
|
||||
Key: flagKey,
|
||||
Value: result.Value,
|
||||
Reason: "static provider evaluation result",
|
||||
Variant: result.Variant,
|
||||
allFlags = append(allFlags, goffmodel.OFREPFlagBulkEvaluateSuccessResponse{
|
||||
OFREPEvaluateSuccessResponse: goffmodel.OFREPEvaluateSuccessResponse{
|
||||
Key: flagKey,
|
||||
Value: result.Value,
|
||||
Reason: "static provider evaluation result",
|
||||
Variant: result.Variant,
|
||||
Metadata: result.FlagMetadata,
|
||||
},
|
||||
ErrorCode: string(result.ErrorCode),
|
||||
ErrorDetails: result.ErrorMessage,
|
||||
})
|
||||
}
|
||||
|
||||
return OFREPBulkResponse{Flags: allFlags}, nil
|
||||
}
|
||||
|
||||
// OFREPBulkResponse represents the response for bulk flag evaluation
|
||||
type OFREPBulkResponse struct {
|
||||
Flags []OFREPFlag `json:"flags"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
// OFREPFlag represents a single flag in the bulk response
|
||||
type OFREPFlag struct {
|
||||
Key string `json:"key"`
|
||||
Value any `json:"value"`
|
||||
Reason string `json:"reason"`
|
||||
Variant string `json:"variant,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
ErrorCode string `json:"errorCode,omitempty"`
|
||||
ErrorDetails string `json:"errorDetails,omitempty"`
|
||||
return goffmodel.OFREPBulkEvaluateSuccessResponse{Flags: allFlags}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user