Skip to content

Introduce functional options for configuration construction.#198

Merged
AlphaOne1 merged 5 commits into
masterfrom
add_functional_options
Jun 28, 2026
Merged

Introduce functional options for configuration construction.#198
AlphaOne1 merged 5 commits into
masterfrom
add_functional_options

Conversation

@AlphaOne1

@AlphaOne1 AlphaOne1 commented Jun 28, 2026

Copy link
Copy Markdown
Owner
  • Added WithValue for templated access to custom key-value pairs.
  • Introduced .Value variable for configuration templates.
  • Replaced older configuration methods with New for a unified interface.
  • Updated documentation, examples, and changelog accordingly.
  • Upgraded golang.org/x/tools to v0.47.0.

Summary by CodeRabbit

  • New Features
    • Added a new New[T](...) functional-options API for constructing configs, including WithFile, WithReader, WithValue, and WithSecretRE.
    • Added/updated documentation and examples for injecting custom template values via .Values.
    • Added support for file output via ToFile.
  • Bug Fixes
    • Improved validation and error reporting when no configuration sources are provided.
    • Strengthened handling for SecretRE set to nil.
  • Documentation
    • Updated README with “Using Custom Values” and refreshed secret-hiding regex examples.
    • Added a new release entry to the changelog.
  • Chores
    • Updated lint configuration and a Go dependency.

- Added `WithValue` for templated access to custom key-value pairs.
- Introduced `.Value` variable for configuration templates.
- Replaced older configuration methods with `New` for a unified interface.
- Updated documentation, examples, and changelog accordingly.
- Upgraded `golang.org/x/tools` to v0.47.0.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>
@AlphaOne1 AlphaOne1 self-assigned this Jun 28, 2026
@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@AlphaOne1, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 48 minutes and 53 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a246c120-4f5a-439a-9cb4-a2de8e1ec143

📥 Commits

Reviewing files that changed from the base of the PR and between f241d36 and 9a48fd0.

📒 Files selected for processing (1)
  • testData/test_config_0_overlay.yaml
📝 Walkthrough

Walkthrough

Refactors config construction to a functional options API via New[T], adds WithValue templating inputs, changes source loading and overlay handling, and updates tests, examples, docs, changelog, lint config, and one indirect dependency.

Changes

Functional Options and WithValue Feature

Layer / File(s) Summary
Config struct and options API
config.go
Defines source handling, extends Config[T] with sources and values, adds Option[T] plus WithSecretRE, WithFile, WithReader, WithValue, introduces New[T], relocates ToFile, and replaces From* helpers with wrappers around New[T].
Source loading and overlay
config.go
Implements readSources with empty-source validation and a single-source fast path, renders templates with Values in fromSingle, and overlays additional sources by building temporary YAML configs and merging nodes.
Config and source tests
config_internal_test.go, config_test.go, testData/test_config_3.yaml, testData/test_config_0_overlay.yaml
Updates config tests to use New[T] with source and value options, adds a rendered-template case, adds no-source and empty-source error checks, tightens secret-regexp assertions, and updates overlay fixtures to use .Values.
Value example and smoke tests
examples/templating/value/main.go, examples/templating/value/main_test.go, examples/templating/value/my_config.yaml, examples/templating/env/main_test.go
Adds a templated config example using New[Config] and WithValue, includes a smoke test for main(), and removes the os.Args override from the env example test.
Docs, changelog, lint, and dependency
README.md, CHANGELOG.md, .golangci.yaml, go.mod
Updates the README custom-values section and secret-regexp example, adds a 0.10.0 changelog entry, adds a varnamelen ignore rule for c *Config[T], and bumps golang.org/x/tools to v0.47.0.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • AlphaOne1/templig#144: Both PRs change config.go secret-regexp handling and template-related config plumbing.

Poem

🐰 Hop, hop — a New[T] delight,
With values tucked into templates just right.
The rabbit says: “pass through the YAML stream!”
Then masks secrets like a tidy dream.
Fresh examples bounce, and tests all cheer ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 58.82% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a functional-options API for configuration construction.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch add_functional_options

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov-commenter

codecov-commenter commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
config.go 100.00% <100.00%> (ø)
examples/templating/value/main.go 100.00% <100.00%> (ø)

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Nitpick comments (2)
config_test.go (2)

420-423: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Exercise the new overlay value path in this test.

This WithValue("does", "nothing") input is never consumed by either YAML source, so a regression in config.go's value propagation during overlay would still pass here. Make one of the overlaid sources template from the injected value, or switch one source to inline templated YAML.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@config_test.go` around lines 420 - 423, The test setup in the templig.New
call is not actually exercising value propagation through overlay because
WithValue("does", "nothing") is unused by both YAML inputs. Update
config_test.go so one of the overlaid sources in the templig.New path references
the injected value via templating, or replace one WithFile source with inline
templated YAML, and keep the relevant symbols templig.New, WithValue, and
WithFile in place to verify the overlay value path.

859-863: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert the sentinel error instead of only err != nil.

As written, this test passes for any constructor failure. Checking errors.Is(err, templig.ErrNoSecretRegexp) keeps it pinned to the WithSecretRE(nil) contract from config.go.

Suggested change
 	_, err := templig.New[TestConfig](
 		templig.WithFile[TestConfig]("testData/test_config_0.yaml"),
 		templig.WithSecretRE[TestConfig](nil))
 
