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
import (
@ -5,45 +6,85 @@ import (
"github.com/open-feature/go-sdk/openfeature"
)
type BooleanProvider func(ctx context.Context) (bool, error)
type FloatProvider func(ctx context.Context) (float64, error)
type IntProvider func(ctx context.Context) (int64, error)
type StringProvider func(ctx context.Context) (string, error)
type BooleanProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error)
type BooleanProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.BooleanEvaluationDetails, error)
type FloatProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (float64, 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.
var DiscountPercentage = struct {
// Value returns the value of the flag DiscountPercentage,
// as well as the evaluation error, if present.
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) {
return client.FloatValue(ctx, "discountPercentage", 0.15, openfeature.EvaluationContext{})
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (float64, error) {
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.
var EnableFeatureA = struct {
// Value returns the value of the flag EnableFeatureA,
// as well as the evaluation error, if present.
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) {
return client.BooleanValue(ctx, "enableFeatureA", false, openfeature.EvaluationContext{})
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error) {
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.
var GreetingMessage = struct {
// Value returns the value of the flag GreetingMessage,
// as well as the evaluation error, if present.
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) {
return client.StringValue(ctx, "greetingMessage", "Hello there!", openfeature.EvaluationContext{})
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (string, error) {
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.
var UsernameMaxLength = struct {
// Value returns the value of the flag UsernameMaxLength,
// as well as the evaluation error, if present.
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) {
return client.IntValue(ctx, "usernameMaxLength", 50, openfeature.EvaluationContext{})
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (int64, error) {
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() {
client = openfeature.NewClient("testpackage")
client = openfeature.GetApiInstance().GetClient()
}

View File

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

View File

@ -1,3 +1,4 @@
// AUTOMATICALLY GENERATED BY OPENFEATURE CODEGEN, DO NOT EDIT.
package {{.GoPackage}}
import (
@ -6,25 +7,38 @@ import (
{{- end}}
)
type BooleanProvider func(ctx context.Context) (bool, error)
type FloatProvider func(ctx context.Context) (float64, error)
type IntProvider func(ctx context.Context) (int64, error)
type StringProvider func(ctx context.Context) (string, error)
type BooleanProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error)
type BooleanProviderDetails func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.BooleanEvaluationDetails, error)
type FloatProvider func(ctx context.Context, evalCtx openfeature.EvaluationContext) (float64, 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}}
// {{.Docs}}
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) {
return client.{{FlagAccessFunc .Type}}(ctx, {{FlagInitParam .Name}}, {{DefaultValueLiteral .}}, openfeature.EvaluationContext{})
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) ({{TypeString .Type}}, error) {
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}}
func init() {
client = openfeature.NewClient("{{.GoPackage}}")
client = openfeature.GetApiInstance().GetClient()
}