Skip to content

fix: normalize key delimiter to lowercase for nested lookups (#2069) - #2175

Open
xingzihai wants to merge 1 commit into
spf13:masterfrom
xingzihai:fix/key-delimiter-case
Open

xingzihai wants to merge 1 commit into
spf13:masterfrom
xingzihai:fix/key-delimiter-case

Conversation

@xingzihai

Copy link
Copy Markdown

Overview:
Normalize the key delimiter to lowercase at construction so that nested key lookups work when the delimiter contains uppercase letters. This is a one-line fix in the KeyDelimiter option.

What problem does it solve?:
When a user configures a key delimiter containing uppercase letters (e.g. KeyDelimiter("<SEPARATOR>")), nested key lookups silently fail. This is because all keys are lowercased before being split on the delimiter (strings.ToLower(key) at viper.go:716, then strings.Split(lcaseKey, v.keyDelim) at viper.go:725/1198/1209), but the delimiter itself is stored raw. A lowercased key like severity<separator>foo never contains the uppercase <SEPARATOR>, so strings.Split yields a single flat element and the nested value is lost.

Closes #2069. This is the same fix as the never-merged PR #2084.

Special notes for a reviewer:

  • The fix is minimal: one line in the KeyDelimiter option (v.keyDelim = strings.ToLower(d)).
  • All 25 use-sites of keyDelim (Get/Set/SetDefault/AllKeys/AllSettings/searchIndexableWithPathPrefixes/etc.) are fixed by normalizing at the single entry point.
  • The default delimiter "." is already lowercase and unaffected.
  • Sub() copies the already-normalized value, so no change needed there.
  • Regression test TestKeyDelimiterCase verifies that a nested YAML value is correctly resolved with an uppercase delimiter (fails pre-fix, passes post-fix).

)

Keys are lowercased before being split on the key delimiter, but the
delimiter itself was stored raw. With an uppercase delimiter (e.g.
KeyDelimiter("<SEPARATOR>")), the lowercased key never contains the
delimiter, so strings.Split yields a single flat element and nested
values are silently lost.

Normalize the delimiter to lowercase at construction in the KeyDelimiter
option. All 25 use-sites are fixed by normalizing at the single entry
point. The default delimiter "." is already lowercase and unaffected.

Closes spf13#2069. Same fix as never-merged PR spf13#2084.

Add regression test TestKeyDelimiterCase verifying nested YAML values
resolve correctly with an uppercase delimiter (fails pre-fix, passes
post-fix).
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

key delimiter can't be uppercase

2 participants