Skip to content

Commit

Permalink
[Swift5][client] try to fix JsonEncondable
Browse files Browse the repository at this point in the history
  • Loading branch information
4brunu committed Feb 7, 2022
1 parent 5180047 commit 4647a79
Show file tree
Hide file tree
Showing 33 changed files with 213 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}{{/useVapor}}{{#generateModelAdditionalProperties}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,30 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
encoding = nil
let upload = manager.upload(multipartFormData: { mpForm in
for (k, v) in self.parameters! {
switch v {
for (key, value) in self.parameters! {
switch value {
case let fileURL as URL:
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
mpForm.append(fileURL, withName: key, fileName: fileURL.lastPathComponent, mimeType: mimeType)
} else {
mpForm.append(fileURL, withName: k)
mpForm.append(fileURL, withName: key)
}
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
if let data = string.data(using: String.Encoding.utf8) {
mpForm.append(data, withName: key)
}
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
if let data = number.stringValue.data(using: String.Encoding.utf8) {
mpForm.append(data, withName: key)
}
case let data as Data:
mpForm.append(data, withName: k)
mpForm.append(data, withName: key)
case let jsonEncodable as JSONEncodable:
if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
mpForm.append(data, withName: key)
}
default:
fatalError("Unprocessable value \(v) with key \(k)")
fatalError("Unprocessable value \(value) with key \(key)")
}
}
}, to: URLString, method: xMethod, headers: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {

This comment has been minimized.

Copy link
@4brunu

4brunu Feb 7, 2022

Author Owner

protocol 'JSONEncodable' as a type cannot conform to 'Encodable'

urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,30 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
encoding = nil

let upload = manager.upload(multipartFormData: { mpForm in
for (k, v) in self.parameters! {
switch v {
for (key, value) in self.parameters! {
switch value {
case let fileURL as URL:
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
mpForm.append(fileURL, withName: key, fileName: fileURL.lastPathComponent, mimeType: mimeType)
} else {
mpForm.append(fileURL, withName: k)
mpForm.append(fileURL, withName: key)
}
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
if let data = string.data(using: String.Encoding.utf8) {
mpForm.append(data, withName: key)
}
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
if let data = number.stringValue.data(using: String.Encoding.utf8) {
mpForm.append(data, withName: key)
}
case let data as Data:
mpForm.append(data, withName: k)
mpForm.append(data, withName: key)
case let jsonEncodable as JSONEncodable:
if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
mpForm.append(data, withName: key)
}
default:
fatalError("Unprocessable value \(v) with key \(k)")
fatalError("Unprocessable value \(value) with key \(key)")
}
}
}, to: URLString, method: xMethod, headers: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ private class FormDataEncoding: ParameterEncoding {
data: data
)

case let jsonEncodable as JSONEncodable:

if let data = try? CodableHelper.jsonEncoder.encode(jsonEncodable) {
urlRequest = configureDataUploadRequest(
urlRequest: urlRequest,
boundary: boundary,
name: key,
data: data
)
}

default:
fatalError("Unprocessable value \(value) with key \(key)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ extension Date: JSONEncodable {

extension JSONEncodable where Self: Encodable {
func encodeToJSON() -> Any {
let encoder = CodableHelper.jsonEncoder
guard let data = try? encoder.encode(self) else {
fatalError("Could not encode to json: \(self)")
}
return data.encodeToJSON()
return self
}
}

Expand Down
Loading

0 comments on commit 4647a79

Please sign in to comment.