Skip to content

Commit 5e93375

Browse files
committed
update all the other things with new changes
Signed-off-by: Roman Dmytrenko <[email protected]>
1 parent 212970c commit 5e93375

File tree

4 files changed

+39
-158
lines changed

4 files changed

+39
-158
lines changed

internal/cmd/testdata/success_go.golden

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func (s stringer) String() string {
2020
}
2121

2222
type (
23-
evaluationValue[T any] func(context.Context, openfeature.EvaluationContext) (T, error)
23+
evaluationValue[T any] func(context.Context, openfeature.EvaluationContext) T
2424
evaluationDetails[T any] func(context.Context, openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[T], error)
2525
)
2626

27-
var client openfeature.IClient = nil
27+
var client = openfeature.NewDefaultClient()
2828

2929
// DiscountPercentage - Discount percentage applied to purchases.
3030
var DiscountPercentage = struct {
@@ -38,8 +38,8 @@ var DiscountPercentage = struct {
3838
ValueWithDetails evaluationDetails[float64]
3939
}{
4040
Stringer: stringer("discountPercentage"),
41-
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (float64, error) {
42-
return client.FloatValue(ctx, "discountPercentage", 0.15, evalCtx)
41+
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) float64 {
42+
return client.Float(ctx, "discountPercentage", 0.15, evalCtx)
4343
},
4444
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[float64], error) {
4545
return client.FloatValueDetails(ctx, "discountPercentage", 0.15, evalCtx)
@@ -58,8 +58,8 @@ var EnableFeatureA = struct {
5858
ValueWithDetails evaluationDetails[bool]
5959
}{
6060
Stringer: stringer("enableFeatureA"),
61-
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error) {
62-
return client.BooleanValue(ctx, "enableFeatureA", false, evalCtx)
61+
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) bool {
62+
return client.Boolean(ctx, "enableFeatureA", false, evalCtx)
6363
},
6464
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[bool], error) {
6565
return client.BooleanValueDetails(ctx, "enableFeatureA", false, evalCtx)
@@ -78,8 +78,8 @@ var GreetingMessage = struct {
7878
ValueWithDetails evaluationDetails[string]
7979
}{
8080
Stringer: stringer("greetingMessage"),
81-
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (string, error) {
82-
return client.StringValue(ctx, "greetingMessage", "Hello there!", evalCtx)
81+
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) string {
82+
return client.String(ctx, "greetingMessage", "Hello there!", evalCtx)
8383
},
8484
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[string], error) {
8585
return client.StringValueDetails(ctx, "greetingMessage", "Hello there!", evalCtx)
@@ -98,8 +98,8 @@ var ThemeCustomization = struct {
9898
ValueWithDetails evaluationDetails[any]
9999
}{
100100
Stringer: stringer("themeCustomization"),
101-
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (any, error) {
102-
return client.ObjectValue(ctx, "themeCustomization", map[string]any{"primaryColor": "#007bff", "secondaryColor": "#6c757d"}, evalCtx)
101+
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) any {
102+
return client.Object(ctx, "themeCustomization", map[string]any{"primaryColor": "#007bff", "secondaryColor": "#6c757d"}, evalCtx)
103103
},
104104
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[any], error) {
105105
return client.ObjectValueDetails(ctx, "themeCustomization", map[string]any{"primaryColor": "#007bff", "secondaryColor": "#6c757d"}, evalCtx)
@@ -118,14 +118,10 @@ var UsernameMaxLength = struct {
118118
ValueWithDetails evaluationDetails[int64]
119119
}{
120120
Stringer: stringer("usernameMaxLength"),
121-
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (int64, error) {
122-
return client.IntValue(ctx, "usernameMaxLength", 50, evalCtx)
121+
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) int64 {
122+
return client.Int(ctx, "usernameMaxLength", 50, evalCtx)
123123
},
124124
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[int64], error) {
125125
return client.IntValueDetails(ctx, "usernameMaxLength", 50, evalCtx)
126126
},
127127
}
128-
129-
func init() {
130-
client = openfeature.NewDefaultClient()
131-
}

internal/generators/golang/golang.tmpl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (s stringer) String() string {
1919
}
2020

2121
type (
22-
evaluationValue[T any] func(context.Context, openfeature.EvaluationContext) (T, error)
22+
evaluationValue[T any] func(context.Context, openfeature.EvaluationContext) T
2323
evaluationDetails[T any] func(context.Context, openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[T], error)
2424
)
2525

@@ -38,15 +38,11 @@ var {{ .Key | ToPascal }} = struct {
3838
ValueWithDetails evaluationDetails[{{- if eq (.Type | OpenFeatureType) "Object"}}any{{- else}}{{ .Type | TypeString }}{{- end}}]
3939
}{
4040
Stringer: stringer({{ .Key | Quote }}),
41-
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) ({{- if eq (.Type | OpenFeatureType) "Object"}}any{{- else}}{{ .Type | TypeString }}{{- end}}, error) {
42-
return client.{{ .Type | OpenFeatureType }}Value(ctx, {{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "Object" }}{{.DefaultValue | ToMapLiteral }}{{- else }}{{ .DefaultValue | QuoteString }}{{- end}}, evalCtx)
41+
Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) {{- if eq (.Type | OpenFeatureType) "Object"}}any{{- else}}{{ .Type | TypeString }}{{- end}} {
42+
return client.{{ .Type | OpenFeatureType }}(ctx, {{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "Object" }}{{.DefaultValue | ToMapLiteral }}{{- else }}{{ .DefaultValue | QuoteString }}{{- end}}, evalCtx)
4343
},
4444
ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[{{- if eq (.Type | OpenFeatureType) "Object"}}any{{- else}}{{ .Type | TypeString }}{{- end}}], error){
4545
return client.{{ .Type | OpenFeatureType }}ValueDetails(ctx, {{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "Object" }}{{.DefaultValue | ToMapLiteral }}{{- else }}{{ .DefaultValue | QuoteString }}{{- end}}, evalCtx)
4646
},
4747
}
4848
{{- end}}
49-
50-
func init() {
51-
client = openfeature.NewDefaultClient()
52-
}

test/go-integration/openfeature/openfeature.go

Lines changed: 0 additions & 128 deletions
This file was deleted.

test/go-integration/test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,55 @@ func run() error {
6363
// Set the provider and wait for it to be ready
6464
err := openfeature.SetProviderAndWait(provider)
6565
if err != nil {
66-
return fmt.Errorf("Failed to set provider: %w", err)
66+
return fmt.Errorf("failed to set provider: %w", err)
6767
}
6868

6969
ctx := context.Background()
7070
evalCtx := openfeature.NewEvaluationContext("someid", map[string]any{})
7171

7272
// Use the generated code for all flag evaluations
73-
enableFeatureA, err := generated.EnableFeatureA.Value(ctx, evalCtx)
73+
enableFeatureA := generated.EnableFeatureA.Value(ctx, evalCtx)
74+
if enableFeatureA != false {
75+
return fmt.Errorf("error evaluating %s flag. want: %v, got: %v", generated.EnableFeatureA, false, enableFeatureA)
76+
}
77+
_, err = generated.EnableFeatureA.ValueWithDetails(ctx, evalCtx)
7478
if err != nil {
7579
return fmt.Errorf("Error evaluating boolean flag: %w", err)
7680
}
7781
fmt.Printf("enableFeatureA: %v\n", enableFeatureA)
7882

79-
discount, err := generated.DiscountPercentage.Value(ctx, evalCtx)
83+
discount := generated.DiscountPercentage.Value(ctx, evalCtx)
84+
if discount != 0.15 {
85+
return fmt.Errorf("error evaluating %s flag. want: %v, got: %v", generated.DiscountPercentage, 0.15, discount)
86+
}
87+
_, err = generated.DiscountPercentage.ValueWithDetails(ctx, evalCtx)
8088
if err != nil {
8189
return fmt.Errorf("Failed to get discount: %w", err)
8290
}
8391
fmt.Printf("Discount Percentage: %.2f\n", discount)
8492

85-
greetingMessage, err := generated.GreetingMessage.Value(ctx, evalCtx)
93+
greetingMessage := generated.GreetingMessage.Value(ctx, evalCtx)
94+
if greetingMessage != "Hello there!" {
95+
return fmt.Errorf("error evaluating %s flag. want: %v, got: %v", generated.GreetingMessage, "Hello there!", greetingMessage)
96+
}
97+
_, err = generated.GreetingMessage.ValueWithDetails(ctx, evalCtx)
8698
if err != nil {
8799
return fmt.Errorf("Error evaluating string flag: %w", err)
88100
}
89101
fmt.Printf("greetingMessage: %v\n", greetingMessage)
90102

91-
usernameMaxLength, err := generated.UsernameMaxLength.Value(ctx, evalCtx)
103+
usernameMaxLength := generated.UsernameMaxLength.Value(ctx, evalCtx)
104+
if usernameMaxLength != 50 {
105+
return fmt.Errorf("error evaluating %s flag. want: %v, got: %v", generated.UsernameMaxLength, 50, usernameMaxLength)
106+
}
107+
_, err = generated.UsernameMaxLength.ValueWithDetails(ctx, evalCtx)
92108
if err != nil {
93109
return fmt.Errorf("Error evaluating int flag: %v\n", err)
94110
}
95111
fmt.Printf("usernameMaxLength: %v\n", usernameMaxLength)
96112

97-
themeCustomization, err := generated.ThemeCustomization.Value(ctx, evalCtx)
113+
themeCustomization := generated.ThemeCustomization.Value(ctx, evalCtx)
114+
_, err = generated.ThemeCustomization.ValueWithDetails(ctx, evalCtx)
98115
if err != nil {
99116
return fmt.Errorf("Error evaluating int flag: %v\n", err)
100117
}
@@ -108,6 +125,6 @@ func run() error {
108125
fmt.Printf("themeCustomization flag key: %s\n", generated.ThemeCustomization.String())
109126

110127
fmt.Println("Generated Go code compiles successfully!")
111-
128+
112129
return nil
113130
}

0 commit comments

Comments
 (0)