Skip to content

refactor(config): simplify env var expansion to pre-parse approach#164

Merged
spachava753 merged 1 commit intomainfrom
simplify-env-var-expansion
Feb 2, 2026
Merged

refactor(config): simplify env var expansion to pre-parse approach#164
spachava753 merged 1 commit intomainfrom
simplify-env-var-expansion

Conversation

@spachava753
Copy link
Copy Markdown
Owner

Summary

Refactors environment variable expansion in the config system from a post-parse struct method to pre-parse text expansion using os.ExpandEnv().

Changes

  • loader.go: Added os.ExpandEnv() call in parseConfigData() before parsing, plus error hint helper for better error messages
  • env.go: Deleted entirely (78 lines of expansion helpers removed)
  • env_test.go: Rewritten with 15 test cases covering normal and edge cases
  • loader_test.go: Removed old TestExpandEnvironmentVariables_CodeMode
  • docs/specs/config.md: Updated documentation with caveats section

Benefits

  1. Universal env var support: Expansion now works everywhere in the config, not just specific fields
  2. Simpler implementation: One line replaces 78 lines of code
  3. More flexible: Users can use env vars in any string field including map keys, array elements, nested values
  4. Easier to maintain: No need to update expansion code when new config fields are added
  5. Better error messages: Hints about env var expansion when parsing fails

Testing

All config tests pass. Added edge case tests for:

  • Special characters in env var values (with quotes for safety)
  • Error hint messages when parsing fails after expansion

Environment variable expansion now happens on the raw config file content before YAML/JSON parsing, rather than post-parse on specific struct fields. This provides universal env var support throughout the entire config file using os.ExpandEnv().

The previous implementation required manually expanding env vars in each supported field, which was error-prone when adding new config fields and limited flexibility. The new approach expands $VAR and ${VAR} syntax anywhere in the config, including map keys, array elements, and nested values.

Added error hints when parsing fails after expansion to help users diagnose issues caused by special characters in env var values. Updated documentation with caveats about YAML/JSON special characters and the lack of escape syntax for literal $ signs.
@spachava753 spachava753 merged commit 8bde135 into main Feb 2, 2026
@spachava753 spachava753 deleted the simplify-env-var-expansion branch April 10, 2026 00:23
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.

1 participant