Skip to content

Commit 08a6ad5

Browse files
committed
Doc Comments
1 parent c871e3a commit 08a6ad5

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

Sources/KeyValueDecoder.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,36 @@
3232
import Foundation
3333
import CoreFoundation
3434

35+
/// Top level encoder that converts `[String: Any]`, `[Any]` or `Any` into `Codable` types.
3536
public final class KeyValueDecoder {
3637

38+
/// Contextual user-provided information for use during encoding.
3739
public var userInfo: [CodingUserInfoKey: Any]
40+
41+
/// The strategy to use for decoding `nil`. Defaults to `Optional<Any>.none` which can be decoded to any optional type.
3842
public var nilDecodingStrategy: NilDecodingStrategy = .default
3943

44+
/// Initializes `self` with default strategies.
4045
public init () {
4146
self.userInfo = [:]
4247
}
4348

44-
public func decode<T: Decodable>(_ type: T.Type, from value: Any) throws -> T {
49+
///
50+
/// Decodes any loosely typed key value into a `Decodable` type.
51+
/// - Parameters:
52+
/// - type: The `Decodable` type to decode.
53+
/// - value: The value to decode. May be `[Any]`, `[String: Any]` or any supported primitive `Bool`,
54+
/// `String`, `Int`, `UInt`, `URL`, `Data` or `Decimal`.
55+
/// - Returns: The decoded instance of `type`.
56+
///
57+
/// - Throws: `DecodingError` if a value cannot be decoded. The context will contain a keyPath of the failed property.
58+
public func decode<T: Decodable>(_ type: T.Type = T.self, from value: Any) throws -> T {
4559
let container = SingleContainer(value: value, codingPath: [], userInfo: userInfo, nilDecodingStrategy: nilDecodingStrategy)
4660
return try container.decode(type)
4761
}
4862

49-
public typealias NilDecodingStrategy = KeyValueEncoder.NilEncodingStrategy
63+
/// Strategy used to decode nil values.
64+
public typealias NilDecodingStrategy = NilCodingStrategy
5065
}
5166

5267
extension KeyValueDecoder {

Sources/KeyValueEncoder.swift

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,48 @@
3131

3232
import Foundation
3333

34+
/// Top level encoder that converts `Codable` instances into loosely typed `[String: Any]`, `[Any]` or `Any`.
3435
public final class KeyValueEncoder {
3536

37+
/// Contextual user-provided information for use during encoding.
3638
public var userInfo: [CodingUserInfoKey: Any]
39+
40+
/// The strategy to use for encoding `nil`. Defaults to `Optional<Any>.none` which can be cast to any optional type.
3741
public var nilEncodingStrategy: NilEncodingStrategy = .default
3842

43+
/// Initializes `self` with default strategies.
3944
public init () {
4045
self.userInfo = [:]
4146
}
4247

48+
/// Encodes a value into a loosely typed key value type. May be a container `[Any]`, `[String: Any]`
49+
/// or any supported plist primitive `Bool`, `String`, `Int`, `UInt`, `URL`, `Data` or `Decimal`.
50+
/// - Parameter value: The `Encodable` value to encode.
51+
/// - Returns: The encoded value.
4352
public func encode<T>(_ value: T) throws -> Any? where T: Encodable {
4453
try encodeValue(value).getValue(strategy: nilEncodingStrategy)
4554
}
4655

47-
func encodeValue<T>(_ value: T) throws -> EncodedValue where T: Encodable {
48-
try Encoder(userInfo: userInfo, nilEncodingStrategy: nilEncodingStrategy).encodeToValue(value)
49-
}
56+
/// Strategy used to encode nil values.
57+
public typealias NilEncodingStrategy = NilCodingStrategy
58+
}
5059

51-
public enum NilEncodingStrategy {
52-
case removed
53-
case placeholder(Any, isNull: (Any) -> Bool)
60+
/// Strategy used to encode and decode nil values.
61+
public enum NilCodingStrategy {
62+
/// `nil` values are removed
63+
case removed
5464

55-
public static let `default` = NilEncodingStrategy.placeholder(Optional<Any>.none as Any, isNull: isOptionalNone)
56-
public static let stringNull = NilEncodingStrategy.placeholder("$null", isNull: { ($0 as? String == "$null") })
57-
public static let nsNull = NilEncodingStrategy.placeholder(NSNull(), isNull: { $0 is NSNull })
58-
}
65+
/// `nil` values are substituted with a placeholder value
66+
case placeholder(Any, isNull: (Any) -> Bool)
67+
68+
/// `nil` values are substituted with `Optional<Any>.none`. Can be cast to any optional type.
69+
public static let `default` = NilCodingStrategy.placeholder(Optional<Any>.none as Any, isNull: isOptionalNone)
70+
71+
/// `nil` values are substituted with `"$null"` placeholder string. Compatible with `PropertyListEncoder`.
72+
public static let stringNull = NilCodingStrategy.placeholder("$null", isNull: { ($0 as? String == "$null") })
73+
74+
/// `nil` values are substituted with `"NSNull()"`. Compatible with `JSONSerialization`.
75+
public static let nsNull = NilCodingStrategy.placeholder(NSNull(), isNull: { $0 is NSNull })
5976
}
6077

6178
extension KeyValueEncoder {
@@ -82,6 +99,10 @@ extension KeyValueEncoder {
8299
}
83100
}
84101
}
102+
103+
func encodeValue<T: Encodable>(_ value: T) throws -> EncodedValue{
104+
try Encoder(userInfo: userInfo, nilEncodingStrategy: nilEncodingStrategy).encodeToValue(value)
105+
}
85106
}
86107

87108
extension KeyValueEncoder.NilEncodingStrategy {

0 commit comments

Comments
 (0)