fix: name map keys by value in error messages and metadata - #207
Open
yu2971512385-ui wants to merge 1 commit into
Open
yu2971512385-ui wants to merge 1 commit into
yu2971512385-ui wants to merge 1 commit into
Conversation
decodeMapFromMap built the per-key name with reflect.Value.String(),
which returns the key only for a string Kind and a placeholder for
everything else. Any map that is not keyed by a string kind - notably
the map[any]any that YAML decoders produce - therefore reported every
key as <interface {} Value>:
'M[<interface {} Value>]' expected type 'string', got unconvertible type '[]string'
and Metadata.Keys collected the same placeholder instead of the keys.
Unwrap an interface key and format the underlying value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Decoding any map that is not keyed by a
stringkind — including themap[any]anythatgopkg.in/yaml.v2and other decoders produce — reports every key as<interface {} Value>:The same decode against a
map[string]anysource names the key correctly (M[x]), so the diagnostics silently degrade exactly for the input shape that is most common in YAML-backed configuration — the caller is told a key failed but not which one.Root cause
decodeMapFromMapbuilds the name withreflect.Value.String():reflect.Value.String()is documented as the one getter that does not panic for the wrong kind: it returns the string only forKind() == Stringand"<T Value>"otherwise. Formap[any]anythe key values haveKind() == Interface, and formap[int]Tthey haveKind() == Int.Fix
Add a small
mapKeyNamehelper that unwraps an interface key and formats the underlying value, and use it for the field name. String keys keep taking the existing fast path, so no existing name changes:Tests
TestDecodeMapKeyName— an int key that cannot be decoded into astringkey must be named'[7]'in the error.TestMetadata_MapKeys— anany-keyed source map must contributeVmap[foo]/Vmap[baz]toMetadata.Keys.Both fail without the change;
go test ./...passes with it.