ddossot / jerg

JSON Schema to Erlang Records Generator
Other
47 stars 13 forks source link
___     _        __     _____       ___
  (_   | \    ___) |    \    )  ____)  
    |  |  |  (__   |     )  /  /  __   
 _  |  |  |   __)  |    /  (  (  (  \  
( |_|  |  |  (___  | |\ \   \  \__)  ) 
_\    /__/       )_| |_\ \___)      (__

jerg - JSON Schema to Erlang Records Generator

WAT?

jerg generates this:

% Monetary price
% Intended to be used embedded in other schemas
-record(price,
        {
            currency_code :: binary(),
            amount        :: number()
        }).

% Product (Short View)
% Intended to be used in search results
-record(product_short,
        {
            % Numeric object identity, set to -1 at creation time
            id             = -1 :: integer(),
            name                :: binary(),
            list_price          :: #price{},
            discount_price      :: #price{}
        }).

% Product Category
-record(product_category,
        {
            % Numeric object identity, set to -1 at creation time
            id   = -1 :: integer(),
            name      :: binary()
        }).

% Product (Full View)
% Intended to be used when full description is needed
-record(product_full,
        {
            % Numeric object identity, set to -1 at creation time
            id             = -1 :: integer(),
            name                :: binary(),
            list_price          :: #price{},
            discount_price      :: #price{},
            description         :: binary(),
            categories          :: list(#product_category{})
        }).

out of that:

category.json
{
    "title": "Product Category",
    "recordName": "product_category",
    "type": "object",
    "extends" : {
        "$ref": "identified_object.json"
    },
    "properties": {
        "name": {
            "type": "string"
        }
    }
}
identified_object.json
{
    "title": "Identity field for all objects",
    "description" : "Intended to be extended by identified objects",
    "type": "object",
    "abstract" : "true",
    "properties": {
        "id": {
            "description" : "Numeric object identity, set to -1 at creation time",
            "type": "integer",
            "default" : -1
        }
    }
}
price.json
{
    "title": "Monetary price",
    "description" : "Intended to be used embedded in other schemas",
    "type": "object",
    "properties": {
        "currency_code": {
            "type": "string"
        },
        "amount": {
            "type": "number"
        }
    }
}
product_full.json
{
    "title": "Product (Full View)",
    "description" : "Intended to be used when full description is needed",
    "type": "object",
    "extends" : {
        "$ref": "product_short.json"
    },
    "properties": {
        "description": {
            "type": "string"
        },
        "categories": {
            "type" : "array",
            "items" : {
              "$ref": "category.json"
            }
        }
    }
}
product_short.json
{
    "title": "Product (Short View)",
    "description" : "Intended to be used in search results",
    "type": "object",
    "extends" : {
        "$ref": "identified_object.json"
    },
    "properties": {
        "name": {
            "type": "string"
        },
        "list_price": {
            "$ref": "price.json"
        },
        "discount_price": {
            "$ref": "price.json"
        }
    }
}

Features

jerg supports:

jerg also supports a few extensions to JSON schema:

Build

jerg is built with rebar.

Run:

rebar get-deps compile
rebar escriptize skip_deps=true

Usage

$ jerg
Usage: jerg [-s <source>] [-t [<target>]] [-p <prefix>] [-v]

  -s, --source   Source directory or file.
  -t, --target   Generation target. Use - to output to stdout. [default: include/json_records.hrl]
  -p, --prefix   Records name prefix.
  -v, --version  Display version information and stop.

jerg is intended to be used as part of your build chain in order to produce Erlang records right before compilation starts.

If you use rebar, this is done by adding the following to your rebar.config:

{pre_hooks, [{compile, "jerg -s priv/json_schemas"}]}.

This assumes that your JSON schemas are located in priv/json_schemas and you're OK with outputting the generated records to include/json_records.hrl.

What next?

Limitations

jerg doesn't support the following:

Copyright 2013 - David Dossot - MIT License