Seven correctness fixes to the recently landed anchors/styles, canonical, timestamp/binary, and full-node-key work, each reproduced and covered by a regression test. - Stream dump doubled the `---` marker under explicitDocumentStart or canonical: the single-document body now renders marker-free and the stream path owns marker emission, so a two-document stream reparses as two documents, not four. - Tagged flow-mapping keys used block-context safety, so a key like `a,b` emitted bare and reparsed as several entries; flow keys now use the flow-aware plain-scalar check. - Plain scalars ending in `:` reparse as an empty-valued mapping key, so both the model and tagged emit paths now fall back to quoting them. - An alias mapping key pointing at a defined non-scalar anchor raised the misleading undefinedAlias; it now raises expectedScalarKey, and a truly undefined alias key still raises undefinedAlias. - Tagged duplicate-key detection keyed on source spelling: it now keys on the resolved scalar value plus effective tag, so 0x1 and 1 collide while 1 and "1" do not and custom-tagged keys stay distinct. - Dumping a tree nested past the emitter limit overflowed the stack. - Timestamp now accepts a leap-second value of 60, counted as 60 elapsed seconds in the epoch math. - Tagged.Dumper honors sortKeys in its block and canonical paths for all-plain-string-key mappings, matching the model dumper. Nesting-guard design: dump is public and non-throwing across 90+ call sites, so making it throwing would be a breaking change. A tree nested deeper than the parser's own maxNestingDepth cannot round-trip through PureYAML (the parser rejects it), so no faithful emission exists. Each dump entry point now measures depth iteratively (no recursion, so the guard itself cannot overflow) and, past the limit, returns a documented, reparseable marker document that resolves to null. Under the limit the existing recursive renderers run byte-identically, so parsed trees and every realistic hand-built tree are unaffected. This mirrors the parser's enforceNestingLimit, which bounds depth before any recursive consumer.
94 lines
3.5 KiB
Swift
94 lines
3.5 KiB
Swift
extension PureYAML.Emitting.Dumper {
|
|
/// The YAML-specification canonical form, matching libyaml's canonical
|
|
/// emitter layout: an explicit `---` document start, an explicit tag on
|
|
/// every node, double-quoted scalars, and flow collections with one
|
|
/// entry per line, mapping entries in the explicit `? key : value,`
|
|
/// form. Tags derive from each value's case, the same resolution the
|
|
/// parser applies in reverse.
|
|
func canonicalDocument(_ value: PureYAML.Model.Value) -> String {
|
|
"---\n" + canonicalBody(value)
|
|
}
|
|
|
|
/// The canonical body without the leading `---` marker, so the marker is
|
|
/// owned by the single-document and stream paths, not baked into the body.
|
|
func canonicalBody(_ value: PureYAML.Model.Value) -> String {
|
|
canonicalLines(value, indent: 0).joined(separator: "\n") + "\n"
|
|
}
|
|
|
|
func canonicalLines(
|
|
_ value: PureYAML.Model.Value,
|
|
indent: Int,
|
|
) -> [String] {
|
|
let prefix = String(repeating: " ", count: indent)
|
|
switch value {
|
|
case let .sequence(values):
|
|
return canonicalSequenceLines(values, indent: indent)
|
|
case let .mapping(mapping):
|
|
return canonicalMappingLines(mapping, indent: indent)
|
|
default:
|
|
return [prefix + canonicalScalar(value)]
|
|
}
|
|
}
|
|
|
|
func canonicalSequenceLines(
|
|
_ values: [PureYAML.Model.Value],
|
|
indent: Int,
|
|
) -> [String] {
|
|
let prefix = String(repeating: " ", count: indent)
|
|
guard !values.isEmpty else {
|
|
return [prefix + "!!seq []"]
|
|
}
|
|
var lines = [prefix + "!!seq ["]
|
|
for value in values {
|
|
var entry = canonicalLines(value, indent: indent + options.indentWidth)
|
|
entry[entry.count - 1] += ","
|
|
lines.append(contentsOf: entry)
|
|
}
|
|
lines.append(prefix + "]")
|
|
return lines
|
|
}
|
|
|
|
func canonicalMappingLines(
|
|
_ mapping: PureYAML.Model.Mapping,
|
|
indent: Int,
|
|
) -> [String] {
|
|
let prefix = String(repeating: " ", count: indent)
|
|
guard !mapping.pairs.isEmpty else {
|
|
return [prefix + "!!map {}"]
|
|
}
|
|
let inner = indent + options.indentWidth
|
|
let innerPrefix = String(repeating: " ", count: inner)
|
|
var lines = [prefix + "!!map {"]
|
|
for pair in orderedPairs(mapping) {
|
|
var keyLines = canonicalLines(pair.keyNode.value, indent: inner)
|
|
keyLines[0] = innerPrefix + "? " + String(keyLines[0].dropFirst(innerPrefix.count))
|
|
lines.append(contentsOf: keyLines)
|
|
var valueLines = canonicalLines(pair.value, indent: inner)
|
|
valueLines[0] = innerPrefix + ": " + String(valueLines[0].dropFirst(innerPrefix.count))
|
|
valueLines[valueLines.count - 1] += ","
|
|
lines.append(contentsOf: valueLines)
|
|
}
|
|
lines.append(prefix + "}")
|
|
return lines
|
|
}
|
|
|
|
/// A canonical scalar: the value's implicit tag made explicit, content
|
|
/// double-quoted so no resolution ambiguity survives.
|
|
func canonicalScalar(_ value: PureYAML.Model.Value) -> String {
|
|
switch value {
|
|
case .null:
|
|
"!!null \"null\""
|
|
case let .bool(flag):
|
|
"!!bool \"\(flag ? "true" : "false")\""
|
|
case let .int(number):
|
|
"!!int \"\(number)\""
|
|
case let .double(number):
|
|
"!!float \"\(number)\""
|
|
case let .string(string):
|
|
"!!str " + quote(string)
|
|
case .sequence, .mapping:
|
|
""
|
|
}
|
|
}
|
|
}
|