Skip to content

feat: introduce OFREP provider #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"providers/unleash": "0.0.3-alpha",
"providers/harness": "0.0.4-alpha",
"providers/statsig": "0.0.2",
"providers/ofrep": "0.0.0",
"tests/flagd": "1.4.1"
}
50 changes: 50 additions & 0 deletions providers/ofrep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# OpenFeature Remote Evaluation Protocol Provider

This is the Go implementation of the OFREP provider.
The provider works by evaluating flags against OFREP single flag evaluation endpoint.

## Installation

Use OFREP provider with the latest OpenFeature Go SDK

```sh
go get github.com/open-feature/go-sdk-contrib/providers/ofrep
go get github.com/open-feature/go-sdk
```

## Usage

Initialize the provider with the URL of the OFREP implementing service,

```go
ofrepProvider := ofrep.NewProvider("http://localhost:8016")
```

Then, register the provider with the OpenFeature Go SDK and use derived clients for flag evaluations,

```go
openfeature.SetProvider(ofrepProvider)
```

## Configuration

You can configure the provider using following configuration options,

| Configuration option | Details |
|----------------------|-------------------------------------------------------------------------------------------------------------------------|
| WithApiKeyAuth | Set the token to be used with "X-API-Key" header |
| WithBearerToken | Set the token to be used with "Bearer" HTTP Authorization schema |
| WithClient | Provider a custom, pre-configured http.Client for OFREP service communication |
| WithHeaderProvider | Register a custom header provider for OFREP calls. You may utilize this for custom authentication/authorization headers |


For example, consider below example which set bearer token and provider a customized http client,

```go
provider := ofrep.NewProvider(
"http://localhost:8016",
ofrep.WithBearerToken("TOKEN"),
ofrep.WithClient(&http.Client{
Timeout: 1 * time.Second,
}))
```
21 changes: 21 additions & 0 deletions providers/ofrep/evaluate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ofrep

import (
"context"

of "github.com/open-feature/go-sdk/openfeature"
)

// Evaluator contract for flag evaluation
type Evaluator interface {
ResolveBoolean(ctx context.Context, key string, defaultValue bool,
evalCtx map[string]interface{}) of.BoolResolutionDetail
ResolveString(ctx context.Context, key string, defaultValue string,
evalCtx map[string]interface{}) of.StringResolutionDetail
ResolveFloat(ctx context.Context, key string, defaultValue float64,
evalCtx map[string]interface{}) of.FloatResolutionDetail
ResolveInt(ctx context.Context, key string, defaultValue int64,
evalCtx map[string]interface{}) of.IntResolutionDetail
ResolveObject(ctx context.Context, key string, defaultValue interface{},
evalCtx map[string]interface{}) of.InterfaceResolutionDetail
}
10 changes: 10 additions & 0 deletions providers/ofrep/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/open-feature/go-sdk-contrib/providers/ofrep

go 1.21.0

require github.com/open-feature/go-sdk v1.10.0

require (
github.com/go-logr/logr v1.4.1 // indirect
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect
)
10 changes: 10 additions & 0 deletions providers/ofrep/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/open-feature/go-sdk v1.10.0 h1:druQtYOrN+gyz3rMsXp0F2jW1oBXJb0V26PVQnUGLbM=
github.com/open-feature/go-sdk v1.10.0/go.mod h1:+rkJhLBtYsJ5PZNddAgFILhRAAxwrJ32aU7UEUm4zQI=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 h1:/RIbNt/Zr7rVhIkQhooTxCxFcdWLGIKnZA4IXNFSrvo=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
194 changes: 194 additions & 0 deletions providers/ofrep/internal/evaluate/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package evaluate

import (
"context"
"fmt"

"github.com/open-feature/go-sdk-contrib/providers/ofrep/internal/outbound"
of "github.com/open-feature/go-sdk/openfeature"
)

// Flags is the flag evaluator implementation. It contains domain logic of the OpenFeature flag evaluation.
type Flags struct {
resolver resolver
}

type resolver interface {
resolveSingle(ctx context.Context, key string, evalCtx map[string]interface{}) (*successDto, *of.ResolutionError)
}

func NewFlagsEvaluator(cfg outbound.Configuration) *Flags {
return &Flags{
resolver: NewOutboundResolver(cfg),
}
}

