Skip to content

Commit d966bef

Browse files
authored
Modified a Param component of the BodyBuilder to a Field component (#22)
1 parent 522ea70 commit d966bef

File tree

7 files changed

+37
-46
lines changed

7 files changed

+37
-46
lines changed
Binary file not shown.

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ Request {
7676
Request {
7777
...
7878
Body {
79-
Param("VALUE", forKey: "KEY")
80-
Param("VALUE1", forKey: "KEY1")
81-
Param("VALUE2", forKey: "KEY2")
79+
Field("VALUE", forKey: "KEY")
80+
Field("VALUE1", forKey: "KEY1")
81+
Field("VALUE2", forKey: "KEY2")
8282
...
8383
}
8484
...
@@ -240,13 +240,13 @@ enum APIs: RouterProtocol {
240240
Path("orgs/\(organizationName)/repos")
241241
}
242242
Body {
243-
Param(repositoryInfo.name, forKey: "name")
244-
Param(repositoryInfo.description, forKey: "description")
245-
Param(repositoryInfo.homePage, forKey: "homepage")
246-
Param(repositoryInfo.private, forKey: "private")
247-
Param(repositoryInfo.hasIssues, forKey: "has_issues")
248-
Param(repositoryInfo.hasProjects, forKey: "has_projects")
249-
Param(repositoryInfo.hasWiki, forKey: "has_wiki")
243+
Field(repositoryInfo.name, forKey: "name")
244+
Field(repositoryInfo.description, forKey: "description")
245+
Field(repositoryInfo.homePage, forKey: "homepage")
246+
Field(repositoryInfo.private, forKey: "private")
247+
Field(repositoryInfo.hasIssues, forKey: "has_issues")
248+
Field(repositoryInfo.hasProjects, forKey: "has_projects")
249+
Field(repositoryInfo.hasWiki, forKey: "has_wiki")
250250
}
251251
}
252252
case let .searchRepositories(query):

Sources/APIRouter/BodyBuilder.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,3 @@ public extension Body {
4545
self = body
4646
}
4747
}
48-
49-
public struct Param: BodyProtocol {
50-
private let value: Any
51-
private let key: String
52-
53-
public init(_ value: Any, forKey key: String) {
54-
self.value = value
55-
self.key = key
56-
}
57-
58-
public func build(_ body: inout Body) {
59-
var dictionary: Dictionary<String, Any> = [:]
60-
for item in body.body {
61-
dictionary.updateValue(item.value, forKey: item.key)
62-
}
63-
dictionary.updateValue(value, forKey: key)
64-
body = Body(dictionary)
65-
}
66-
}
67-

Sources/APIRouter/HeaderBuilder.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,32 @@ public extension Header {
4646
}
4747
}
4848

49-
public struct Field: HeaderProtocol {
50-
private let value: String
49+
public struct Field: HeaderProtocol, BodyProtocol {
50+
private let value: Any
5151
private let key: String
5252

53-
public init(_ value: String, forKey key: String) {
53+
public init(_ value: Any, forKey key: String) {
5454
self.value = value
5555
self.key = key
5656
}
5757

5858
public func build(_ header: inout Header) {
59-
var headers: [String: String] = [:]
59+
var headers: Dictionary<String, String> = [:]
6060
for item in header.headers {
61-
headers.updateValue(item.value, forKey: item.key)
61+
headers.updateValue(String(item.value), forKey: item.key)
62+
}
63+
if let value = value as? String {
64+
headers.updateValue(String(value), forKey: key)
6265
}
63-
headers.updateValue(value, forKey: key)
6466
header = Header(headers)
6567
}
68+
69+
public func build(_ body: inout Body) {
70+
var dictionary: Dictionary<String, Any> = [:]
71+
for item in body.body {
72+
dictionary.updateValue(item.value, forKey: item.key)
73+
}
74+
dictionary.updateValue(value, forKey: key)
75+
body = Body(dictionary)
76+
}
6677
}

Tests/APIRouterTests/BodyBuilderTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ final class BodyBuilderTests: XCTestCase {
1313
Body {
1414
switch self {
1515
case .one:
16-
Param("VALUE", forKey: "OPTIONONE")
16+
Field("VALUE", forKey: "OPTIONONE")
1717
case .two:
18-
Param("VALUE", forKey: "OPTIONTWO")
18+
Field("VALUE", forKey: "OPTIONTWO")
1919
}
2020
}
2121
}
@@ -40,9 +40,9 @@ final class BodyBuilderTests: XCTestCase {
4040
Request {
4141
Body {
4242
if self == .one {
43-
Param("VALUE", forKey: "OPTIONONE")
43+
Field("VALUE", forKey: "OPTIONONE")
4444
} else {
45-
Param("VALUE", forKey: "OPTIONTWO")
45+
Field("VALUE", forKey: "OPTIONTWO")
4646
}
4747
}
4848
}
@@ -69,7 +69,7 @@ final class BodyBuilderTests: XCTestCase {
6969
let request = Request {
7070
Body {
7171
for param in params {
72-
Param(param.value, forKey: param.key)
72+
Field(param.value, forKey: param.key)
7373
}
7474
}
7575
}

Tests/APIRouterTests/RequestBuilderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class RequestBuilderTests: XCTestCase {
66
func testGeneratedHttpRequestBodyWithBodyBuilder() {
77
let request = Request {
88
Body {
9-
Param("VALUE", forKey: "KEY")
9+
Field("VALUE", forKey: "KEY")
1010
}
1111
}
1212

Tests/APIRouterTests/RouterBuilderTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ final class RouterBuilderTests: XCTestCase {
77
let router = Router {
88
Request {
99
Body {
10-
Param("VALUE", forKey: "KEY")
10+
Field("VALUE", forKey: "KEY")
1111
}
1212
Method(.get)
1313
Header {
@@ -40,7 +40,7 @@ final class RouterBuilderTests: XCTestCase {
4040
BaseURL("https://www.baseurl.com")
4141
Request {
4242
Body {
43-
Param("VALUE", forKey: "KEY")
43+
Field("VALUE", forKey: "KEY")
4444
}
4545
Method(.get)
4646
Header {
@@ -77,7 +77,7 @@ final class RouterBuilderTests: XCTestCase {
7777
case .one:
7878
Request {
7979
Body {
80-
Param("VALUE", forKey: "KEY")
80+
Field("VALUE", forKey: "KEY")
8181
}
8282
Method(.get)
8383
Header {
@@ -91,7 +91,7 @@ final class RouterBuilderTests: XCTestCase {
9191
case .two:
9292
Request {
9393
Body {
94-
Param("VALUE", forKey: "KEY")
94+
Field("VALUE", forKey: "KEY")
9595
}
9696
Method(.get)
9797
Header {

0 commit comments

Comments
 (0)