ini icon indicating copy to clipboard operation
ini copied to clipboard

Add option to preserve empty values in ValueWithShadows

Open matanw opened this issue 5 months ago • 1 comments

Describe the feature

Currently, ValueWithShadows() ignores empty values after PR #316. This breaks workflows where an empty assignment is semantically meaningful (e.g., systemd unit files use empty values to reset previous assignments, see https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html: "If the empty string is assigned to this option, the list of commands to start is reset, prior assignments of this option will have no effect.").

Describe the solution you'd like

Introduce a new opt‑in option to preserve empty shadow values:

opts := ini.LoadOptions{
    AllowShadows:         true,
    PreserveEmptyShadows: true, // new option
}

f, _ := ini.Load(data, opts)
vals := f.Section("Service").Key("Environment").ValueWithShadows()
// Expected: []string{"FOO=bar", ""}

Default behavior would remain unchanged to avoid breaking existing users.

Describe alternatives you've considered

Add a new field to LoadOptions, an enum named EmptyValueHandling, with three possible values:

  1. Ignore – current default behavior, empty values are skipped.
  2. Preserve – empty values are kept in ValueWithShadows().
  3. IgnorePrevious – when an empty value is encountered, all previous values for the key are discarded (systemd‑style reset).

Additional context

No response

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

matanw avatar Jul 28 '25 07:07 matanw

Implementation offer: I’d be happy to open a PR implementing this feature if maintainers agree with the proposed design.

matanw avatar Jul 28 '25 07:07 matanw