feat: update golang output (#63)

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
<!-- add the description of the PR here -->

- updates the golang output for the flag accessors

---------

Signed-off-by: Florin-Mihai Anghel <fanghel@google.com>
Signed-off-by: Florin-Mihai Anghel <44744433+anghelflorinm@users.noreply.github.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
This commit is contained in:
Florin-Mihai Anghel 2024-12-09 19:25:21 +01:00 committed by GitHub
parent 79b36ddd24
commit 0e7db0209e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 46 deletions

View File

@ -1,3 +1,4 @@
// AUTOMATICALLY GENERATED BY OPENFEATURE CODEGEN, DO NOT EDIT.
package testpackage package testpackage
import ( import (
@ -5,45 +6,85 @@ import (
"github.com/open-feature/go-sdk/openfeature" "github.com/open-feature/go-sdk/openfeature"
) )
type BooleanProvider func(ctx context.Context) (bool, error) type BooleanProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error)
type FloatProvider func(ctx context.Context) (float64, error) type BooleanProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.BooleanEvaluationDetails, error)
type IntProvider func(ctx context.Context) (int64, error) type FloatProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (float64, error)
type StringProvider func(ctx context.Context) (string, error) type FloatProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.FloatEvaluationDetails, error)
type IntProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (int64, error)
type IntProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.IntEvaluationDetails, error)
type StringProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (string, error)
type StringProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.StringEvaluationDetails, error)
var client *openfeature.Client = nil var client openfeature.IClient = nil
// Discount percentage applied to purchases. // Discount percentage applied to purchases.
var DiscountPercentage = struct { var DiscountPercentage = struct {
// Value returns the value of the flag DiscountPercentage,
// as well as the evaluation error, if present.
Value FloatProvider Value FloatProvider
// ValueWithDetails returns the value of the flag DiscountPercentage,
// the evaluation error, if any, and the evaluation details.
ValueWithDetails FloatProviderDetails
}{ }{
Value: func(ctx context.Context) (float64, error) { Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (float64, error) {
return client.FloatValue(ctx, "discountPercentage", 0.15, openfeature.EvaluationContext{}) return client.FloatValue(ctx, "discountPercentage", 0.15, evalCtx)
},
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.FloatEvaluationDetails, error){
return client.FloatValueDetails(ctx, "discountPercentage", 0.15, evalCtx)
}, },
} }
// Controls whether Feature A is enabled. // Controls whether Feature A is enabled.
var EnableFeatureA = struct { var EnableFeatureA = struct {
// Value returns the value of the flag EnableFeatureA,
// as well as the evaluation error, if present.
Value BooleanProvider Value BooleanProvider
// ValueWithDetails returns the value of the flag EnableFeatureA,
// the evaluation error, if any, and the evaluation details.
ValueWithDetails BooleanProviderDetails
}{ }{
Value: func(ctx context.Context) (bool, error) { Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error) {
return client.BooleanValue(ctx, "enableFeatureA", false, openfeature.EvaluationContext{}) return client.BooleanValue(ctx, "enableFeatureA", false, evalCtx)
},
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.BooleanEvaluationDetails, error){
return client.BooleanValueDetails(ctx, "enableFeatureA", false, evalCtx)
}, },
} }
// The message to use for greeting users. // The message to use for greeting users.
var GreetingMessage = struct { var GreetingMessage = struct {
// Value returns the value of the flag GreetingMessage,
// as well as the evaluation error, if present.
Value StringProvider Value StringProvider
// ValueWithDetails returns the value of the flag GreetingMessage,
// the evaluation error, if any, and the evaluation details.
ValueWithDetails StringProviderDetails
}{ }{
Value: func(ctx context.Context) (string, error) { Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (string, error) {
return client.StringValue(ctx, "greetingMessage", "Hello there!", openfeature.EvaluationContext{}) return client.StringValue(ctx, "greetingMessage", "Hello there!", evalCtx)
},
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.StringEvaluationDetails, error){
return client.StringValueDetails(ctx, "greetingMessage", "Hello there!", evalCtx)
}, },
} }
// Maximum allowed length for usernames. // Maximum allowed length for usernames.
var UsernameMaxLength = struct { var UsernameMaxLength = struct {
// Value returns the value of the flag UsernameMaxLength,
// as well as the evaluation error, if present.
Value IntProvider Value IntProvider
// ValueWithDetails returns the value of the flag UsernameMaxLength,
// the evaluation error, if any, and the evaluation details.
ValueWithDetails IntProviderDetails
}{ }{
Value: func(ctx context.Context) (int64, error) { Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (int64, error) {
return client.IntValue(ctx, "usernameMaxLength", 50, openfeature.EvaluationContext{}) return client.IntValue(ctx, "usernameMaxLength", 50, evalCtx)
},
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.IntEvaluationDetails, error){
return client.IntValueDetails(ctx, "usernameMaxLength", 50, evalCtx)
}, },
} }
func init() { func init() {
client = openfeature.NewClient("testpackage") client = openfeature.GetApiInstance().GetClient()
} }

