-
Notifications
You must be signed in to change notification settings - Fork 50
Replace dynamic type checks with generic overloads #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
simonjbeaumont
wants to merge
1
commit into
apple:main
from
simonjbeaumont:sb-remove-dynamic-type-checks
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -90,19 +90,27 @@ extension Converter { | |||||
from data: Data, | ||||||
transforming transform: (T) -> C | ||||||
) throws -> C { | ||||||
let decoded: T | ||||||
if let myType = T.self as? _StringParameterConvertible.Type { | ||||||
guard | ||||||
let stringValue = String(data: data, encoding: .utf8), | ||||||
let decodedValue = myType.init(stringValue) | ||||||
else { | ||||||
throw RuntimeError.failedToDecodePrimitiveBodyFromData | ||||||
} | ||||||
decoded = decodedValue as! T | ||||||
} else { | ||||||
decoded = try decoder.decode(type, from: data) | ||||||
return transform(try decoder.decode(type, from: data)) | ||||||
} | ||||||
|
||||||
/// Gets a deserialized value from body data. | ||||||
/// - Parameters: | ||||||
/// - type: Type used to decode the data. | ||||||
/// - data: Encoded body data. | ||||||
/// - transform: Closure for transforming the Decodable type into a final type. | ||||||
/// - Returns: Deserialized body value. | ||||||
public func bodyGet<T: Decodable & _StringParameterConvertible, C: _StringParameterConvertible>( | ||||||
_ type: T.Type, | ||||||
from data: Data, | ||||||
transforming transform: (T) -> C | ||||||
) throws -> C { | ||||||
guard | ||||||
let stringValue = String(data: data, encoding: .utf8), | ||||||
let decodedValue = T(stringValue) | ||||||
else { | ||||||
throw RuntimeError.failedToDecodePrimitiveBodyFromData | ||||||
} | ||||||
return transform(decoded) | ||||||
return transform(decodedValue) | ||||||
} | ||||||
|
||||||
/// Provides an optional serialized value for the body value. | ||||||
|
@@ -126,6 +134,27 @@ extension Converter { | |||||
) | ||||||
} | ||||||
|
||||||
/// Provides an optional serialized value for the body value. | ||||||
/// - Parameters: | ||||||
/// - value: Encodable value to turn into data. | ||||||
/// - headerFields: Headers container where to add the Content-Type header. | ||||||
/// - transform: Closure for transforming the Encodable value into body content. | ||||||
/// - Returns: Data for the serialized body value, or nil if `value` was nil. | ||||||
public func bodyAddOptional<T: Encodable & _StringParameterConvertible, C: _StringParameterConvertible>( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
_ value: C?, | ||||||
headerFields: inout [HeaderField], | ||||||
transforming transform: (C) -> EncodableBodyContent<T> | ||||||
) throws -> Data? { | ||||||
guard let value else { | ||||||
return nil | ||||||
} | ||||||
return try bodyAddRequired( | ||||||
value, | ||||||
headerFields: &headerFields, | ||||||
transforming: transform | ||||||
) | ||||||
} | ||||||
|
||||||
/// Provides a required serialized value for the body value. | ||||||
/// - Parameters: | ||||||
/// - value: Encodable value to turn into data. | ||||||
|
@@ -139,15 +168,28 @@ extension Converter { | |||||
) throws -> Data { | ||||||
let body = transform(value) | ||||||
headerFields.add(name: "content-type", value: body.contentType) | ||||||
if let value = value as? _StringParameterConvertible { | ||||||
guard let data = value.description.data(using: .utf8) else { | ||||||
throw RuntimeError.failedToEncodePrimitiveBodyIntoData | ||||||
} | ||||||
return data | ||||||
} | ||||||
return try encoder.encode(body.value) | ||||||
} | ||||||
|
||||||
/// Provides a required serialized value for the body value. | ||||||
/// - Parameters: | ||||||
/// - value: Encodable value to turn into data. | ||||||
/// - headerFields: Headers container where to add the Content-Type header. | ||||||
/// - transform: Closure for transforming the Encodable value into body content. | ||||||
/// - Returns: Data for the serialized body value. | ||||||
public func bodyAddRequired<T: Encodable & _StringParameterConvertible, C: _StringParameterConvertible>( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
_ value: C, | ||||||
headerFields: inout [HeaderField], | ||||||
transforming transform: (C) -> EncodableBodyContent<T> | ||||||
) throws -> Data where C: _StringParameterConvertible { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let body = transform(value) | ||||||
headerFields.add(name: "content-type", value: body.contentType) | ||||||
guard let data = body.value.description.data(using: .utf8) else { | ||||||
throw RuntimeError.failedToEncodePrimitiveBodyIntoData | ||||||
} | ||||||
return data | ||||||
} | ||||||
|
||||||
// MARK: Body - Primivite - Data | ||||||
|
||||||
/// Gets a deserialized value from body data. | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.