ModelGen is a command-line tool for generating models from JSON Schemas.
Models are usually boilerplate code, why not generate them and forget? It will save you time writing boilerplate and eliminate model errors as your application scales in complexity.
This means that adding a property to a data object is truly a one-line change — no copy-paste required. If you want to refactor all your models it is simple as changing the template and regenerate them.
We support the following languages:
- Swift
- Kotlin
- Java
But you can add support to any other language with few lines of code.
Unlike most of the model generators, it works with two files, the .json
and .stencil
so you have full control on how you want your models to look like.
The Models are defined in JSON, based on JSON Schema but not limited to, basically anything you add on schema you can use the template. It is an extensible and language-independent specification.
Take a look at Example folder.
- Xcode 10.2+ and Swift 4.2+
Run the following command to install using homebrew:
$ brew tap hebertialmeida/ModelGen https://github.com/hebertialmeida/ModelGen.git
$ brew install ModelGen
Run the following commands to build and install manually:
$ git clone https://github.com/hebertialmeida/ModelGen.git
$ cd ModelGen
$ make install
ModelGen takes a schema file as an input.
{
"title": "Company",
"type": "object",
"description": "Definition of a Company",
"identifier": "id",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"logo": {"type": "string", "format": "uri"},
"subdomain": {"type": "string"}
},
"required": ["id", "name", "subdomain"]
}
ModelGen takes a template to generate in the format you want.
//
// {{ spec.title }}.swift
// ModelGen
//
// Generated by [ModelGen]: https://github.com/hebertialmeida/ModelGen
// Copyright © {% now "yyyy" %} ModelGen. All rights reserved.
//
{% if spec.description %}
/// {{ spec.description }}
{% endif %}
public struct {{ spec.title }} {
{% for property in spec.properties %}
{% if property.doc %}
/**
{{ property.doc }}
*/
{% endif %}
public let {{ property.name }}: {{ property.type }}{% if not property.required %}?{% endif %}
{% endfor %}
// MARK: - Initializers
{% map spec.properties into params using property %}{{ property.name }}: {{ property.type }}{% if not property.required %}?{% endif %}{% endmap %}
public init({{ params|join:", " }}) {
{% for property in spec.properties %}
self.{{ property.name }} = {{ property.name }}
{% endfor %}
}
}
// MARK: - Equatable
extension {{ spec.title }}: Equatable {
static public func == (lhs: {{spec.title}}, rhs: {{spec.title}}) -> Bool {
{% for property in spec.properties %}
guard lhs.{{property.name}} == rhs.{{property.name}} else { return false }
{% endfor %}
return true
}
}
To make it easy you can create a .modelgen.yml
spec: ../Specs/
output: ./Model/
template: template.stencil
language: swift
And then:
$ modelgen
Generate from a directory:
$ modelgen --spec ./Specs --template template.stencil --output ./Model
Generate a single file:
$ modelgen --spec company.json --template template.stencil --output Company.swift
//
// Company.swift
// ModelGen
//
// Generated by [ModelGen]: https://github.com/hebertialmeida/ModelGen
// Copyright © 2019 ModelGen. All rights reserved.
//
/// Definition of a Company
public struct Company {
public let id: Int
public let logo: URL?
public let name: String
public let subdomain: String
// MARK: - Initializers
public init(id: Int, logo: URL?, name: String, subdomain: String) {
self.id = id
self.logo = logo
self.name = name
self.subdomain = subdomain
}
}
// MARK: - Equatable
extension Company: Equatable {
static public func == (lhs: Company, rhs: Company) -> Bool {
guard lhs.id == rhs.id else { return false }
guard lhs.logo == rhs.logo else { return false }
guard lhs.name == rhs.name else { return false }
guard lhs.subdomain == rhs.subdomain else { return false }
return true
}
}
This tool is powered by:
- Stencil and few other libs by Kyle Fuller
- StencilSwiftKit by SwiftGen
The inital concept was based on Peter Livesey's pull request and inspired by plank from Pinterest.
If you want to contribute, don't hesitate to open an pull request.
ModelGen is available under the MIT license. See the LICENSE file.