Skip to content

Resolve YAML << merge keys when decoding - #217

Merged
vito merged 4 commits into
map-literal-coercionfrom
yaml-merge-keys
Oct 30, 2026
Merged

vito merged 4 commits into
map-literal-coercionfrom
yaml-merge-keys

Conversation

@vito

@vito vito commented Oct 30, 2026

Copy link
Copy Markdown
Owner

YAML.decode kept a << merge key as a literal "<<" entry. So a common config pattern failed to decode into records or maps:

base: &base
  image: nginx
web:
  <<: *base
  port: 1

Before this change, decoding that as Map[Service!]! failed with ["web"]["<<"]: expected ... got object.

Decoding

Merge keys now follow the YAML merge-key spec:

  • The value is a mapping (inline or an alias) or a sequence of them.
  • The mapping's own keys win over merged ones, wherever they appear (before or after <<).
  • In <<: [*a, *b], earlier mappings win over later ones.
  • A quoted "<<" is an ordinary key.
  • A non-mapping merge value is an error: a: <<: merge value must be a mapping or a sequence of mappings. Self-merges hit the existing cyclic-alias check.

Encoding

yaml.v3 writes a "<<" map key bare (<<: 1). With merges honored, YAML.decode(YAML.encode(["<<": 1])) would therefore have failed. In maps that have a "<<" key, that key is swapped for a type whose MarshalYAML returns a double-quoted string node, so it comes out as "<<": 1. Everything else still goes straight through yaml.Marshal, so other output is the same as before, including "<<" values (a: <<).

An earlier version of the fix built a yaml.Node with Node.Encode. That method re-parses its own output, which broke tab-led multi-line strings; it was replaced, and a test covers that case.

Tests are in tests/test_from_yaml.dang: merges into maps and records, the precedence rules, inline merges, quoted "<<", the error, and encode round-trips. The docs (docs/lit/data/json-yaml.md, skill stdlib.md) describe merge-key handling.

@vito
vito added this pull request to stack #218 October 30, 2026 16:37
vito added 4 commits October 30, 2026 12:42
YAML.decode kept a `<<` merge key as a literal "<<" entry, so `web: {<<: *base,
port: 1}` failed to decode into a record or map (`["web"]["<<"]: expected int,
got object`). Merge keys now follow the YAML merge-key spec: the value is a
mapping or a sequence of them, the mapping's own keys win wherever they
appear, and earlier mappings in a sequence win over later ones. A quoted "<<"
stays an ordinary key, and a non-mapping merge value is an error.

Signed-off-by: Alex Suraci <[email protected]>
yaml.v3 resolves a "<<" map key to the !!merge tag and emits it bare, so now
that YAML.decode honors merge keys, decoding YAML.encode(["<<": 1]) failed
instead of round-tripping. Encode through a yaml.Node and retag every "<<"
key as a double-quoted string.

Signed-off-by: Alex Suraci <[email protected]>
Encoding through a yaml.Node tags every "<<" string !!merge, not just keys,
so a "<<" value came out as `a: !!merge <<`. Retag those as strings too; they
emit plain, as before the node round-trip.

Signed-off-by: Alex Suraci <[email protected]>
yaml.Node.Encode emits YAML and parses it back, which broke encoding a
multi-line string led by a tab and reshaped newline-only strings. Marshal the
plain tree as before, and swap only a "<<" key, in the maps that have one, for
a key type whose MarshalYAML yields a double-quoted string node.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: dcbaed1
Status: ✅  Deploy successful!
Preview URL: https://f6a13cc9.dang-3kk.pages.dev
Branch Preview URL: https://yaml-merge-keys.dang-3kk.pages.dev

View logs

@vito
vito merged commit 286aba3 into main Oct 30, 2026
9 checks passed
sipsma added a commit to grouville/dagger that referenced this pull request Nov 3, 2026
The engine pinned Guillaume's fork of Dang for vito/dang#211, which adds
PrepareDir and PreparedDirectory.Evaluate. vito/dang#250 carried that
change onto Dang main and merged, so Dang now moves to its merge commit
and the replace of the fork goes away.

Since the merge of vito/dang#213 that Dang was on before, Dang's type
checker became stricter (vito/dang#230 to #235, #246 and #249): programs
it accepted that could fail at runtime are now type errors, so a module
that loaded before may now fail to load. Among them, operators need
non-null operands, Map.get is typed as nullable (vito/dang#215), and a
decode result must be given a type before it is used. Function
parameters match by name, so a positional call through an interface
binds in the interface's order. YAML << merge keys are resolved when
decoding (vito/dang#217), and malformed syntax is reported instead of
panicking the parser (vito/dang#243). The release note describes what
module authors will see.

Signed-off-by: Erik Sipsma <[email protected]>
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.

1 participant