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.
137 lines
6.0 KiB
Swift
137 lines
6.0 KiB
Swift
public extension PureYAML.Tagged {
|
|
/// The caseless namespace of built-in type-directed checks for the tagged-node tree, the tagged
|
|
/// layer's counterpart of `Validation.BuiltinValidation`. Each is a `Validation<_, Node>` on the one
|
|
/// shared validation framework, not a copy of the machinery (validation-rules.md, principle 10).
|
|
enum BuiltinValidation {
|
|
/// Reports each duplicate key within a single tagged mapping. The tagged-layer counterpart of
|
|
/// `Validation.BuiltinValidation.mappingKeysAreUnique`; duplicate keys are as invalid in a
|
|
/// tag-preserving tree as in the plain model, and `Tagged.Mapping` keeps every parsed pair.
|
|
public static var mappingKeysAreUnique: PureYAML.Validation.Validation<Mapping, Node> {
|
|
PureYAML.Validation.Validation(description: "Mapping keys are unique") { context in
|
|
var seen = Set<PureYAML.Tagged.MappingKeyIdentity>()
|
|
var errors: [PureYAML.Validation.ValidationError] = []
|
|
for pair in context.subject.pairs where !seen.insert(keyIdentity(of: pair)).inserted {
|
|
errors.append(PureYAML.Validation.ValidationError(
|
|
severity: .error,
|
|
reason: "Duplicate mapping key '\(pair.key)'",
|
|
path: context.path.appending(.key(pair.key)),
|
|
))
|
|
}
|
|
return errors
|
|
}
|
|
}
|
|
|
|
static func keyIdentity(of pair: PureYAML.Tagged.Pair) -> PureYAML.Tagged.MappingKeyIdentity {
|
|
let node = pair.keyNode.resolved
|
|
guard case let .scalar(scalar) = node else {
|
|
// Collection keys do not arise from parsing; fall back to the
|
|
// resolved key text so equal spellings still collide.
|
|
return PureYAML.Tagged.MappingKeyIdentity(value: .string(pair.key), tag: node.tag?.rawValue)
|
|
}
|
|
return PureYAML.Tagged.MappingKeyIdentity(value: scalar.value, tag: effectiveTag(of: scalar)?.rawValue)
|
|
}
|
|
|
|
static func effectiveTag(of scalar: PureYAML.Tagged.Scalar) -> PureYAML.Tagged.Tag? {
|
|
if let tag = scalar.tag {
|
|
return tag
|
|
}
|
|
switch scalar.value {
|
|
case .null:
|
|
return .null
|
|
case .bool:
|
|
return .bool
|
|
case .int:
|
|
return .int
|
|
case .double:
|
|
return .float
|
|
case .string:
|
|
return .string
|
|
case .sequence, .mapping:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
/// Reports unsupported built-in tags and built-in tags applied to the wrong node kind, on the
|
|
/// node itself and on each mapping key.
|
|
public static var builtInTagsAreSupportedAndKindConsistent: PureYAML.Validation.Validation<Node, Node> {
|
|
PureYAML.Validation.Validation(description: "Built-in tags are supported and kind-consistent") { context in
|
|
var errors: [PureYAML.Validation.ValidationError] = []
|
|
if case let .mapping(mapping) = context.subject {
|
|
errors.append(contentsOf: mapping.pairs.compactMap { pair in
|
|
keyTagIssue(for: pair, at: context.path.appending(.key(pair.key)))
|
|
})
|
|
}
|
|
|
|
guard let tag = context.subject.tag, tag.isBuiltIn else {
|
|
return errors
|
|
}
|
|
|
|
if let expectedKind = tag.expectedKind, expectedKind != context.subject.kind {
|
|
errors.append(
|
|
PureYAML.Validation.ValidationError(
|
|
severity: .error,
|
|
reason: "Tag '\(tag.rawValue)' expects a \(expectedKind) node, found \(context.subject.kind)",
|
|
path: context.path,
|
|
),
|
|
)
|
|
return errors
|
|
}
|
|
|
|
if !tag.isSupportedBuiltInTag {
|
|
errors.append(
|
|
PureYAML.Validation.ValidationError(
|
|
severity: .error,
|
|
reason: "Unsupported built-in tag '\(tag.rawValue)'",
|
|
path: context.path,
|
|
),
|
|
)
|
|
}
|
|
|
|
return errors
|
|
} when: { context in
|
|
if context.subject.tag?.isBuiltIn == true {
|
|
return true
|
|
}
|
|
if case let .mapping(mapping) = context.subject {
|
|
return mapping.pairs.contains { $0.keyTag?.isBuiltIn == true }
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
static func keyTagIssue(
|
|
for pair: PureYAML.Tagged.Pair,
|
|
at path: PureYAML.Validation.Path,
|
|
) -> PureYAML.Validation.ValidationError? {
|
|
guard let tag = pair.keyTag, tag.isBuiltIn else {
|
|
return nil
|
|
}
|
|
if tag == .merge, pair.key == "<<" {
|
|
return nil
|
|
}
|
|
if tag == .merge {
|
|
return PureYAML.Validation.ValidationError(
|
|
severity: .error,
|
|
reason: "Tag '\(tag.rawValue)' is only supported on '<<' mapping keys",
|
|
path: path,
|
|
)
|
|
}
|
|
if let expectedKind = tag.expectedKind, expectedKind != .scalar {
|
|
return PureYAML.Validation.ValidationError(
|
|
severity: .error,
|
|
reason: "Tag '\(tag.rawValue)' expects a scalar mapping key",
|
|
path: path,
|
|
)
|
|
}
|
|
guard tag.isSupportedBuiltInTag else {
|
|
return PureYAML.Validation.ValidationError(
|
|
severity: .error,
|
|
reason: "Unsupported built-in tag '\(tag.rawValue)' on mapping key",
|
|
path: path,
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
}
|