View File

@ -53,32 +53,16 @@ func flagInitParam(flagName string) string {
return strconv.Quote(flagName) return strconv.Quote(flagName)
} }
// flagVarType returns the Go type for a flag's proto definition. func openFeatureType(t types.FlagType) string {
func providerType(t types.FlagType) string {
switch t { switch t {
case types.IntType: case types.IntType:
return "IntProvider" return "Int"
case types.FloatType: case types.FloatType:
return "FloatProvider" return "Float"
case types.BoolType: case types.BoolType:
return "BooleanProvider" return "Boolean"
case types.StringType: case types.StringType:
return "StringProvider" return "String"
default:
return ""
}
}
func flagAccessFunc(t types.FlagType) string {
switch t {
case types.IntType:
return "IntValue"
case types.FloatType:
return "FloatValue"
case types.BoolType:
return "BooleanValue"
case types.StringType:
return "StringValue"
default: default:
return "" return ""
} }
@ -125,8 +109,7 @@ func (g *genImpl) Generate(input types.Input) error {
funcs := template.FuncMap{ funcs := template.FuncMap{
"FlagVarName": flagVarName, "FlagVarName": flagVarName,
"FlagInitParam": flagInitParam, "FlagInitParam": flagInitParam,
"ProviderType": providerType, "OpenFeatureType": openFeatureType,
"FlagAccessFunc": flagAccessFunc,
"SupportImports": supportImports, "SupportImports": supportImports,
"DefaultValueLiteral": defaultValueLiteral, "DefaultValueLiteral": defaultValueLiteral,
"TypeString": typeString, "TypeString": typeString,

View File

@ -1,3 +1,4 @@
// AUTOMATICALLY GENERATED BY OPENFEATURE CODEGEN, DO NOT EDIT.
package {{.GoPackage}} package {{.GoPackage}}
import ( import (
@ -6,25 +7,38 @@ import (
{{- end}} {{- end}}
) )
type BooleanProvider func(ctx context.Context) (bool, error) type BooleanProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error)
type FloatProvider func(ctx context.Context) (float64, error) type BooleanProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.BooleanEvaluationDetails, error)
type IntProvider func(ctx context.Context) (int64, error) type FloatProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (float64, error)
type StringProvider func(ctx context.Context) (string, error) type FloatProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.FloatEvaluationDetails, error)
type IntProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (int64, error)
type IntProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.IntEvaluationDetails, error)
type StringProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (string, error)
type StringProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.StringEvaluationDetails, error)
var client *openfeature.Client = nil var client openfeature.IClient = nil
{{- range .Flags}} {{- range .Flags}}
// {{.Docs}} // {{.Docs}}
var {{FlagVarName .Name}} = struct { var {{FlagVarName .Name}} = struct {
Value {{ProviderType .Type}} // Value returns the value of the flag {{FlagVarName .Name}},
// as well as the evaluation error, if present.
Value {{OpenFeatureType .Type}}Provider
// ValueWithDetails returns the value of the flag {{FlagVarName .Name}},
// the evaluation error, if any, and the evaluation details.
ValueWithDetails {{OpenFeatureType .Type}}ProviderDetails
}{ }{
Value: func(ctx context.Context) ({{TypeString .Type}}, error) { Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) ({{TypeString .Type}}, error) {
return client.{{FlagAccessFunc .Type}}(ctx, {{FlagInitParam .Name}}, {{DefaultValueLiteral .}}, openfeature.EvaluationContext{}) return client.{{OpenFeatureType .Type}}Value(ctx, {{FlagInitParam .Name}}, {{DefaultValueLiteral .}}, evalCtx)
},
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.{{OpenFeatureType .Type}}EvaluationDetails, error){
return client.{{OpenFeatureType .Type}}ValueDetails(ctx, {{FlagInitParam .Name}}, {{DefaultValueLiteral .}}, evalCtx)
}, },
} }
{{- end}} {{- end}}
func init() { func init() {
client = openfeature.NewClient("{{.GoPackage}}") client = openfeature.GetApiInstance().GetClient()
} }