Skip to content

Pass the key to ValueMapper - #438

Open
unknwon wants to merge 1 commit into
mainfrom
value-mapper-key
Open

unknwon wants to merge 1 commit into
mainfrom
value-mapper-key

Conversation

@unknwon

@unknwon unknwon commented Nov 15, 2026

Copy link
Copy Markdown
Member

Describe the pull request

ValueMapper only got the raw value, so a mapper couldn't tell which key it was mapping or read other keys, which is what #357 needs for ${ENV||default} or %{section.key} style expansion.

ValueMapper is now func(k *Key, value string) string, and two accessors reach the rest of the file: Key.Section() and Section.File(). During a %(name)s substitution the referenced value is mapped with its own key, not the referencing one. The mapper runs without the file's lock held, so it can read other keys.

This is a breaking change to the v2 API, so it should land before v2.0.0. A func(string) string such as os.ExpandEnv is wrapped as func(_ *ini.Key, v string) string { return os.ExpandEnv(v) }; the migration guide says so.

Link to the issue: closes #357

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code.

AI usage

Written by Amp (an AI coding agent) at the maintainer's request. Validation: go test ./... and go vet. The new test resolves ${section.key} through k.Section().File() and fails if a referenced value is mapped with the referencing key.

ValueMapper is now func(k *Key, value string) string, and Key.Section and
Section.File reach the rest of the file, so a mapper can expand
references to other sections or keys of its own (#357).

This branch has not been deployed

No deployments
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.

增强 ValueMapper 类型

1 participant