feat: promote embedded (anonymous) structs across loaders#48
Open
rlball wants to merge 1 commit into
Open
Conversation
Embedded struct fields are now flattened/promoted consistently instead of being prefixed with the embedded type name. The env and flag loaders previously prefixed embedded fields (EMBEDDED_DB_HOST), while json/toml flattened them, leaving behavior inconsistent across formats. The env, flag, and render name generators now drop the type-name prefix for embedded struct fields (guarded to structs only, excluding time.Time and embedded basic types), matching json/toml and Go's encoding/json promotion. yaml requires a yaml:",inline" tag (yaml.v2 limitation). Also document every supported struct field tag with its accepted values and examples, and add an embedded-struct section to the README.
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.
Summary
Makes embedded (anonymous) struct fields behave consistently across all loaders by promoting/flattening them, the same mental model as Go's
encoding/json.Previously the behavior was inconsistent for the same embedded field:
envEMBEDDED_DB_HOST(type-name prefix)HOST(promoted)flag--embedded-db-host(type-name prefix)--host(promoted)jsonHost(already flat)tomlHost(already flat)yamlembeddeddb:hostwithyaml:",inline"Changes
util/node: addIsAnonymous()helper.load/env,load/flag,render: drop the type-name prefix for embedded struct fields. Guarded to structs only (excludestime.Timeand embedded basic types). An explicitenv/flagtag still overrides.yaml:",inline"and exported-type requirements.Behavior change / compatibility
Anyone who relied on the previously-undocumented env/flag prefix for an embedded struct (e.g.
EMBEDDED_DB_HOST) must switch to the promoted name (HOST) or convert the embed to a named field. Named struct fields are unaffected.Notes surfaced during verification (documented in README)
gopkg.in/yaml.v2does not inline anonymous structs automatically;yaml:",inline"is required.Test plan
TestEmbeddedStructPromotionEnv— env promotesHOST/PORT(no prefix).TestEmbeddedStructPromotionFiles— yaml (inline)/toml/json all flatten.omitprefixtests) still passes.go test -race ./...,go vet ./...,gofmt -l .all clean.