-
Notifications
You must be signed in to change notification settings - Fork 20
feat(generators): expose underlying OpenFeature client on generated clients #236
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,9 @@ type ( | |
| evaluationDetails[T any] func(context.Context, openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[T], error) | ||
| ) | ||
|
|
||
| var client = openfeature.NewDefaultClient() | ||
| // Client is the underlying OpenFeature client used for flag evaluations. | ||
| // It can be used directly for ad-hoc flag evaluations beyond what is defined in the manifest. | ||
| var Client = openfeature.NewDefaultClient() | ||
|
|
||
| {{- range .Flagset.Flags }} | ||
| // {{ .Key | ToPascal }} returns the value of the "{{ .Key }}" feature flag. | ||
|
|
@@ -41,10 +43,10 @@ var {{ .Key | ToPascal }} = struct { | |
| }{ | ||
| Stringer: stringer({{ .Key | Quote }}), | ||
| Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) {{- if eq (.Type | OpenFeatureType) "Object"}}any{{- else}}{{ .Type | TypeString }}{{- end}} { | ||
| return client.{{ .Type | OpenFeatureType }}(ctx, {{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "Object" }}{{.DefaultValue | ToMapLiteral }}{{- else }}{{ .DefaultValue | QuoteString }}{{- end}}, evalCtx) | ||
| return Client.{{ .Type | OpenFeatureType }}(ctx, {{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "Object" }}{{.DefaultValue | ToMapLiteral }}{{- else }}{{ .DefaultValue | QuoteString }}{{- end}}, evalCtx) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this reference to match the suggested name change for the exported client variable. |
||
| }, | ||
| ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.GenericEvaluationDetails[{{- if eq (.Type | OpenFeatureType) "Object"}}any{{- else}}{{ .Type | TypeString }}{{- end}}], error){ | ||
| return client.{{ .Type | OpenFeatureType }}ValueDetails(ctx, {{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "Object" }}{{.DefaultValue | ToMapLiteral }}{{- else }}{{ .DefaultValue | QuoteString }}{{- end}}, evalCtx) | ||
| return Client.{{ .Type | OpenFeatureType }}ValueDetails(ctx, {{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "Object" }}{{.DefaultValue | ToMapLiteral }}{{- else }}{{ .DefaultValue | QuoteString }}{{- end}}, evalCtx) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this reference to match the suggested name change for the exported client variable. |
||
| }, | ||
| } | ||
| {{- end}} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { | |
| JsonValue, | ||
| } from "@openfeature/server-sdk"; | ||
| import type { | ||
| Client, | ||
| EvaluationContext, | ||
| EvaluationDetails, | ||
| FlagEvaluationOptions, | ||
|
|
@@ -20,6 +21,8 @@ export const FlagKeys = { | |
| } as const; | ||
|
|
||
| export interface GeneratedClient { | ||
| /** The underlying OpenFeature client for ad-hoc flag evaluations. */ | ||
| readonly client: Client; | ||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a |
||
| {{- range .Flagset.Flags }} | ||
| /** | ||
| * {{ if .Description }}{{ .Description }}{{ else }}Feature flag{{ end }} | ||
|
|
@@ -82,6 +85,7 @@ export function getGeneratedClient(domainOrContext?: string | EvaluationContext, | |
| const client = domain ? OpenFeature.getClient(domain, context) : OpenFeature.getClient(context) | ||
|
|
||
| return { | ||
| client, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| {{- range .Flagset.Flags }} | ||
| {{ .Key | ToCamel }}: (context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<{{ if eq (.Type | OpenFeatureType) "object" }}JsonValue{{ else }}{{ .Type | OpenFeatureType }}{{ end }}> => { | ||
| return client.get{{ .Type | OpenFeatureType | ToPascal }}Value({{ .Key | Quote }}, {{ if eq (.Type | OpenFeatureType) "object"}}{{ .DefaultValue | ToJSONString }}{{ else }}{{ .DefaultValue | QuoteString }}{{ end }}, context, options); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,6 +105,9 @@ func run() error { | |
| } | ||
| fmt.Printf("themeCustomization: %v\n", themeCustomization) | ||
|
|
||
| // Verify the underlying client is accessible for ad-hoc evaluations | ||
| _ = generated.Client | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // Test the String() method functionality for all flags | ||
| fmt.Printf("enableFeatureA flag key: %s\n", generated.EnableFeatureA.String()) | ||
| fmt.Printf("discountPercentage flag key: %s\n", generated.DiscountPercentage.String()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,12 @@ async function main() { | |
| const { getGeneratedClient } = await import(clientPath); | ||
| const client = getGeneratedClient(); | ||
|
|
||
| // Verify the underlying client is accessible | ||
| if (!client.client) { | ||
| throw new Error('Underlying OpenFeature client not exposed'); | ||
| } | ||
|
Comment on lines
+65
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| console.log('✅ Underlying client accessible'); | ||
|
|
||
| console.log('🧪 Testing flags...'); | ||
|
|
||
| // Test each flag | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Clientas the name for the exported package-level variable may cause a naming collision if a feature flag in the manifest is also namedclient. When transformed viaToPascal, the flag variable would also be namedClient. Consider using a more distinct name likeOpenFeatureClientto avoid this risk.