-
Notifications
You must be signed in to change notification settings - Fork 2
feat: scaffold OpenFeature Go SDK feature-flag support #146
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| module github.com/devantler-tech/go-template | ||
|
|
||
| go 1.25.10 | ||
|
|
||
| require github.com/open-feature/go-sdk v1.17.2 | ||
|
|
||
| require ( | ||
| github.com/go-logr/logr v1.4.3 // indirect | ||
| go.uber.org/mock v0.6.0 // indirect | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= | ||
| github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= | ||
| github.com/open-feature/go-sdk v1.17.2 h1:pTdeNks/hgnPrlqdgtFwltnIron1oOxqg4FmLlirJlY= | ||
| github.com/open-feature/go-sdk v1.17.2/go.mod h1:kTMCquVtck18XdSCI6rBoNFEBLvkOy4Tphu2pV8bq34= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= | ||
| github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= | ||
| go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= | ||
| go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= | ||
| golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8= | ||
| golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Package featureflag is a minimal, replaceable feature-flag scaffold. | ||
| // | ||
| // It wires the portable OpenFeature Go SDK (github.com/open-feature/go-sdk) as | ||
| // the runtime flag API so every new feature can be landed behind a flag, | ||
| // default-off, and flipped on only after validation — the feature-flag-first | ||
| // delivery convention (see AGENTS.md ## Maintenance). | ||
| // | ||
| // The template ships the in-memory provider (openfeature/memprovider) so the | ||
| // example flag evaluates and its tests pass with no running backend. In a real | ||
| // service, swap the provider for flagd | ||
| // (github.com/open-feature/go-sdk-contrib/providers/flagd) to read FeatureFlag | ||
| // custom resources from the GitOps platform, or a managed backend — call sites | ||
| // (Enabled below) are unchanged because they only touch the OpenFeature client. | ||
| // | ||
| // Lifecycle: short-lived *release* flags are removed after rollout (flag debt | ||
| // is the #1 failure mode); long-lived *ops/permission* flags are the exception. | ||
| // Delete this package and its example flag when you add your own first feature. | ||
| package featureflag | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/open-feature/go-sdk/openfeature" | ||
| "github.com/open-feature/go-sdk/openfeature/memprovider" | ||
| ) | ||
|
|
||
| // ExampleFlag is a sample boolean flag key, evaluated default-off. | ||
| // | ||
| // It gates ExampleFeature below purely to demonstrate the both-states pattern; | ||
| // replace it with your own feature's key. | ||
| const ExampleFlag = "example-feature" | ||
|
|
||
| // NewProvider builds an OpenFeature provider that serves the given boolean flag | ||
| // states in-process — the template's default so the example evaluates and its | ||
| // tests pass with no running backend. | ||
| // | ||
| // In a real service, replace this with the flagd provider (or a managed | ||
| // backend); call sites are unaffected because they only touch the client. | ||
| func NewProvider(flags map[string]bool) memprovider.InMemoryProvider { | ||
| memFlags := make(map[string]memprovider.InMemoryFlag, len(flags)) | ||
|
|
||
| for key, on := range flags { | ||
| variant := "off" | ||
| if on { | ||
| variant = "on" | ||
| } | ||
|
|
||
| memFlags[key] = memprovider.InMemoryFlag{ | ||
| State: memprovider.Enabled, | ||
| DefaultVariant: variant, | ||
| Variants: map[string]any{"on": true, "off": false}, | ||
| } | ||
| } | ||
|
|
||
| return memprovider.NewInMemoryProvider(memFlags) | ||
| } | ||
|
|
||
| // NewClient registers provider under domain and returns a client bound to it. | ||
| // | ||
| // A real service calls this once at startup (domain = the service name); the | ||
| // returned client is safe to share across goroutines. | ||
| func NewClient(domain string, provider openfeature.FeatureProvider) (*openfeature.Client, error) { | ||
| err := openfeature.SetNamedProviderAndWait(domain, provider) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("register feature-flag provider for %q: %w", domain, err) | ||
| } | ||
|
|
||
| return openfeature.NewClient(domain), nil | ||
| } | ||
|
|
||
| // Enabled reports whether flag is on for client, defaulting to OFF on a missing | ||
| // flag or any evaluation error — the default-off rule every new feature follows | ||
| // so a flag failure can never silently turn a feature on. | ||
| func Enabled(ctx context.Context, client *openfeature.Client, flag string) bool { | ||
| return client.Boolean(ctx, flag, false, openfeature.EvaluationContext{}) | ||
| } | ||
|
|
||
| // ExampleFeature returns the feature's output when ExampleFlag is enabled and a | ||
| // safe default otherwise — the shape a real feature takes: guard the new path | ||
| // behind Enabled, keep the old path as the default. Delete this when you add | ||
| // your own first flagged feature. | ||
| func ExampleFeature(ctx context.Context, client *openfeature.Client) string { | ||
| if Enabled(ctx, client, ExampleFlag) { | ||
| return "new feature output" | ||
| } | ||
|
|
||
| return "default output" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package featureflag_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/devantler-tech/go-template/pkg/featureflag" | ||
| ) | ||
|
|
||
| func TestExampleFeature(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| flags map[string]bool | ||
| want string | ||
| }{ | ||
| { | ||
| name: "flag on serves the new path", | ||
| flags: map[string]bool{featureflag.ExampleFlag: true}, | ||
| want: "new feature output", | ||
| }, | ||
| { | ||
| name: "flag off serves the default path", | ||
| flags: map[string]bool{featureflag.ExampleFlag: false}, | ||
| want: "default output", | ||
| }, | ||
| { | ||
| name: "flag absent defaults off", | ||
| flags: map[string]bool{}, | ||
| want: "default output", | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| provider := featureflag.NewProvider(testCase.flags) | ||
|
|
||
| client, err := featureflag.NewClient(testCase.name, provider) | ||
| if err != nil { | ||
| t.Fatalf("NewClient(%q) returned error: %v", testCase.name, err) | ||
| } | ||
|
|
||
| got := featureflag.ExampleFeature(context.Background(), client) | ||
| if got != testCase.want { | ||
| t.Errorf("ExampleFeature() = %q, want %q", got, testCase.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.