-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persistent collections updates (part 3) #176
Merged
lorentey
merged 13 commits into
apple:feature/PersistentCollections
from
lorentey:PersistentCollections-updates
Sep 14, 2022
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
afdb471
[PersistentCollections] “trie”, “subNode” → “child”, “data” → “item”
lorentey ca19ecb
[PersistentDictionary] Don’t copy collision node contents into arrays
lorentey 3bc9b42
[PersistentCollections] Standard invariant checks
lorentey 3441a48
[PersistentDictionary] rootNode → root
lorentey f36649d
[PersistentDictionary] Review Iterator
lorentey 92f3d66
[PersistentDictionary] Move members to corresponding protocol conform…
lorentey 65bf478
[PersistentDictionary] Remove capacity property
lorentey 493aa91
[PersistentDictionary] Conform to Codable
lorentey dd76591
[_CollectionsUtilities] New module. Use it to unify description/debug…
lorentey 1e41efa
[PersistentCollections] Slap @inlinable on everything
lorentey 4aff69e
[_CollectionUtilities] More additions
lorentey b1cf1ee
[PersistentCollections] Start splitting up _HashPath into _HashValue …
lorentey 77edbef
[_CollectionsUtilities] Restore original debug descriptions
lorentey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
Sources/PersistentCollections/PersistentDictionary+Codable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift Collections open source project | ||
// | ||
// Copyright (c) 2022 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Code in this file is a slightly adapted variant of `Dictionary`'s `Codable` | ||
// implementation in the Standard Library as of Swift 5.7. | ||
// `PersistentDictionary` therefore encodes/decodes itself exactly the same as | ||
// `Dictionary`, and the two types can each decode data encoded by the other. | ||
|
||
/// A wrapper for dictionary keys which are Strings or Ints. | ||
internal struct _DictionaryCodingKey: CodingKey { | ||
internal let stringValue: String | ||
internal let intValue: Int? | ||
|
||
internal init(stringValue: String) { | ||
self.stringValue = stringValue | ||
self.intValue = Int(stringValue) | ||
} | ||
|
||
internal init(intValue: Int) { | ||
self.stringValue = "\(intValue)" | ||
self.intValue = intValue | ||
} | ||
|
||
fileprivate init(codingKey: CodingKey) { | ||
self.stringValue = codingKey.stringValue | ||
self.intValue = codingKey.intValue | ||
} | ||
} | ||
|
||
extension PersistentDictionary: Encodable | ||
where Key: Encodable, Value: Encodable | ||
{ | ||
public func encode(to encoder: Encoder) throws { | ||
if Key.self == String.self { | ||
// Since the keys are already Strings, we can use them as keys directly. | ||
var container = encoder.container(keyedBy: _DictionaryCodingKey.self) | ||
for (key, value) in self { | ||
let codingKey = _DictionaryCodingKey(stringValue: key as! String) | ||
try container.encode(value, forKey: codingKey) | ||
} | ||
} else if Key.self == Int.self { | ||
// Since the keys are already Ints, we can use them as keys directly. | ||
var container = encoder.container(keyedBy: _DictionaryCodingKey.self) | ||
for (key, value) in self { | ||
let codingKey = _DictionaryCodingKey(intValue: key as! Int) | ||
try container.encode(value, forKey: codingKey) | ||
} | ||
} else if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *), | ||
Key.self is CodingKeyRepresentable.Type { | ||
// Since the keys are CodingKeyRepresentable, we can use the `codingKey` | ||
// to create `_DictionaryCodingKey` instances. | ||
var container = encoder.container(keyedBy: _DictionaryCodingKey.self) | ||
for (key, value) in self { | ||
let codingKey = (key as! CodingKeyRepresentable).codingKey | ||
let dictionaryCodingKey = _DictionaryCodingKey(codingKey: codingKey) | ||
try container.encode(value, forKey: dictionaryCodingKey) | ||
} | ||
} else { | ||
// Keys are Encodable but not Strings or Ints, so we cannot arbitrarily | ||
// convert to keys. We can encode as an array of alternating key-value | ||
// pairs, though. | ||
var container = encoder.unkeyedContainer() | ||
for (key, value) in self { | ||
try container.encode(key) | ||
try container.encode(value) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension PersistentDictionary: Decodable | ||
where Key: Decodable, Value: Decodable | ||
{ | ||
/// Creates a new dictionary by decoding from the given decoder. | ||
/// | ||
/// This initializer throws an error if reading from the decoder fails, or | ||
/// if the data read is corrupted or otherwise invalid. | ||
/// | ||
/// - Parameter decoder: The decoder to read data from. | ||
public init(from decoder: Decoder) throws { | ||
self.init() | ||
|
||
if Key.self == String.self { | ||
// The keys are Strings, so we should be able to expect a keyed container. | ||
let container = try decoder.container(keyedBy: _DictionaryCodingKey.self) | ||
for key in container.allKeys { | ||
let value = try container.decode(Value.self, forKey: key) | ||
self[key.stringValue as! Key] = value | ||
} | ||
} else if Key.self == Int.self { | ||
// The keys are Ints, so we should be able to expect a keyed container. | ||
let container = try decoder.container(keyedBy: _DictionaryCodingKey.self) | ||
for key in container.allKeys { | ||
guard key.intValue != nil else { | ||
// We provide stringValues for Int keys; if an encoder chooses not to | ||
// use the actual intValues, we've encoded string keys. | ||
// So on init, _DictionaryCodingKey tries to parse string keys as | ||
// Ints. If that succeeds, then we would have had an intValue here. | ||
// We don't, so this isn't a valid Int key. | ||
var codingPath = decoder.codingPath | ||
codingPath.append(key) | ||
throw DecodingError.typeMismatch( | ||
Int.self, | ||
DecodingError.Context( | ||
codingPath: codingPath, | ||
debugDescription: "Expected Int key but found String key instead." | ||
) | ||
) | ||
} | ||
|
||
let value = try container.decode(Value.self, forKey: key) | ||
self[key.intValue! as! Key] = value | ||
} | ||
} else if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *), | ||
let keyType = Key.self as? CodingKeyRepresentable.Type { | ||
// The keys are CodingKeyRepresentable, so we should be able to expect | ||
// a keyed container. | ||
let container = try decoder.container(keyedBy: _DictionaryCodingKey.self) | ||
for codingKey in container.allKeys { | ||
guard let key: Key = keyType.init(codingKey: codingKey) as? Key else { | ||
throw DecodingError.dataCorruptedError( | ||
forKey: codingKey, | ||
in: container, | ||
debugDescription: "Could not convert key to type \(Key.self)" | ||
) | ||
} | ||
let value: Value = try container.decode(Value.self, forKey: codingKey) | ||
self[key] = value | ||
} | ||
} else { | ||
// We should have encoded as an array of alternating key-value pairs. | ||
var container = try decoder.unkeyedContainer() | ||
|
||
// We're expecting to get pairs. If the container has a known count, it | ||
// had better be even; no point in doing work if not. | ||
if let count = container.count { | ||
guard count % 2 == 0 else { | ||
throw DecodingError.dataCorrupted( | ||
DecodingError.Context( | ||
codingPath: decoder.codingPath, | ||
debugDescription: "Expected collection of key-value pairs; encountered odd-length array instead." | ||
) | ||
) | ||
} | ||
} | ||
|
||
while !container.isAtEnd { | ||
let key = try container.decode(Key.self) | ||
|
||
guard !container.isAtEnd else { | ||
throw DecodingError.dataCorrupted( | ||
DecodingError.Context( | ||
codingPath: decoder.codingPath, | ||
debugDescription: "Unkeyed container reached end before value in key-value pair." | ||
) | ||
) | ||
} | ||
|
||
let value = try container.decode(Value.self) | ||
self[key] = value | ||
} | ||
} | ||
} | ||
} | ||
|
16 changes: 0 additions & 16 deletions
16
Sources/PersistentCollections/PersistentDictionary+Decodable.swift
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
Sources/PersistentCollections/PersistentDictionary+Encodable.swift
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The according tests in https://github.com/lorentey/swift-collections/blob/PersistentCollections-updates/Tests/PersistentCollectionsTests/PersistentCollections%20Tests.swift#L970-L1033 should preferably be activated upon the time the feature is added.