docs(api): reject arbitrary any conversion
This commit is contained in:
parent
307d40c053
commit
b6a39f7096
36
Tests/ArbitraryAnyDecisionTests.swift
Normal file
36
Tests/ArbitraryAnyDecisionTests.swift
Normal file
@ -0,0 +1,36 @@
|
||||
@testable import PureYAML
|
||||
import Testing
|
||||
|
||||
@Suite("Arbitrary Any API Decision")
|
||||
struct ArbitraryAnyDecisionTests {
|
||||
@Test("Model values preserve diagnostics that dictionary-shaped projections erase")
|
||||
func test_modelValuesPreserveDiagnosticsThatDictionaryShapedProjectionsErase() {
|
||||
let value = PureYAML.Model.Value.mapping(.init([
|
||||
.init(key: "title", value: .string("First")),
|
||||
.init(key: "title", value: .string("Second")),
|
||||
.init(key: "slug", value: .string("example")),
|
||||
]))
|
||||
|
||||
guard case let .mapping(mapping) = value else {
|
||||
recordIssue("expected mapping fixture")
|
||||
return
|
||||
}
|
||||
|
||||
let projectedDictionary = Dictionary(
|
||||
mapping.pairs.map { ($0.key, $0.value) },
|
||||
uniquingKeysWith: { _, second in second },
|
||||
)
|
||||
let validationResult = PureYAML.Validation.Validator().collect(value)
|
||||
|
||||
#expect(mapping.pairs.map(\.key) == ["title", "title", "slug"])
|
||||
#expect(projectedDictionary.keys.sorted() == ["slug", "title"])
|
||||
#expect(projectedDictionary["title"] == .string("Second"))
|
||||
#expect(validationResult.issues == [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'title'",
|
||||
path: .init([.key("title")]),
|
||||
),
|
||||
])
|
||||
}
|
||||
}
|
||||
@ -27,6 +27,8 @@ blocking browser deployment.
|
||||
- No C source in the public package.
|
||||
- No external SwiftPM dependency.
|
||||
- No JavaScript parser or build-time generator.
|
||||
- No arbitrary `Any` load or dump API that collapses YAML mappings into
|
||||
dictionary-shaped values before validation.
|
||||
- No claim of complete YAML 1.2 coverage until the test corpus proves it.
|
||||
|
||||
## Current Architecture
|
||||
@ -136,6 +138,21 @@ Typed decoding runs default validation before Swift `Decodable` construction.
|
||||
That keeps ambiguous YAML states, such as duplicate mapping keys, from being
|
||||
silently collapsed by keyed property lookup.
|
||||
|
||||
## Rejected API Shape: Arbitrary Any
|
||||
|
||||
PureYAML does not provide a public arbitrary `Any` loader or dumper. Unknown YAML
|
||||
must stay in `PureYAML.Model.Value`; known YAML should use typed `Codable`.
|
||||
|
||||
This is a deterministic validation decision. A Swift dictionary-shaped value
|
||||
cannot preserve all YAML mapping entries when keys repeat, cannot represent
|
||||
complex mapping keys with the current public model, and makes merge/tag
|
||||
normalization ambiguous. The research constructor also includes
|
||||
Foundation-specific scalar conversions, which would break the dependency-free
|
||||
and WASM-compatible library boundary.
|
||||
|
||||
Application code may project `Model.Value` into JSON-like dictionaries after
|
||||
validation, but that projection must own the data-loss policy explicitly.
|
||||
|
||||
## Planned Compatibility Work
|
||||
|
||||
Compatibility should be added in small, test-backed slices:
|
||||
|
||||
@ -25,7 +25,31 @@ See [../ATTRIBUTION.md](../ATTRIBUTION.md) for the project attribution.
|
||||
| Decode a `Decodable` type from YAML | `PureYAML.decode(_:from:)` | Runs default validation before typed decoding. |
|
||||
| Encode an `Encodable` value to YAML | `PureYAML.encodeToYAML(_:options:)` | Encodes through `PureYAML.Model.Value`, then dumps. |
|
||||
| Validate document structure | `PureYAML.validate(_:using:strict:)` | Default rule reports duplicate mapping keys with exact paths. |
|
||||
| Load arbitrary `Any` dictionaries and arrays | Use `Model.Value` or typed `Codable` | There is no arbitrary `Any` conversion API yet. |
|
||||
| Load arbitrary `Any` dictionaries and arrays | Use `Model.Value` or typed `Codable` | There is deliberately no public arbitrary `Any` conversion API. Dictionary-shaped values erase YAML diagnostics. |
|
||||
|
||||
## Arbitrary Any Decision
|
||||
|
||||
PureYAML does not expose `loadAny`, `dumpAny`, or dictionary-and-array `Any`
|
||||
entry points. That is a deliberate compatibility boundary, not a missing
|
||||
wrapper.
|
||||
|
||||
The research implementation's arbitrary-value constructor is useful to study,
|
||||
but its observable shape conflicts with PureYAML's first principles:
|
||||
|
||||
- mapping conversion becomes dictionary-shaped, so duplicate keys and insertion
|
||||
order are not a first-class contract;
|
||||
- merge keys may be flattened during construction instead of remaining visible
|
||||
to validation;
|
||||
- scalar construction includes Foundation-specific values such as binary data,
|
||||
dates, and null objects;
|
||||
- custom constructors can hide whether a document was parsed, normalized,
|
||||
flattened, or validated.
|
||||
|
||||
For unknown schemas, use `PureYAML.Model.Value` and validate before projecting
|
||||
into project-specific values. For known schemas, use typed `Codable`. If a
|
||||
caller truly needs a JSON-like dictionary after validation, build that projection
|
||||
in the application layer where duplicate-key and unsupported-shape policies are
|
||||
explicit.
|
||||
|
||||
## Supported Today
|
||||
|
||||
|
||||
@ -48,6 +48,10 @@ Those tests are the executable contract for the short-form examples.
|
||||
| Encode a typed value to a value tree | `PureYAML.encode(_:)` | `PureYAML.Model.Value` |
|
||||
| Encode a typed value to YAML | `PureYAML.encodeToYAML(_:options:)` | `String` |
|
||||
|
||||
PureYAML intentionally does not expose arbitrary `Any` dictionary-and-array load
|
||||
or dump APIs. Use `Model.Value` for unknown schemas and typed `Codable` for
|
||||
known schemas.
|
||||
|
||||
## Model Usage
|
||||
|
||||
Use `PureYAML.Model.Value` when the caller needs to inspect YAML structure,
|
||||
@ -68,6 +72,11 @@ Mappings preserve insertion order and retain duplicate keys. That is deliberate:
|
||||
validation can report duplicate-key diagnostics instead of losing information by
|
||||
collapsing a mapping into a Swift dictionary too early.
|
||||
|
||||
If a caller needs a JSON-like dictionary after validation, build that projection
|
||||
in application code after deciding how duplicate keys, unsupported tags, merge
|
||||
keys, and complex keys should behave. Keeping that step outside PureYAML makes
|
||||
the lossy boundary explicit.
|
||||
|
||||
## Typed Conversion
|
||||
|
||||
Use typed conversion when the YAML shape is already known. Current coverage
|
||||
|
||||
Loading…
Reference in New Issue
Block a user