TOON 3.0 Specification Compliance#3
Conversation
|
@mattt Friendly ping on this one 🙂 |
|
@alexey1312 Thanks for the ping. Taking a look now... |
There was a problem hiding this comment.
Pull request overview
This PR updates the TOONEncoder to comply with the TOON 3.0 specification, building upon the existing 2.1 implementation. The changes introduce two major new features from TOON 3.0 (flattenDepth control and collision avoidance) while maintaining backward compatibility with all existing 2.1 features.
Key Changes:
- Added
flattenDepthproperty to limit the depth of key folding (default: unlimited) - Implemented collision avoidance to prevent folded keys from conflicting with sibling literal keys
- Updated
specVersionto "3.0" to reflect compliance with the latest specification - Enhanced canonical number formatting with explicit
minimumFractionDigits = 0configuration
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| Sources/TOONEncoder/TOONEncoder.swift | Added specVersion constant, flattenDepth property, expandPaths property (unused), updated key folding logic to support depth limits and collision detection, enhanced number formatter for canonical formatting |
| Tests/TOONEncoderTests/TOONEncoderTests.swift | Added comprehensive test coverage for key folding (6 tests), flattenDepth behavior (4 tests), and collision avoidance (2 tests); added version declaration test |
| README.md | Updated documentation to reflect TOON 3.0 compliance, added sections for key folding and flatten depth with examples, updated spec URL to point to toon-format/spec repository |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (1)
Sources/TOONEncoder/TOONEncoder.swift:316
- Lines 281-292 and 309-316 contain redundant if-else branches. In both cases, whether
keyOrder.isEmptyor not, the same content ("\(encodedKey):") is written. The only difference is whetherencodeObjectis called afterward. Consider simplifying to unconditionally write the key, then conditionally callencodeObjectonly whenkeyOrderis not empty:
write(depth: depth, content: "\(encodedKey):", to: &output)
if !keyOrder.isEmpty {
encodeObject(values, keyOrder: keyOrder, output: &output, depth: depth + 1, allowFolding: !hitDepthLimit)
} case .object(let values, let keyOrder):
if keyOrder.isEmpty {
write(depth: depth, content: "\(encodedKey):", to: &output)
} else {
write(depth: depth, content: "\(encodedKey):", to: &output)
encodeObject(
values,
keyOrder: keyOrder,
output: &output,
depth: depth + 1,
allowFolding: !hitDepthLimit
)
}
}
return
}
// Regular encoding without folding
let encodedKey = encodeKey(key)
switch value {
case .null, .bool, .int, .double, .string, .date, .url, .data:
if let encodedValue = encodePrimitive(value, delimiter: delimiter.rawValue, inObject: true) {
write(depth: depth, content: "\(encodedKey): \(encodedValue)", to: &output)
}
case .array(let array):
encodeArray(key: key, array: array, output: &output, depth: depth)
case .object(let values, let keyOrder):
if keyOrder.isEmpty {
write(depth: depth, content: "\(encodedKey):", to: &output)
} else {
write(depth: depth, content: "\(encodedKey):", to: &output)
encodeObject(values, keyOrder: keyOrder, output: &output, depth: depth + 1)
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@alexey1312 Thanks so much for your contribution. This is looking good! I just made a few changes, most notably removing the unused |
|
This is now available in version 0.2.0. |
This PR updates TOONEncoder to fully comply with the TOON v3.0 specification.
Changes
Updated: Specification Version
TOONEncoder.specVersionnow returns"3.0"Added:
flattenDepthProperty (TOON 3.0)Int.max(unlimited)flattenDepth = 2folds{a: {b: {c: 1}}}→a.b:then nestedc: 1Added: Collision Avoidance (TOON 3.0)
Fixed: Nested Folding After Depth Limit
flattenDepthis reached, remaining nested structures are encoded without further foldingPreviously Implemented (TOON 2.1)
.safemodeTest Coverage
flattenDepthbehavior