Skip to content

Support for type normalization before encoding config (e.g., convert "true" string to true boolean) #2021

@sanyam03

Description

@sanyam03

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Problem Description

Describe the feature

When using viper.WriteConfig or a custom write implementation (via marshalWriter), values in the internal config map like "true" (string) are written to the output file (e.g., YAML) as "true" instead of true (boolean).

This usually happens when config values are updated or set programmatically from sources that store everything as strings (e.g., CLI flags, env vars, encrypted configs, etc).

Problem

This causes configuration types to be misleading in output YAML:

packager:
  example_project:
    trust: "true"   # should be: true (bool)


### Proposed Solution

A type normalization step before encoding, where "true" / "false" strings could be coerced into booleans (similarly for numeric strings).

A hook/callback mechanism (e.g., BeforeMarshal(func(map[string]any) map[string]any)) so users can implement normalization logic themselves before Viper marshals the config.

An option or flag like viper.NormalizeTypesOnWrite(true) that opts into this behavior.

### Alternatives Considered

_No response_

### Additional Information

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions