Giter Club home page Giter Club logo

model-generator's Introduction

Model-Generator

Generate code for basic models from JSON schema for Java, Groovy, Kotlin, and TypeScript.

This is kind of a naïve implementation and isn't really too great in my opinion. I realized its demise after messing with a non-jvm language. I'm struggling to find a way to strip out common functionality with this current implementation.

In the future, it would be pretty sweet to try to marry Antlr grammars and JSON Schema to generate classes more robustly with less generator code.

Implemented Languages

  • Java
  • Groovy
  • Kotlin -> 100% positive this isn't correct, I have no practical kotlin experience yet... will revisit
  • TypeScript

Future Implementations ... When I feel like it

  • T-SQL creation scripts
  • Python
  • Ruby
  • C#
  • ColdFusion (purely just masochism at this point)
  • Scala
  • Go

Possible Improvements

  • DateTime - I thought I had implemented this...might be going crazy
  • Parameterized constructor using 'required' on object properties
  • On a similar note, call parameterized super constructor
  • Prefix my additional JSON schema props [extends, primitive, parseTo] with a '$'
  • JPA Annotations
  • Constants / final properties ?
  • Serialization
  • Builder methods -> myModel.withProp1("wasd")
  • hashCode, toString, and equals methods

Specification

A couple of new properties were added to add more functionality.

  • extends - extend from other object in another schema
  • primitive - array: T[] or List ; numbers: Float or float; Integer or int
  • parseTo - attempt a parse to non-standard JSON schema types (BigDecimal, Long, Byte, etc)
    • If parsing fails on a field, it falls back to whatever is specified in type
{
  "id": "http://json-schema.org/thing",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Book",
  "description": "A book",
  "type": "object",
  "extends": {
    "$ref": "Common/Thing.json" // relative to schema directory
  },
  "properties": {
    "genres": {
      "type": "array",
      "items": {
        "type": "string", // String.class
        "maxItems": 10,   // String[10] init in constructor
        "primitive": true // true= T[], false/null= List<T>
      }
    },
    "pageLength": {
      "type": "integer",  // Integer.class
      "parseTo": "Long",  // attempt parse to Long.class
      "default": 25       // default value to 25 in constructor
    },
    "isbn": {
      "type": "string"
    },
    "author": {
      "$ref": "Common/Person.json" // Person.class relative to schema directory
    },
    "price": {
      "type": "number",    // Float.class
      "parseTo": "decimal" // attempt parse to BigDecimal for currency handling, falls back to Float if failed
    }
  }
}

Commands

  • Generate python generate.py
  • Generate with gradle gradle generate
  • Run tests python -m unittest
  • Test build Java Jar ./tests-build/test-java.sh
  • Test build Groovy Jar ./tests-build/test-groovy.sh
  • Test build Kotlin Jar ./tests-build/test-kotlin.sh

References

model-generator's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.