Skip to content

feat: Ignore top-level keys started with x-#5026

Draft
ilia-rassadin-rn wants to merge 2 commits intothomaspoignant:mainfrom
ilia-rassadin-rn:ir-top-level-anchors
Draft

feat: Ignore top-level keys started with x-#5026
ilia-rassadin-rn wants to merge 2 commits intothomaspoignant:mainfrom
ilia-rassadin-rn:ir-top-level-anchors

Conversation

@ilia-rassadin-rn
Copy link
Copy Markdown

Currently, go-feature-flag doesn't support reusable targeting conditions out of the box. So we cannot define set of rules

my-custom-group:
  - query: role eq "admin"
  - query: group eq "QA"
  - query: email eq "custom@example.com"

...
my-flag:
  targeting:
    - query: my-custom-group eq true
      variation: enabled

But something similar can be achieved using YAML anchors. The issue with this approach is go-feature-flag treats every top-level key as a flag. So this is invalid configuration because my-custom-group is invalid flag:

x-custom-group: &x-custom-group
  - query: role eq "admin"
    variation: enabled
  - query: group eq "QA"
    variation: enabled
  - query: email eq "custom@example.com"
    variation: enabled

...
my-flag:
  variations:
    enabled: true
    disabled: false
  targeting:
    - *x-custom-group
  defaultRule:
    variation: disabled

It is possible to define anchors as part of the flag and then reuse it, but it lowers readability of the config file.

Usually, YAML consumers ignores keys started with x-. E.g. docker compose does this since 2017, openapi allows x- keys for extending schema since it was swagger, etc.

So this commit brings this approach into go-feature-flag. Now we can define top-level anchors started with x- and reuse these snippets to define multiple flags.

I've decided to ignore x- keys for JSON and TOML too. E.g. for JSON it might be used as a comment, so there is still value provided. And it is nice to be able to transparently convert flags definition between formats so they remain valid.

Description

Closes issue(s)

Resolve #

Checklist

  • I have tested this code
  • I have added unit test to cover this code
  • I have updated the documentation (README.md and /website/docs)
  • I have followed the contributing guide

@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 25, 2026

Deploy Preview for go-feature-flag-doc-preview canceled.

Name Link
🔨 Latest commit 7a73357
🔍 Latest deploy log https://app.netlify.com/projects/go-feature-flag-doc-preview/deploys/69c3a16e7a03a40008748ded

@github-actions github-actions Bot requested a review from thomaspoignant March 25, 2026 08:37
@ilia-rassadin-rn ilia-rassadin-rn force-pushed the ir-top-level-anchors branch 2 times, most recently from 1dd63a0 to 961b95e Compare March 25, 2026 08:38
@ilia-rassadin-rn ilia-rassadin-rn changed the title Ignore top-level keys started with x- feat: Ignore top-level keys started with x- Mar 25, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the readConfigFile function to filter out configuration keys prefixed with "x-" after parsing TOML, JSON, or YAML files. Additionally, it introduces YAML anchors for reusable targeting configurations in testdata/flag-config.yaml. The review feedback points out a typo in the newly added YAML anchor x-reusable-tartgetting, suggesting it should be x-reusable-targeting, and requests updating both the anchor definition and its alias reference accordingly.

Comment thread testdata/flag-config.yaml Outdated
Comment thread testdata/flag-config.yaml Outdated
Currently, go-feature-flag doesn't support reusable targeting conditions out of the box. So we cannot define set of rules

```yaml
my-custom-group:
  - query: role eq "admin"
  - query: group eq "QA"
  - query: email eq "custom@example.com"

...
my-flag:
  targeting:
    - query: my-custom-group eq true
      variation: enabled
```

But something similar can be achieved using YAML anchors. The issue with this approach is go-feature-flag treats every top-level key as a flag. So this is invalid configuration because my-custom-group is invalid flag:

```yaml
x-custom-group: &x-custom-group
  - query: role eq "admin"
    variation: enabled
  - query: group eq "QA"
    variation: enabled
  - query: email eq "custom@example.com"
    variation: enabled

...
my-flag:
  variations:
    enabled: true
    disabled: false
  targeting:
    - *x-custom-group
  defaultRule:
    variation: disabled
```

It is possible to define anchors as part of the flag and then reuse it, but it lowers readability of the config file.

Usually, YAML consumers ignores keys started with `x-`. E.g. docker compose does this since 2017, openapi allows `x-` keys for extending schema since it was swagger, etc.

So this commit brings this approach into `go-feature-flag`. Now we can define top-level anchors started with `x-` and reuse these snippets to define multiple flags.

I've decided to ignore `x-` keys for JSON and TOML too. E.g. for JSON it might be used as a comment, so there is still value provided. And it is nice to be able to transparently convert flags definition between formats so they remain valid.
@sonarqubecloud
Copy link
Copy Markdown

@ilia-rassadin-rn ilia-rassadin-rn marked this pull request as draft March 25, 2026 08:51
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 25, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.76%. Comparing base (ea02805) to head (7a73357).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
cmd/cli/helper/read_config_file.go 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5026      +/-   ##
==========================================
- Coverage   85.79%   85.76%   -0.03%     
==========================================
  Files         153      153              
  Lines        6541     6542       +1     
==========================================
- Hits         5612     5611       -1     
- Misses        698      699       +1     
- Partials      231      232       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@thomaspoignant
Copy link
Copy Markdown
Owner

Sorry for the delay answering this.
I understand why you need this but I am doubting if x- is the right prefix to use, because some companies may already have flags starting with this pattern and it will become a breaking change.

I am wondering if the prefix should not be configurable instead of being default to x-.

What do you think @ilia-rassadin-rn ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants