Skip to content

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
wants to merge 1 commit into from
Closed

Replace dynamic type checks with generic overloads #10

wants to merge 1 commit into from

Conversation

simonjbeaumont
Copy link
Collaborator

@simonjbeaumont simonjbeaumont commented May 26, 2023

Motivation

As described in apple/swift-openapi-generator#41, there are a number of places where we using as? and as! type casting, which could be replaced with generic overloads.

Modifications

  • Replace converter helper functions with generic overloads.

Result

Test Plan

The tests continue to pass.

Notes

Whilst implementing this change, I think that we may want to revisit the encoding logic, since I think the knowledge of whether to encode a value as binary data, JSON, or plain text, is at the place where the content-type is set, but we'll defer that to a subsequent PR.

@czechboy0
Copy link
Contributor

Filed apple/swift-openapi-generator#43 for the issue you mentioned in the note at the end.

Copy link
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'm glad you found a way to get rid of the dynamic casts.

Proposed 4 changes, just to remove the C constraint on _StringParameterConvertible, which shouldn't be there, and in fact lead to one instance of the wrong overload being chosen.

Verified that with my proposed changes here, the PR passes both runtime and generator tests (with generator using the branch from apple/swift-openapi-generator#44).

/// - 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>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public func bodyGet<T: Decodable & _StringParameterConvertible, C: _StringParameterConvertible>(
public func bodyGet<T: Decodable & _StringParameterConvertible, C>(

/// - 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>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public func bodyAddOptional<T: Encodable & _StringParameterConvertible, C: _StringParameterConvertible>(
public func bodyAddOptional<T: Encodable & _StringParameterConvertible, C>(

/// - 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>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public func bodyAddRequired<T: Encodable & _StringParameterConvertible, C: _StringParameterConvertible>(
public func bodyAddRequired<T: Encodable & _StringParameterConvertible, C>(

_ value: C,
headerFields: inout [HeaderField],
transforming transform: (C) -> EncodableBodyContent<T>
) throws -> Data where C: _StringParameterConvertible {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) throws -> Data where C: _StringParameterConvertible {
) throws -> Data {

@simonjbeaumont simonjbeaumont added the 🔨 semver/patch No public API change. label May 29, 2023
@simonjbeaumont
Copy link
Collaborator Author

I think that we'll close this for now, because it's not likely to be relevant if we tackle apple/swift-openapi-generator#43.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace dynamic type checks with generic overloads
2 participants