Skip to content

Commit

Permalink
Partially handle enum, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kientux committed Jul 10, 2022
1 parent c4a646d commit c9eb1be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ components.queryItems = params.map { URLQueryItem(name: $0.key, value: "\($0.val

- Auto infer name of param from field name, with default/snake_case or custom naming strategy
- Nil and empty array will be remove automatically
- Auto handle array of elements that conform to `CustomStringConvertible`
- Auto handle array (of any elements or elements that conform to `ParamConvertible`)
- Auto expand nested params
- Support any custom type by conforms to `ParamConvertible`, or simply use an in-place custom mapper

Expand Down Expand Up @@ -86,6 +86,8 @@ print(serialized)

> Note: Conversion will go through custom mapper first, if the returned value also conforms to `ParamConvertible` then it will be converted again using the `ParamConvertible` implementation.
> Note: For enum with raw value, currently there's no easy way to automatically serialize its raw value. So you will still have to conform to `ParamConvertible`, but you don't have to write the `parameterValue` implementation.
### Nested params will be expanded if also conforms to `ParamsContainer`

```swift
Expand Down
16 changes: 14 additions & 2 deletions Sources/Parameterize/ParamConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ public protocol ParamConvertible {
var parameterValue: Any? { get }
}

extension Array: ParamConvertible where Element: CustomStringConvertible {
extension Array: ParamConvertible {
public var parameterValue: Any? {
if isEmpty {
return nil
}

return map({ $0.description }).joined(separator: ",")
return map({
if let p = $0 as? ParamConvertible, let v = p.parameterValue {
return String(describing: v)
} else {
return String(describing: $0)
}
}).joined(separator: ",")
}
}

extension ParamConvertible where Self: RawRepresentable {
var parameterValue: Any? {
String(describing: rawValue)
}
}
22 changes: 19 additions & 3 deletions Tests/ParameterizeTests/ParameterizeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ final class ParameterizeTests: XCTestCase {
ids: [1, 2, 3],
page: 2,
pageSize: 250,
productsTypes: ["normal"])
status: [.active, .inactive],
productsTypes: [.normal])
let serialized = ParamSerializer().serialize(object: params)

/// make sure output type is `String`, not `Optional<String>`
Expand All @@ -19,6 +20,8 @@ final class ParameterizeTests: XCTestCase {
XCTAssertEqual(serialized["ids"] as? String, "1,2,3")
XCTAssertEqual(serialized["page"] as? Int, 2)
XCTAssertEqual(serialized["limit"] as? Int, 250)
XCTAssertEqual(serialized["status"] as? String, "0,-1")
XCTAssertEqual(serialized["product_type"] as? String, "normal")
XCTAssertEqual(serialized["product_types"] as? String, "normal")
}

Expand Down Expand Up @@ -114,10 +117,13 @@ struct ProductParams: ParamsContainer {
var createdOnMax: Date? = nil

@Params
var status: [String] = ["active"]
var status: [Status] = [.active]

@Params("product_type")
var productsType: ProductType = .normal

@Params("product_types")
var productsTypes: [String] = []
var productsTypes: [ProductType] = []

@Params("custom_type")
var myUrl: MyCustomType? = nil
Expand Down Expand Up @@ -165,6 +171,16 @@ struct ProductParams: ParamsContainer {
}
}

enum ProductType: String, ParamConvertible {
case normal
case special
}

enum Status: Int, ParamConvertible {
case active = 0
case inactive = -1
}

struct MyCustomType: ParamConvertible {
var url: URL

Expand Down

0 comments on commit c9eb1be

Please sign in to comment.