novi / mysql-swift

A type safe MySQL client for Swift
MIT License
163 stars 40 forks source link

Make RawRepresentable types automatically conform to QueryParameter #75

Closed florianreinhart closed 6 years ago

florianreinhart commented 6 years ago

It would be nice to use enums, OptionSets and other RawRepresentable types directly as query parameters.

This could make use of conditional conformance introduced in Swift 4.1. The following code will not compile and complain that an extension for a protocol cannot have an inheritance clause: Extension of protocol 'RawRepresentable' cannot have an inheritance clause. However, it should explain my idea. I guess mysql-swift needs some special internal handling of RawRepresentables to make this work.

extension RawRepresentable: QueryParameter where RawValue: QueryParameterType {
    public func queryParameter(option: QueryParameterOption) -> QueryParameterType {
        return self.rawValue
    }
}
novi commented 6 years ago

I have implemented experimental codes into mysql-swift. (88d2f5c5a8e9604fdc4d5998610e0f94f03d74c6) But it will case error multiple matching functions named... when structs have both Codable and QueryParameter protocols. And it will be avoided by this fix 9514552af251f3265f82f1fd3fbc6fb40744a1a8.

florianreinhart commented 6 years ago

Thanks @novi! LGTM, though I haven't tested the new code 😉

novi commented 6 years ago

Released 0.9.0 and will close the ticket.