puttsk / codegen

Generating static code from template and json data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Github GitHub forks

Codegen

Codegen is a simple source-to-source utility for generating a code based on json data. This is useful for embeded data into a code for a small to medium data set. The original goal is used for generating static code for gocountries project.

Installation

go get github.com/puttsk/codegen

Examples

Following is an example of codegen template to generate a function with switch statement in Go language.

Template:

func GetCountryFromAlpha3(alpha3 string) (Country) {
    switch alpha3 {
        << for-block begin >>
        case "{{alpha3}}": 
            return Country{
                Alpha2: "{{alpha2}}", 
                Alpha3: "{{alpha3}}", 
                ID: {{id}}, 
                EN:"{{en}}", 
                TH:"{{th}}"}
        << for-block end >>
        default:
            return Country{}
    }
}

Input data (from world_countries dataset):

[
    {"id":4,"alpha2":"af","alpha3":"afg","en":"Afghanistan","th":"อัฟกานิสถาน"},
    {"id":8,"alpha2":"al","alpha3":"alb","en":"Albania""th":"แอลเบเนีย"},
    ...,
    {"id":894,"alpha2":"zm","alpha3":"zmb","en":"Zambia","th":"แซมเบีย"},
    {"id":716,"alpha2":"zw","alpha3":"zwe","en":"Zimbabwe","th":"ซิมบับเว"}
]

Output:

func GetCountryFromAlpha3(alpha3 string) Country {
    switch alpha3 {
    case "afg":
        return Country{
            Alpha2: "af", 
            Alpha3: "afg", 
            ID: 4, 
            EN: "Afghanistan", 
            TH: "อัฟกานิสถาน"}
    case "alb":
        return Country{
            Alpha2: "al", 
            Alpha3: "alb", 
            ID: 8,
            EN: "Albania", 
            TH: "แอลเบเนีย"}
    ...
    default:
        return Country{}
    }
}

Usage

usage: codegen.py [-h] [-d PATH] [-o OUTPUT] [-t TEMPLATE]

Convert input data to an output file based on template

optional arguments:
  -h, --help            show this help message and exit
  -d PATH, --data PATH  path to data file
  -o OUTPUT, --output OUTPUT
                        output file name
  -t TEMPLATE, --template TEMPLATE
                        path to template file

Input Data

Codegen currently support JSON as input. In current version, the input JSON can contain only a list of objects as follows.

[
    {
        "field1": "", 
        "field2": "", 
    }, 
    {
        "field1": "", 
        "field2": "",
        "field3": "", 
    },
    ...
]

Template

Codegen generates an output based on a template file. Codegen template contains two special sequences: <<...>> and {{...}}, which indicate control statement and variables, respectively.

Control statements determines how codegen will handle the content of the template inside the control block. A control block is indicated by two control statement of the same name, where the beginning of the block is indicated by a keyword begin and the block ends at the control statement with a keyword end.

Variables indicates where codegen will put value from input data to the template.

Currently, codegen support only for loop as a control statement. For a for loop, codegen iterates through objects from the input data and replaces variables with data from the field, indicated by the variable name, of the object.

Contributing

TBD

Authors

  • Putt Sakdhnagool - Initial work

See also the list of contributors who participated in this project.

Issues / Feature request

You can submit bug / issues / feature request using Tracker.

License

MIT License

About

Generating static code from template and json data

License:MIT License


Languages

Language:Python 100.0%