-	if err == nil {
-		t.Errorf("setting secret regex to nil should return an error")
+	if !errors.Is(err, templig.ErrNoSecretRegexp) {
+		t.Errorf("expected ErrNoSecretRegexp, got %v", err)
 	}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@config_test.go` around lines 859 - 863, The test around
templig.New[TestConfig] should assert the specific sentinel error rather than
only checking for a non-nil error. Update the assertion to use errors.Is against
templig.ErrNoSecretRegexp so the test stays tied to the WithSecretRE(nil)
contract in templig.New/WithSecretRE instead of passing on any constructor
failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@CHANGELOG.md`:
- Line 10: The changelog entry in CHANGELOG.md uses the wrong template context
name by referencing .Value.valName instead of the actual .Values.valName used by
the template data in config.go; update the documented variable name to match the
plural "Values" so it stays consistent with the README fix and the existing
template symbols.

In `@examples/templating/value/main_test.go`:
- Around line 11-15: The smoke test in TestMainGood only calls main() and never
checks that the example actually loaded, so it can pass even when the
template/render path is broken. Update the test to assert the expected success
path directly, either by invoking templig.New[Config] and checking the result or
by verifying main() produced the expected loaded configuration outcome, so
failures are caught and t is used.

In `@examples/templating/value/main.go`:
- Around line 27-39: The local variable name in the templig example is too short
for the varnamelen lint rule. Rename the `c` variable in the
`templig.New[Config]` assignment to a longer, clearer name such as `cfg`, and
update its subsequent `Get()` and `ToSecretsHiddenStructured()` uses to match
without changing behavior.

In `@examples/templating/value/my_config.yaml`:
- Line 6: The templating example is using the wrong root key, so the value
lookup fails before YAML parsing. Update the expression in the config template
to use .Values.pass instead of .Value.pass, matching the data shape produced by
WithValue and consumed by New so the example renders correctly.

In `@README.md`:
- Line 347: The templating example uses the wrong Helm values reference: replace
`.Value.pass` with `.Values.pass` in the README example to match how `config.go`
exposes custom values. Also search the related templating docs in the CHANGELOG
and `examples/templating/value/*` and update any matching `.Value` usage to
`.Values` so the wording and examples stay consistent.

---

Nitpick comments:
In `@config_test.go`:
- Around line 420-423: The test setup in the templig.New call is not actually
exercising value propagation through overlay because WithValue("does",
"nothing") is unused by both YAML inputs. Update config_test.go so one of the
overlaid sources in the templig.New path references the injected value via
templating, or replace one WithFile source with inline templated YAML, and keep
the relevant symbols templig.New, WithValue, and WithFile in place to verify the
overlay value path.
- Around line 859-863: The test around templig.New[TestConfig] should assert the
specific sentinel error rather than only checking for a non-nil error. Update
the assertion to use errors.Is against templig.ErrNoSecretRegexp so the test
stays tied to the WithSecretRE(nil) contract in templig.New/WithSecretRE instead
of passing on any constructor failure.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c4d4cfe2-fd67-4c1f-9952-a43f7d4a33cb

📥 Commits

Reviewing files that changed from the base of the PR and between b7bd553 and 21c303c.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (11)
  • .golangci.yaml
  • CHANGELOG.md
  • README.md
  • config.go
  • config_internal_test.go
  • config_test.go
  • examples/templating/value/main.go
  • examples/templating/value/main_test.go
  • examples/templating/value/my_config.yaml
  • go.mod
  • testData/test_config_3.yaml

Comment thread CHANGELOG.md Outdated
Comment thread examples/templating/value/main_test.go
Comment thread examples/templating/value/main.go Outdated
Comment thread examples/templating/value/my_config.yaml Outdated
Comment thread README.md Outdated
…ocumentation accordingly.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>
…ingly.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>
…roper resource closure. Update tests accordingly.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@testData/test_config_0_overlay.yaml`:
- Line 6: The YAML overlay entry renders an unquoted template expression, which
can break when the resolved value contains YAML-special characters. Update the
value in test_config_0_overlay.yaml so the .Values.pass2 expression is quoted in
the rendered YAML, keeping the overlay valid for arbitrary inputs and preserving
it as a string.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 99b276a0-70ec-43eb-b676-eb6de92dc386

📥 Commits

Reviewing files that changed from the base of the PR and between 21c303c and f241d36.

📒 Files selected for processing (10)
  • CHANGELOG.md
  • README.md
  • config.go
  • config_internal_test.go
  • config_test.go
  • examples/templating/env/main_test.go
  • examples/templating/value/main.go
  • examples/templating/value/main_test.go
  • examples/templating/value/my_config.yaml
  • testData/test_config_0_overlay.yaml
💤 Files with no reviewable changes (1)
  • examples/templating/env/main_test.go
✅ Files skipped from review due to trivial changes (3)
  • CHANGELOG.md
  • examples/templating/value/my_config.yaml
  • README.md
🚧 Files skipped from review as they are similar to previous changes (4)
  • examples/templating/value/main.go
  • config_internal_test.go
  • config_test.go
  • config.go

Comment thread testData/test_config_0_overlay.yaml Outdated
… with YAML best practices.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>
@AlphaOne1 AlphaOne1 merged commit da949a1 into master Jun 28, 2026
19 checks passed
@AlphaOne1 AlphaOne1 deleted the add_functional_options branch June 28, 2026 19:33
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