Skip to content

Commit c0319af

Browse files
committed
proj: readme, version and coding style
1 parent a4ec7bb commit c0319af

3 files changed

Lines changed: 62 additions & 32 deletions

File tree

‎ExCodable.podspec‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Pod::Spec.new do |s|
33
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
44
s.name = "ExCodable"
55
# export LIB_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)
6-
s.version = ENV["LIB_VERSION"] || "0.3.0"
6+
s.version = ENV["LIB_VERSION"] || "0.4.0"
77
s.summary = "Key-Mapping Extensions for Swift Codable"
88
# s.description = "Key-Mapping Extensions for Swift Codable."
99
s.homepage = "https://github.com/iwill/ExCodable"

‎README.md‎

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ En | [中文](https://iwill.im/ExCodable/)
2727

2828
- Extends Swift `Codable` - `Encodable & Decodable`;
2929
- Supports Key-Mapping via `KeyPath` and Coding-Key:
30-
- ExCodable did not read/write memory via unsafe pointers;
30+
- `ExCodable` did not read/write memory via unsafe pointers;
3131
- No need to encode/decode properties one by one;
3232
- Just requires using `var` to declare properties and provide default values;
3333
- In most cases, the `CodingKey` type is no longer necessary, because it will only be used once, `String` literals may be better.
@@ -155,27 +155,50 @@ static var keyMapping: [KeyMap<Self>] = [
155155
]
156156
```
157157

158-
### 6. Custom encode/decode handlers:
158+
### 6. Custom encode/decode:
159159

160160
```swift
161-
static var keyMapping: [KeyMap<Self>] = [
162-
KeyMap(\.int, to: "int"),
163-
KeyMap(\.string, to: "string", encode: { (encoder, stringKeys, test, keyPath) in
164-
encoder[stringKeys.first!] = "dddd"
165-
}, decode: { (test, keyPath, decoder, stringKeys) in
166-
switch test.int {
167-
case 100: test.string = "Continue"
168-
case 200: test.string = "OK"
169-
case 304: test.string = "Not Modified"
170-
case 403: test.string = "Forbidden"
171-
case 404: test.string = "Not Found"
172-
case 418: test.string = "I'm a teapot"
173-
case 500: test.string = "Internal Server Error"
174-
case 200..<400: test.string = "success"
175-
default: test.string = "failure"
161+
struct TestHandlers: Equatable {
162+
var int: Int = 0
163+
var string: String?
164+
}
165+
```
166+
167+
```swift
168+
extension TestHandlers: ExCodable {
169+
170+
private enum Keys: String, CodingKey {
171+
case int, string
172+
}
173+
private static let dddd = "dddd"
174+
175+
static var keyMapping: [KeyMap<Self>] = [
176+
KeyMap(\.int, to: Keys.int),
177+
KeyMap(\.string, to: Keys.string)
178+
]
179+
180+
init(from decoder: Decoder) throws {
181+
decode(with: Self.keyMapping, using: decoder)
182+
if string == nil || string == Self.dddd {
183+
switch int {
184+
case 100: string = "Continue"
185+
case 200: string = "OK"
186+
case 304: string = "Not Modified"
187+
case 403: string = "Forbidden"
188+
case 404: string = "Not Found"
189+
case 418: string = "I'm a teapot"
190+
case 500: string = "Internal Server Error"
191+
case 200..<400: string = "success"
192+
default: string = "failure"
193+
}
176194
}
177-
})
178-
]
195+
}
196+
func encode(to encoder: Encoder) throws {
197+
encode(with: Self.keyMapping, using: encoder)
198+
encoder[Keys.string] = Self.dddd
199+
}
200+
201+
}
179202
```
180203

181204
### 7. Encode/decode constant properties with subscripts:
@@ -255,37 +278,36 @@ XCTAssertEqual(copy2, test)
255278
- [Swift Package Manager](https://swift.org/package-manager/):
256279

257280
```swift
258-
.package(url: "https://github.com/iwill/ExCodable", from: "0.2")
281+
.package(url: "https://github.com/iwill/ExCodable", from: "0.4.0")
259282
```
260283

261284
- [CocoaPods](http://cocoapods.org):
262285

263286
```ruby
264-
pod 'ExCodable', '~> 0.2'
287+
pod 'ExCodable', '~> 0.4.0'
265288
```
266289

267290
- Code Snippets:
268291

292+
> Title: ExCodable
293+
> Summary: Adopte to ExCodable protocol
269294
> Language: Swift
270295
> Platform: All
271-
> Completion: excodable
296+
> Completion: ExCodable
272297
> Availability: Top Level
273298
274299
```swift
275300
<#extension/struct/class#> <#Type#>: ExCodable {
276-
277-
static var <#keyMapping#>: [KeyMap<<#Type#>>] = [
301+
static var <#keyMapping#>: [KeyMap<<#SelfType#>>] = [
278302
KeyMap(\.<#property#>, to: <#"key"#>),
279303
<#...#>
280304
]
281-
282305
init(from decoder: Decoder) throws {
283-
decode(with: Self.<#keyMapping#>, using: decoder)
306+
decode<#Reference#>(with: Self.<#keyMapping#>, using: decoder)
284307
}
285308
func encode(to encoder: Encoder) throws {
286309
encode(with: Self.<#keyMapping#>, using: encoder)
287310
}
288-
289311
}
290312
```
291313

‎Sources/ExCodable/ExCodable.swift‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public extension KeyMap {
5656
self.init(encode: { (root, encoder) in
5757
encoder[codingKeys.first!] = root[keyPath: keyPath]
5858
}, decode: { (root, decoder) in
59-
if let value: Value = decoder[codingKeys] { root[keyPath: keyPath] = value }
59+
if let value: Value = decoder[codingKeys] {
60+
root[keyPath: keyPath] = value
61+
}
6062
}, decodeReference: nil)
6163
}
6264

@@ -65,7 +67,9 @@ public extension KeyMap {
6567
self.init(encode: { (root, encoder) in
6668
encoder[codingKeys.first!] = root[keyPath: keyPath]
6769
}, decode: { (root, decoder) in
68-
if let value: Value = decoder[codingKeys] { root[keyPath: keyPath] = value }
70+
if let value: Value = decoder[codingKeys] {
71+
root[keyPath: keyPath] = value
72+
}
6973
}, decodeReference: nil)
7074
}
7175

@@ -74,7 +78,9 @@ public extension KeyMap {
7478
self.init(encode: { (root, encoder) in
7579
encoder[codingKeys.first!] = root[keyPath: keyPath]
7680
}, decode: nil, decodeReference: { (root, decoder) in
77-
if let value: Value = decoder[codingKeys] { root[keyPath: keyPath] = value }
81+
if let value: Value = decoder[codingKeys] {
82+
root[keyPath: keyPath] = value
83+
}
7884
})
7985
}
8086

@@ -83,7 +89,9 @@ public extension KeyMap {
8389
self.init(encode: { (root, encoder) in
8490
encoder[codingKeys.first!] = root[keyPath: keyPath]
8591
}, decode: nil, decodeReference: { (root, decoder) in
86-
if let value: Value = decoder[codingKeys] { root[keyPath: keyPath] = value }
92+
if let value: Value = decoder[codingKeys] {
93+
root[keyPath: keyPath] = value
94+
}
8795
})
8896
}
8997
}

0 commit comments

Comments
 (0)