func (h Flags) ResolveBoolean(ctx context.Context, key string, defaultValue bool, evalCtx map[string]interface{}) of.BoolResolutionDetail {
evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx)
if resolutionError != nil {
return of.BoolResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: *resolutionError,
Reason: of.ErrorReason,
},
}
}

b, ok := evalSuccess.Value.(bool)
if !ok {
return of.BoolResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf(
"resolved value %v is not of boolean type", evalSuccess.Value)),
Reason: of.ErrorReason,
},
}
}

return of.BoolResolutionDetail{
Value: b,
ProviderResolutionDetail: of.ProviderResolutionDetail{
Reason: of.Reason(evalSuccess.Reason),
Variant: evalSuccess.Variant,
FlagMetadata: evalSuccess.Metadata,
},
}
}

func (h Flags) ResolveString(ctx context.Context, key string, defaultValue string, evalCtx map[string]interface{}) of.StringResolutionDetail {
evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx)
if resolutionError != nil {
return of.StringResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: *resolutionError,
Reason: of.ErrorReason,
},
}
}

b, ok := evalSuccess.Value.(string)
if !ok {
return of.StringResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf(
"resolved value %v is not of string type", evalSuccess.Value)),
Reason: of.ErrorReason,
},
}
}

return of.StringResolutionDetail{
Value: b,
ProviderResolutionDetail: of.ProviderResolutionDetail{
Reason: of.Reason(evalSuccess.Reason),
Variant: evalSuccess.Variant,
FlagMetadata: evalSuccess.Metadata,
},
}
}

func (h Flags) ResolveFloat(ctx context.Context, key string, defaultValue float64, evalCtx map[string]interface{}) of.FloatResolutionDetail {
evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx)
if resolutionError != nil {
return of.FloatResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: *resolutionError,
Reason: of.ErrorReason,
},
}
}

var value float64

switch evalSuccess.Value.(type) {
case float32:
value = float64(evalSuccess.Value.(float32))
case float64:
value = evalSuccess.Value.(float64)
default:
return of.FloatResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf(
"resolved value %v is not of float type", evalSuccess.Value)),
Reason: of.ErrorReason,
},
}
}

return of.FloatResolutionDetail{
Value: value,
ProviderResolutionDetail: of.ProviderResolutionDetail{
Reason: of.Reason(evalSuccess.Reason),
Variant: evalSuccess.Variant,
FlagMetadata: evalSuccess.Metadata,
},
}
}

func (h Flags) ResolveInt(ctx context.Context, key string, defaultValue int64, evalCtx map[string]interface{}) of.IntResolutionDetail {
evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx)
if resolutionError != nil {
return of.IntResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: *resolutionError,
Reason: of.ErrorReason,
},
}
}

var value int64

switch evalSuccess.Value.(type) {
case int:
value = int64(evalSuccess.Value.(int))
case int64:
value = evalSuccess.Value.(int64)
default:
return of.IntResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: of.NewTypeMismatchResolutionError(fmt.Sprintf(
"resolved value %v is not of integer type", evalSuccess.Value)),
Reason: of.ErrorReason,
},
}
}

return of.IntResolutionDetail{
Value: value,
ProviderResolutionDetail: of.ProviderResolutionDetail{
Reason: of.Reason(evalSuccess.Reason),
Variant: evalSuccess.Variant,
FlagMetadata: evalSuccess.Metadata,
},
}
}

func (h Flags) ResolveObject(ctx context.Context, key string, defaultValue interface{}, evalCtx map[string]interface{}) of.InterfaceResolutionDetail {
evalSuccess, resolutionError := h.resolver.resolveSingle(ctx, key, evalCtx)
if resolutionError != nil {
return of.InterfaceResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: of.ProviderResolutionDetail{
ResolutionError: *resolutionError,
Reason: of.ErrorReason,
},
}
}

return of.InterfaceResolutionDetail{
Value: evalSuccess.Value,
ProviderResolutionDetail: of.ProviderResolutionDetail{
Reason: of.Reason(evalSuccess.Reason),
Variant: evalSuccess.Variant,
FlagMetadata: evalSuccess.Metadata,
},
}
}
Loading