Skip to content

Commit

Permalink
Update README, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kientux committed Jun 30, 2022
1 parent 63d3000 commit 9f56bb7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ components.path = "some/api/path/"
components.queryItems = params.map { URLQueryItem(name: $0.key, value: "\($0.value)")}
```

- Auto infer name of param from simple field name
- 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 expand nested params
Expand Down Expand Up @@ -83,3 +83,37 @@ print(serialized)

// ["query": "search", "name": "tux"]
```

### Naming strategy

```swift
struct ApiParam: ParamsContainer {

@Params
var thisShouldBeSnakeCase: Int = 0
}

let params = ApiParam()
let config = ParamSerializer.Config(namingStrategy: SerializerNamingStrategy.convertToSnakeCase)
let serialized = ParamSerializer(config: config).serialize(object: params)
print(serialized)

// ["this_should_be_snake_case": 0]
```

- Any custom naming strategy can be done by conforming to `NamingStrategy`

```swift
class UppercaseNamingStrategy: NamingStrategy {
public func name(from fieldName: String) -> String {
return fieldName.uppercased()
}
}

let params = ApiParam()
let config = ParamSerializer.Config(namingStrategy: UppercaseNamingStrategy())
let serialized = ParamSerializer(config: config).serialize(object: params)
print(serialized)

// ["THISSHOULDBESNAKECASE": 0]
```
10 changes: 10 additions & 0 deletions Tests/ParameterizeTests/ParameterizeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ final class ParameterizeTests: XCTestCase {
XCTAssert(serialized.keys.contains("namingConvert"))
XCTAssert(serialized.keys.contains("namingConvert1"))
XCTAssert(serialized.keys.contains("NamingConvert2"))

serialized = ParamSerializer(config: .init(namingStrategy: UppercaseNamingStrategy()))
.serialize(object: params)
XCTAssert(serialized.keys.contains("NAMINGCONVERT"))
}
}

Expand Down Expand Up @@ -140,3 +144,9 @@ extension Date: ParamConvertible {
ISO8601DateFormatter().string(from: self)
}
}

class UppercaseNamingStrategy: NamingStrategy {
public func name(from fieldName: String) -> String {
return fieldName.uppercased()
}
}

0 comments on commit 9f56bb7

Please sign in to comment.