Skip to content

Commit 57d4b50

Browse files
sichanyooSichan Yoo
andauthored
fix: Add NodeInfo struct for SmithyCBOR (#935)
* Add NodeInfo struct for SmithyCBOR, like it is in SmithyJSON. * Add Comparable conformance to NodeInfo. --------- Co-authored-by: Sichan Yoo <[email protected]>
1 parent e1638ed commit 57d4b50

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

Sources/SmithyCBOR/NodeInfo.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
public struct NodeInfo: Equatable, Comparable {
9+
/// The name for this CBOR node, or an empty string if none.
10+
public let name: String
11+
12+
public init(_ name: String) {
13+
self.name = name
14+
}
15+
16+
public static func < (lhs: NodeInfo, rhs: NodeInfo) -> Bool {
17+
return lhs.name < rhs.name
18+
}
19+
}
20+
21+
extension NodeInfo: ExpressibleByStringLiteral {
22+
public typealias StringLiteralType = String
23+
24+
public init(stringLiteral value: String) {
25+
self.init(value)
26+
}
27+
}

Sources/SmithyCBOR/Reader/Reader.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import Foundation
1818

1919
@_spi(SmithyReadWrite)
2020
public final class Reader: SmithyReader {
21-
public typealias NodeInfo = String
22-
2321
public let cborValue: CBORType?
2422
public let nodeInfo: NodeInfo
2523
public internal(set) var children: [Reader] = []
@@ -49,12 +47,12 @@ public final class Reader: SmithyReader {
4947
switch cborValue {
5048
case .map(let map):
5149
for (key, value) in map {
52-
let child = Reader(nodeInfo: key, cborValue: value, parent: parent)
50+
let child = Reader(nodeInfo: .init(key), cborValue: value, parent: parent)
5351
children.append(child)
5452
}
5553
case .array(let array):
5654
for (index, value) in array.enumerated() {
57-
let child = Reader(nodeInfo: "\(index)", cborValue: value, parent: parent)
55+
let child = Reader(nodeInfo: .init("\(index)"), cborValue: value, parent: parent)
5856
children.append(child)
5957
}
6058
default:
@@ -190,7 +188,7 @@ public final class Reader: SmithyReader {
190188
guard case .map(let map) = cborValue else { return nil }
191189
var dict = [String: Value]()
192190
for (key, _) in map {
193-
let reader = self[key]
191+
let reader = self[.init(key)]
194192
do {
195193
let value = try valueReadingClosure(reader)
196194
dict[key] = value

Sources/SmithyCBOR/Writer/Writer.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import Foundation
1818

1919
@_spi(SmithyReadWrite)
2020
public final class Writer: SmithyWriter {
21-
public typealias NodeInfo = String
22-
2321
let nodeInfo: NodeInfo
2422
var cborValue: CBORType?
2523
var children: [Writer] = []
@@ -52,7 +50,7 @@ public final class Writer: SmithyWriter {
5250
guard !(child.cborValue == nil && child.children.isEmpty) else {
5351
continue
5452
}
55-
encoder.encode(.text(child.nodeInfo)) // Key for the child
53+
encoder.encode(.text(child.nodeInfo.name)) // Key for the child
5654

5755
if let cborValue = child.cborValue {
5856
encoder.encode(cborValue) // Encode the value directly
@@ -159,7 +157,7 @@ public final class Writer: SmithyWriter {
159157
guard let value else { return }
160158
var map: [String: CBORType] = [:]
161159
for (key, val) in value {
162-
let writer = self[key]
160+
let writer = self[.init(key)]
163161
try valueWritingClosure(val, writer)
164162

165163
if let cborValue = extractCBORValue(from: writer) {
@@ -202,7 +200,7 @@ public final class Writer: SmithyWriter {
202200
var childMap: [String: CBORType] = [:]
203201
for child in writer.children {
204202
if let childValue = extractCBORValue(from: child) {
205-
childMap[child.nodeInfo] = childValue
203+
childMap[child.nodeInfo.name] = childValue
206204
}
207205
}
208206
// Return a map if any children provided a value

0 commit comments

Comments
 (0)