Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <[email protected]>
  • Loading branch information
Kavindu-Dodan committed Jul 21, 2023
1 parent 2d9ec68 commit 4d651da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 63 deletions.
42 changes: 9 additions & 33 deletions e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,17 @@ var ctxFunction = func(this mp.InMemoryFlag, evalCtx openfeature.FlattenedContex
Variant: this.DefaultVariant,
}

// check for expected properties
fn, ok := evalCtx["fn"].(string)
if !ok {
return defaultValue, defaultResolution
expects := openfeature.FlattenedContext{
"fn": "Sulisław",
"ln": "Świętopełk",
"age": int64(29),
"customer": false,
}

if fn != "Sulisław" {
return defaultValue, defaultResolution
}

ln, ok := evalCtx["ln"].(string)
if !ok {
return defaultValue, defaultResolution
}

if ln != "Świętopełk" {
return defaultValue, defaultResolution
}

age, ok := evalCtx["age"].(int64)
if !ok {
return defaultValue, defaultResolution
}

if age != 29 {
return defaultValue, defaultResolution
}

customer, ok := evalCtx["customer"].(bool)
if !ok {
return defaultValue, defaultResolution
}

if customer != false {
return defaultValue, defaultResolution
for k, v := range expects {
if v != evalCtx[k] {
return defaultValue, defaultResolution
}
}

return this.Variants["internal"], openfeature.ProviderResolutionDetail{
Expand Down
58 changes: 28 additions & 30 deletions pkg/openfeature/testing/in_memory_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ func (i InMemoryProvider) Metadata() openfeature.Metadata {
}

func (i InMemoryProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx openfeature.FlattenedContext) openfeature.BoolResolutionDetail {
memoryFlag, ok := i.flags[flag]
memoryFlag, details, ok := i.find(flag)
if !ok {
return openfeature.BoolResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewFlagNotFoundResolutionError(fmt.Sprintf("flag for key %s not found", flag)),
Reason: openfeature.ErrorReason,
},
Value: defaultValue,
ProviderResolutionDetail: *details,
}
}

Expand All @@ -49,14 +46,11 @@ func (i InMemoryProvider) BooleanEvaluation(ctx context.Context, flag string, de
}

func (i InMemoryProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, evalCtx openfeature.FlattenedContext) openfeature.StringResolutionDetail {
memoryFlag, ok := i.flags[flag]
memoryFlag, details, ok := i.find(flag)
if !ok {
return openfeature.StringResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewFlagNotFoundResolutionError(fmt.Sprintf("flag for key %s not found", flag)),
Reason: openfeature.ErrorReason,
},
Value: defaultValue,
ProviderResolutionDetail: *details,
}
}

Expand All @@ -70,14 +64,11 @@ func (i InMemoryProvider) StringEvaluation(ctx context.Context, flag string, def
}

func (i InMemoryProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx openfeature.FlattenedContext) openfeature.FloatResolutionDetail {
memoryFlag, ok := i.flags[flag]
memoryFlag, details, ok := i.find(flag)
if !ok {
return openfeature.FloatResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewFlagNotFoundResolutionError(fmt.Sprintf("flag for key %s not found", flag)),
Reason: openfeature.ErrorReason,
},
Value: defaultValue,
ProviderResolutionDetail: *details,
}
}

Expand All @@ -91,14 +82,11 @@ func (i InMemoryProvider) FloatEvaluation(ctx context.Context, flag string, defa
}

func (i InMemoryProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx openfeature.FlattenedContext) openfeature.IntResolutionDetail {
memoryFlag, ok := i.flags[flag]
memoryFlag, details, ok := i.find(flag)
if !ok {
return openfeature.IntResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewFlagNotFoundResolutionError(fmt.Sprintf("flag for key %s not found", flag)),
Reason: openfeature.ErrorReason,
},
Value: defaultValue,
ProviderResolutionDetail: *details,
}
}

Expand All @@ -112,14 +100,11 @@ func (i InMemoryProvider) IntEvaluation(ctx context.Context, flag string, defaul
}

func (i InMemoryProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, evalCtx openfeature.FlattenedContext) openfeature.InterfaceResolutionDetail {
memoryFlag, ok := i.flags[flag]
memoryFlag, details, ok := i.find(flag)
if !ok {
return openfeature.InterfaceResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewFlagNotFoundResolutionError(fmt.Sprintf("flag for key %s not found", flag)),
Reason: openfeature.ErrorReason,
},
Value: defaultValue,
ProviderResolutionDetail: *details,
}
}

Expand All @@ -144,6 +129,19 @@ func (i InMemoryProvider) Hooks() []openfeature.Hook {
return []openfeature.Hook{}
}

func (i InMemoryProvider) find(flag string) (*InMemoryFlag, *openfeature.ProviderResolutionDetail, bool) {
memoryFlag, ok := i.flags[flag]
if !ok {
return nil,
&openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewFlagNotFoundResolutionError(fmt.Sprintf("flag for key %s not found", flag)),
Reason: openfeature.ErrorReason,
}, false
}

return &memoryFlag, nil, true
}

// helpers

// genericResolve is a helper to extract type verified evaluation and fill openfeature.ProviderResolutionDetail
Expand Down

0 comments on commit 4d651da

Please sign in to comment.