Skip to content

Generate Stringers #109

@mogsie

Description

@mogsie

When working with feature flags, it's sometimes necessary to access the original feature flag key buried inside the generated code.

Here are some examples:

  1. In our test cases, we use environment variables to control flags, and we have to duplicate the feature flag name to mimic the uppercase'd underscored mapping ("FEATURE_MY_FEATURE").
  2. For prometheus, you have to set up the valid metrics at initialization time, well before evaluating any metrics
  3. You might also provide command-line flags to set features. For those you need information about (at least) the name of the flag.

It would be great if the generated code had the ability to access this original string, without having to e.g. evaluate the flag.

In go, it could be something like this:

// stringer transforms a string to a Stringer
type stringer string

// String implements the fmt.Stringer interface
func (s stringer) String() string {
	return string(s)
}

var MyCoolFeature = struct {
	fmt.Stringer
	Value BooleanProvider
	ValueWithDetails BooleanProviderDetails
}{
	Stringer: stringer("my-cool-feature"),
	Value: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (bool, error) {
		return client.BooleanValue(ctx, "my-cool-feature", false, evalCtx)
	},
	ValueWithDetails: func(ctx context.Context, evalCtx openfeature.EvaluationContext) (openfeature.BooleanEvaluationDetails, error) {
		return client.BooleanValueDetails(ctx, "my-cool-feature", false, evalCtx)
	},
}

The caller can then know the original key of a provider by stringing it.

MyCoolFeature.String()

And from there, we could do things like craft environment variables, or command line flags.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions