Skip to content

Commit

Permalink
add option to sort maps by key
Browse files Browse the repository at this point in the history
so it does not introduce breaking changes this flag is set as default to true

Signed-off-by: Goncalo Frade <[email protected]>
  • Loading branch information
beatt83 committed Nov 22, 2024
1 parent 04ccff1 commit fca4773
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
51 changes: 38 additions & 13 deletions Sources/CBOREncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,26 @@ extension CBOR {
res.reserveCapacity(1 + map.count * (MemoryLayout<A>.size + MemoryLayout<B>.size + 2))
res = map.count.encode(options: options)
res[0] = res[0] | 0b101_00000
for (k, v) in map {
res.append(contentsOf: k.encode(options: options))
res.append(contentsOf: v.encode(options: options))

if options.shouldSortMapKeys {
let sortedKeysWithEncodedKeys = map.keys.map {
(encoded: $0.encode(options: options), key: $0)
}.sorted(by: {
$0.encoded.lexicographicallyPrecedes($1.encoded)
})

sortedKeysWithEncodedKeys.forEach { keyTuple in
res.append(contentsOf: keyTuple.encoded)
guard let value = map[keyTuple.key] else {
return
}
res.append(contentsOf: value.encode(options: options))
}
} else {
for (k, v) in map {
res.append(contentsOf: k.encode(options: options))
res.append(contentsOf: v.encode(options: options))
}
}
return res
}
Expand Down Expand Up @@ -442,16 +459,24 @@ extension CBOR {
if options.forbidNonStringMapKeys {
try ensureStringKey(A.self)
}
let sortedKeysWithEncodedKeys = map.keys.map {
(encoded: $0.encode(options: options), key: $0)
}.sorted(by: {
$0.encoded.lexicographicallyPrecedes($1.encoded)
})

try sortedKeysWithEncodedKeys.forEach { keyTuple in
res.append(contentsOf: keyTuple.encoded)
let encodedVal = try encodeAny(map[keyTuple.key]!, options: options)
res.append(contentsOf: encodedVal)
if options.shouldSortMapKeys {
let sortedKeysWithEncodedKeys = map.keys.map {
(encoded: $0.encode(options: options), key: $0)
}.sorted(by: {
$0.encoded.lexicographicallyPrecedes($1.encoded)
})

try sortedKeysWithEncodedKeys.forEach { keyTuple in
res.append(contentsOf: keyTuple.encoded)
let encodedVal = try encodeAny(map[keyTuple.key]!, options: options)
res.append(contentsOf: encodedVal)
}
} else {
for (k, v) in map {
res.append(contentsOf: k.encode(options: options))
let encodedVal = try encodeAny(v, options: options)
res.append(contentsOf: encodedVal)
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/CBOROptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ public struct CBOROptions {
let forbidNonStringMapKeys: Bool
/// The maximum number of nested items, inclusive, to decode. A maximum set to 0 dissallows anything other than top-level primitives.
let maximumDepth: Int
let shouldSortMapKeys: Bool

public init(
useStringKeys: Bool = false,
dateStrategy: DateStrategy = .taggedAsEpochTimestamp,
forbidNonStringMapKeys: Bool = false,
maximumDepth: Int = .max
maximumDepth: Int = .max,
shouldShortMapKeys: Bool = true
) {
self.useStringKeys = useStringKeys
self.dateStrategy = dateStrategy
self.forbidNonStringMapKeys = forbidNonStringMapKeys
self.maximumDepth = maximumDepth
self.shouldSortMapKeys = shouldShortMapKeys
}

func toCodableEncoderOptions() -> CodableCBOREncoder._Options {
Expand Down
15 changes: 14 additions & 1 deletion Tests/CBOREncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class CBOREncoderTests: XCTestCase {
"a": 1,
"b": [2, 3]
]
let encodedMapToAny = try! CBOR.encodeMap(mapToAny)
let encodedMapToAny = try! CBOR.encodeMap(mapToAny, options: .init(shouldShortMapKeys: true))
XCTAssertEqual(encodedMapToAny, [0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03])

let mapToAnyWithIntKeys: [Int: Any] = [
Expand All @@ -113,6 +113,19 @@ class CBOREncoderTests: XCTestCase {
XCTAssertEqual(err as! CBOREncoderError, CBOREncoderError.nonStringKeyInMap)
}
}

func testEncodeSortedMaps() {
XCTAssertEqual(CBOR.encode(Dictionary<Int, Int>()), [0xa0])

let encoded = CBOR.encode([3: 4, 1: 2])
XCTAssert(encoded == [0xa2, 0x01, 0x02, 0x03, 0x04])

let arr1: CBOR = [1]
let arr2: CBOR = [2,3]
let nestedEnc: [UInt8] = CBOR.encode(["b": arr2, "a": arr1])
let encodedAFirst: [UInt8] = [0xa2, 0x61, 0x61, 0x81, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03]
XCTAssert(nestedEnc == encodedAFirst)
}

func testEncodeTagged() {
let bignum: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] // 2**64
Expand Down

0 comments on commit fca4773

Please sign in to comment.