Skip to content

Conversation

@DanRulev
Copy link

What does this PR do?

Adds WriteConfigAsTyped and SafeWriteConfigAsTyped to write configuration with proper types instead of strings. This is especially useful when config values come from sources that only provide strings — like environment variables or CLI flags.

Without this, Viper writes "true" instead of true, "42" instead of 42, etc. This PR fixes that by automatically coercing types before writing.

Example

v.Set("enabled", "true")
v.Set("port", "8080")
v.WriteConfigAsTyped("config.yaml")

Before

enabled: "true"
port: "8080"

After

enabled: true
port: 8080

Changes
✅ WriteConfigAsTyped(filename) — writes config with type coercion
✅ SafeWriteConfigAsTyped(filename) — same, but fails if file exists
✅ Recursive coercion in nested maps and slices
✅ Handles bool, int64, float64 from string representations
✅ Full test coverage for JSON, YAML, TOML
Test Plan
TestWriteConfigAsTyped — covers all formats
TestSafeWriteConfigAsTyped — checks file existence
Correct type conversion
Notes
Integers are parsed as int64 for better TOML/YAML output (no .0)
Uses existing Viper patterns: afero, logger, encoderRegistry

@CLAassistant
Copy link

CLAassistant commented Aug 30, 2025

CLA assistant check
All committers have signed the CLA.

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