Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rnystrom/BottleRocket
An experimental CLI tool for generating normalized Swift models from example JSON responses.
https://github.com/rnystrom/BottleRocket
code-generation json-response swift swift-models
Last synced: 8 days ago
JSON representation
An experimental CLI tool for generating normalized Swift models from example JSON responses.
- Host: GitHub
- URL: https://github.com/rnystrom/BottleRocket
- Owner: rnystrom
- Created: 2017-05-04T20:36:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-05-12T21:27:55.000Z (over 7 years ago)
- Last Synced: 2024-10-29T21:14:12.912Z (13 days ago)
- Topics: code-generation, json-response, swift, swift-models
- Language: Swift
- Homepage:
- Size: 2.69 MB
- Stars: 12
- Watchers: 2
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# BottleRocket
An experimental CLI tool for generating normalized Swift models from example JSON responses.
## Goals
- Generate Swift models that don't need any cleanup by hand
- Allow parsing partial JSON responses into the same model
- Properties that don't show up in every model are optional
- Generated Swift JSON parsing code without dependencies
- Experiment with functional programming techniques in Swift## Installation
- Download project
- Open `BottleRocket.xcodeproj`
- Build
- Should be installed in `/usr/local/bin`## Example
Suppose you have two different endpoints that return JSON:
`http://myservice.com/api/events`
```json
{
"title": "Birthday party",
"likes": 12,
"is_public": true,
"users": [
{
"name": "Sam",
"id": 1
},
{
"name": "Zoey",
"id": 2
}
]
}
````http://myservice.com/api/user/1`
```json
{
"name": "Sam",
"age": 28,
"id": 1,
"horoscope": "gemini"
}
```You can save both sample responses into two files: `events.json` and `user.json`. Then run:
```shell
$ bottlerocket gen ~/path/to/models
```Which then generates two different models:
```swift
final class Event: NSCoding {
let title: String
let likes: NSNumber
let is_public: Bool
let users: [User]init(title: String, likes: NSNumber, is_public: Bool, users: [User]) {
// ...
}convenience init?(json: [String: Any]) {
// unpack and parse models
// call designated init
}// encode/decode
}final class User: NSCoding {
let name: String
let id: NSNumber// only shows up in api/user/1 response
let age: NSNumber?
let horoscope: String?// init, json, coding, etc
}
```## Dependencies
- [InflectorKit](https://github.com/mattt/InflectorKit) to convert plural keys into singular classnames
- [SwiftCLI](https://github.com/jakeheis/SwiftCLI) because this is my first CLI app