You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
internal: update structpath to accept both dot and bracket syntax for fields and maps (#3640)
## Changes
Simplify internal representation of structpath.PathNode: struct fields
and map keys are represented the same way internally.
When parsing, users can specify either `.field` or `[‘field’]`, both
will be encoded as tagStringKey in PathNode.
When saving PathNode as string, String() selects appropriate encoding
(prefers dot unless it uses reserved characters). DynPath() is removed,
String() returns DynPath()-like value for those values that can be
represented by dot notation.
At the same time, PathNode now accurately represents `foo.*` vs
`foo[*]`, this structure is preserved and not equivalent. This again
improves compatibility with dyn.Path as it represents index and keys
wildcard differently.
structaccess is updated to accept PathNode. Since PathNode only gives
StringKey() now and does not distinguish between map and struct fields,
structaccess.Get essentially supports both syntaxes for both kinds.
structwalk is updated to emit dotStar for map wildcards, making it
compatible with code expecting dyn.Pattern (this is used for required
fields and enum validation).
structwalk.PathNode.String() is rewritten to be iterative rather than
recursive.
PathNode gets new methods: Prefix(N), SkipPrefix(N), AsSlice().
## Why
Dot notation is currently used for maps e.g. "resources.jobs.foo". With
precise map encoding that would look like "resources.jobs['foo']" which
looks unfamiliar and also will not match if someone does string
comparison against "resources.jobs.foo".
Adopting this scheme makes it easier to work with PathNode in dyn.Path
world and in fact makes it drop-in replacement (the syntax is superset).
## Tests
Existing and new unit tests.
0 commit comments