Skip to content

Commit 1d7c23a

Browse files
committed
added decodeArray using type
1 parent e64eadf commit 1d7c23a

5 files changed

Lines changed: 198 additions & 138 deletions

File tree

.swift-format

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": 1,
3+
"lineLength": 200,
4+
"indentation": {
5+
"tabs": 1
6+
},
7+
"tabWidth": 2,
8+
"maximumBlankLines": 3,
9+
"respectsExistingLineBreaks": true,
10+
"lineBreakBeforeControlFlowKeywords": false,
11+
"lineBreakBeforeEachArgument": false,
12+
"multiElementCollectionTrailingCommas": false,
13+
"indentBlankLines": false
14+
}

.swiftlint.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
type_name:
4+
allowed_symbols: ["_"] # these are allowed in type names
5+
6+
7+
vertical_whitespace:
8+
max_empty_lines: 3
9+
10+
line_length:
11+
warning: 180
12+
error: 360
13+
14+
file_length:
15+
warning: 1000
16+
error: 2000

Main/Source/Decoder.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import Foundation
1111
public protocol Decoder {
1212

1313
typealias DecodeClosure<T> = (Decoder) -> T?
14-
15-
func decode<T:Encodable>(type: T.Type) -> T?
14+
15+
func decode<T: Encodable>(type: T.Type) -> T?
1616
func decode<T>(forKey key: String, closure: DecodeClosure<T>) -> T?
1717
func decoder(forKey key: String) -> Decoder?
1818
func decodeArray<T>(forKey key: String, closure: DecodeClosure<T>) -> [T]?
@@ -27,64 +27,70 @@ public protocol Decoder {
2727
func string(forKey key: String, default: String) -> String
2828
func bool(forKey key: String, default: Bool) -> Bool
2929
func integer(forKey key: String, default: Int) -> Int
30-
30+
3131
func date(forKey key: String) -> Date?
3232

3333
func dictionary(forKey key: String) -> [String: Any]?
3434

3535
var keys: [String] { get }
3636
}
3737

38-
public extension Decoder {
39-
40-
func decode<T>(forKey key: String, closure: (Decoder) -> T?) -> T? {
38+
extension Decoder {
39+
40+
public func decode<T>(forKey key: String, closure: (Decoder) -> T?) -> T? {
4141
if let decoder = self.decoder(forKey: key) {
4242
return closure(decoder)
4343
}
4444
return nil
4545
}
46-
47-
func decoder(forKeyPath path: String...) -> Decoder? {
46+
47+
public func decodeArray<T: Encodable>(forKey key: String, type: T.Type) -> [T]? {
48+
return self.decodeArray(forKey: key) { decoder in
49+
return T(decoder: decoder)
50+
}
51+
}
52+
53+
public func decoder(forKeyPath path: String...) -> Decoder? {
4854
return self.decoder(forKeyPath: path)
4955
}
5056

51-
func decoder(forKeyPath keyPath: [String]) -> Decoder? {
57+
public func decoder(forKeyPath keyPath: [String]) -> Decoder? {
5258
if let key = keyPath.first {
5359
let result = self.decoder(forKey: key)
5460
return result?.decoder(forKeyPath: Array(keyPath.dropFirst()))
5561
}
5662
return self
5763
}
58-
5964

60-
func string(forKey key: String, default defaultValue: String) -> String {
65+
public func string(forKey key: String, default defaultValue: String) -> String {
6166
if let result = self.string(forKey: key) {
6267
return result
6368
}
6469
return defaultValue
6570
}
6671

67-
func bool(forKey key: String, default defaultValue: Bool) -> Bool {
72+
public func bool(forKey key: String, default defaultValue: Bool) -> Bool {
6873
if let result = self.bool(forKey: key) {
6974
return result
7075
}
7176
return defaultValue
7277
}
7378

74-
func integer(forKey key: String, default defaultValue: Int) -> Int {
79+
public func integer(forKey key: String, default defaultValue: Int) -> Int {
7580
if let result = self.integer(forKey: key) {
7681
return result
7782
}
7883
return defaultValue
7984
}
8085

8186
@available(iOS 10, *)
82-
func date(forKey key: String) -> Date? {
87+
public func date(forKey key: String) -> Date? {
8388
if let stringValue = string(forKey: key) {
8489
let formatter = ISO8601DateFormatter()
8590
return formatter.date(from: stringValue)
8691
}
8792
return nil
8893
}
89-
94+
95+
9096
}

Test/Source/Helper/Quadrilateral.swift

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,64 @@
66
//
77

88
import Foundation
9+
910
@testable import OBCoder
1011

1112
public struct Quadrilateral: Encodable, Equatable {
1213

14+
public enum Corner: Int {
15+
case topLeft = 0
16+
case topRight
17+
case bottomLeft
18+
case bottomRight
19+
}
1320

14-
public enum Corner : Int {
15-
case topLeft = 0
16-
case topRight
17-
case bottomLeft
18-
case bottomRight
19-
}
20-
21-
public enum Edge : Int {
22-
case top = 0
23-
case bottom
24-
case left
25-
case right
26-
}
27-
28-
public static let zero = Quadrilateral(topLeft: .zero, topRight: .zero, bottomLeft: .zero, bottomRight: .zero)
29-
30-
31-
public var topLeft: CGPoint
32-
public var topRight: CGPoint
33-
public var bottomLeft: CGPoint
34-
public var bottomRight: CGPoint
21+
public enum Edge: Int {
22+
case top = 0
23+
case bottom
24+
case left
25+
case right
26+
}
3527

28+
public static let zero = Quadrilateral(
29+
topLeft: .zero, topRight: .zero, bottomLeft: .zero, bottomRight: .zero)
3630

31+
public var topLeft: CGPoint
32+
public var topRight: CGPoint
33+
public var bottomLeft: CGPoint
34+
public var bottomRight: CGPoint
3735

38-
public init(topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) {
39-
self.topLeft = topLeft
40-
self.topRight = topRight
41-
self.bottomLeft = bottomLeft
42-
self.bottomRight = bottomRight
43-
}
36+
public init(topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) {
37+
self.topLeft = topLeft
38+
self.topRight = topRight
39+
self.bottomLeft = bottomLeft
40+
self.bottomRight = bottomRight
41+
}
4442

45-
public init?(decoder: OBCoder.Decoder) {
46-
guard let topLeft = decoder.point(forKey: "topLeft") else {
47-
return nil
48-
}
49-
guard let topRight = decoder.point(forKey: "topRight") else {
50-
return nil
51-
}
52-
guard let bottomLeft = decoder.point(forKey: "bottomLeft") else {
53-
return nil
54-
}
55-
guard let bottomRight = decoder.point(forKey: "bottomRight") else {
56-
return nil
57-
}
58-
self.topLeft = topLeft
59-
self.topRight = topRight
60-
self.bottomLeft = bottomLeft
61-
self.bottomRight = bottomRight
62-
}
43+
public init?(decoder: OBCoder.Decoder) {
44+
guard let topLeft = decoder.point(forKey: "topLeft") else {
45+
return nil
46+
}
47+
guard let topRight = decoder.point(forKey: "topRight") else {
48+
return nil
49+
}
50+
guard let bottomLeft = decoder.point(forKey: "bottomLeft") else {
51+
return nil
52+
}
53+
guard let bottomRight = decoder.point(forKey: "bottomRight") else {
54+
return nil
55+
}
56+
self.topLeft = topLeft
57+
self.topRight = topRight
58+
self.bottomLeft = bottomLeft
59+
self.bottomRight = bottomRight
60+
}
6361

64-
public func encode(with coder: Coder) {
65-
coder.encode(topLeft, forKey: "topLeft")
66-
coder.encode(topRight, forKey: "topRight")
67-
coder.encode(bottomLeft, forKey: "bottomLeft")
68-
coder.encode(bottomRight, forKey: "bottomRight")
69-
}
62+
public func encode(with coder: Coder) {
63+
coder.encode(topLeft, forKey: "topLeft")
64+
coder.encode(topRight, forKey: "topRight")
65+
coder.encode(bottomLeft, forKey: "bottomLeft")
66+
coder.encode(bottomRight, forKey: "bottomRight")
67+
}
7068

7169
}

0 commit comments

Comments
 (0)