Giter Club home page Giter Club logo

bottlerocket's Introduction

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

{
  "title": "Birthday party",
  "likes": 12,
  "is_public": true,
  "users": [
    {
      "name": "Sam",
      "id": 1
    },
    {
      "name": "Zoey",
      "id": 2
    }
  ]
}

http://myservice.com/api/user/1

{
  "name": "Sam",
  "age": 28,
  "id": 1,
  "horoscope": "gemini"
}

You can save both sample responses into two files: events.json and user.json. Then run:

$ bottlerocket gen ~/path/to/models

Which then generates two different models:

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 to convert plural keys into singular classnames
  • SwiftCLI because this is my first CLI app

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.