Skip to content

Commit 1c9a21e

Browse files
committed
sty: comments
1 parent 6849413 commit 1c9a21e

2 files changed

Lines changed: 56 additions & 40 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// ExCodable.swift
3+
// ExCodable
4+
//
5+
// Created by Mr. Ming on 2021-07-01.
6+
// Copyright (c) 2021 Mr. Ming <minglq.9@gmail.com>. Released under the MIT license.
7+
//
8+
9+
import Foundation
10+
11+
public extension ExCodable {
12+
@available(*, deprecated, renamed: "encode(to:with:nonnull:throws:)")
13+
func encode(with keyMapping: [KeyMap<Self>], using encoder: Encoder) {
14+
try? encode(to: encoder, with: keyMapping)
15+
}
16+
@available(*, deprecated, renamed: "decode(from:with:nonnull:throws:)")
17+
mutating func decode(with keyMapping: [KeyMap<Self>], using decoder: Decoder) {
18+
try? decode(from: decoder, with: keyMapping)
19+
}
20+
@available(*, deprecated, renamed: "decodeReference(from:with:nonnull:throws:)")
21+
func decodeReference(with keyMapping: [KeyMap<Self>], using decoder: Decoder) {
22+
try? decodeReference(from: decoder, with: keyMapping)
23+
}
24+
}
25+
26+
@available(*, deprecated, renamed: "append(decodingTypeConverter:)")
27+
public protocol KeyedDecodingContainerCustomTypeConversion: ExCodableDecodingTypeConverter {
28+
func decodeForTypeConversion<T: Decodable, K: CodingKey>(_ container: KeyedDecodingContainer<K>, codingKey: K, as type: T.Type) -> T?
29+
}
30+
@available(*, deprecated)
31+
public extension KeyedDecodingContainerCustomTypeConversion {
32+
func decode<T: Decodable, K: CodingKey>(_ container: KeyedDecodingContainer<K>, codingKey: K, as type: T.Type) throws -> T? {
33+
return decodeForTypeConversion(container, codingKey: codingKey, as: type)
34+
}
35+
}

‎Sources/ExCodable/ExCodable.swift‎

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ public protocol ExCodable: Codable {
2424
static var keyMapping: [KeyMap<Root>] { get }
2525
}
2626

27+
// default implementation for Encodable
28+
public extension ExCodable where Root == Self {
29+
func encode(to encoder: Encoder) throws {
30+
try encode(to: encoder, with: Self.keyMapping)
31+
}
32+
}
33+
34+
// MARK: - keyMapping
35+
36+
// encode/decode
2737
public extension ExCodable {
38+
static var keyMapping: [KeyMap<Root>] { [] } // default implementation for optional property
2839
func encode(to encoder: Encoder, with keyMapping: [KeyMap<Self>], nonnull: Bool = false, throws: Bool = false) throws {
2940
try keyMapping.forEach { try $0.encode(self, encoder, nonnull, `throws`) }
3041
}
@@ -35,13 +46,6 @@ public extension ExCodable {
3546
try keyMapping.forEach { try $0.decodeReference?(self, decoder, nonnull, `throws`) }
3647
}
3748
}
38-
public extension ExCodable where Root == Self {
39-
func encode(to encoder: Encoder) throws {
40-
try encode(to: encoder, with: Self.keyMapping)
41-
}
42-
}
43-
44-
// MARK: -
4549

4650
public final class KeyMap<Root: Codable> {
4751
fileprivate let encode: (_ root: Root, _ encoder: Encoder, _ nonnullAll: Bool, _ throwsAll: Bool) throws -> Void
@@ -119,7 +123,7 @@ public extension Decoder { // , abortIfNull nonnull: Bool = false, abortOnError
119123
}
120124
}
121125

122-
// MARK: -
126+
// MARK: - Encoder&Decoder
123127

124128
public extension Encoder {
125129

@@ -225,13 +229,18 @@ public extension Decoder {
225229
}
226230
}
227231

228-
private struct ExCodingKey: CodingKey {
229-
let stringValue: String, intValue: Int?
232+
// MARK: - ExCodingKey
233+
234+
private struct ExCodingKey {
235+
public let stringValue: String, intValue: Int?
230236
init(_ stringValue: String) { (self.stringValue, self.intValue) = (stringValue, nil) }
231237
init(_ stringValue: Substring) { self.init(String(stringValue)) }
232-
init?(stringValue: String) { self.init(stringValue) }
233238
init(_ intValue: Int) { (self.intValue, self.stringValue) = (intValue, String(intValue)) }
234-
init?(intValue: Int) { self.init(intValue) }
239+
}
240+
241+
extension ExCodingKey: CodingKey {
242+
public init?(stringValue: String) { self.init(stringValue) }
243+
public init?(intValue: Int) { self.init(intValue) }
235244
}
236245

237246
// MARK: - alternative-keys + nested-keys + type-conversion
@@ -498,31 +507,3 @@ extension JSONDecoder: DataDecoder {}
498507
extension PropertyListEncoder: DataEncoder {}
499508
extension PropertyListDecoder: DataDecoder {}
500509
#endif
501-
502-
// MARK: - #### DEPRECATED ####
503-
504-
public extension ExCodable {
505-
@available(*, deprecated, renamed: "encode(to:with:nonnull:throws:)")
506-
func encode(with keyMapping: [KeyMap<Self>], using encoder: Encoder) {
507-
try? encode(to: encoder, with: keyMapping)
508-
}
509-
@available(*, deprecated, renamed: "decode(from:with:nonnull:throws:)")
510-
mutating func decode(with keyMapping: [KeyMap<Self>], using decoder: Decoder) {
511-
try? decode(from: decoder, with: keyMapping)
512-
}
513-
@available(*, deprecated, renamed: "decodeReference(from:with:nonnull:throws:)")
514-
func decodeReference(with keyMapping: [KeyMap<Self>], using decoder: Decoder) {
515-
try? decodeReference(from: decoder, with: keyMapping)
516-
}
517-
}
518-
519-
@available(*, deprecated, renamed: "append(decodingTypeConverter:)")
520-
public protocol KeyedDecodingContainerCustomTypeConversion: ExCodableDecodingTypeConverter {
521-
func decodeForTypeConversion<T: Decodable, K: CodingKey>(_ container: KeyedDecodingContainer<K>, codingKey: K, as type: T.Type) -> T?
522-
}
523-
@available(*, deprecated)
524-
public extension KeyedDecodingContainerCustomTypeConversion {
525-
func decode<T: Decodable, K: CodingKey>(_ container: KeyedDecodingContainer<K>, codingKey: K, as type: T.Type) throws -> T? {
526-
return decodeForTypeConversion(container, codingKey: codingKey, as: type)
527-
}
528-
}

0 commit comments

Comments
 (0)