/gendata

Template Data Generator - JSON To GoLang, PHP and other

Primary LanguageGoApache License 2.0Apache-2.0

GenData

Godoc Reference GitHub tag (latest by date) GitHub Workflow Status codecov License


Template Data Generator

Wiki

Doc Syntax Examples

Installation

Follow those steps to install the library:

  1. Import the library our code:
go get github.com/nikitaksv/gendata

or install cli tool

go install github.com/nikitaksv/gendata

Usage

CLI

gendata gen -l go -t testdata/template.txt -d testdata/data.json -o testdata/

Code example PHP Class generator from JSON data:

package main

import (
	"bytes"
	"context"
	"fmt"
	"log"

	gendata "github.com/nikitaksv/gendata/pkg/service"
)

func main() {
	tmpl := []byte(`
<?php

namespace common\models;

using yii\base\BaseObject;

/***
 * Class {{ Name }}
 * @package common\models
 */
class {{ Name }} extends BaseObject
{
{{ Properties }}
	/**
	 * @var {{ Type.Doc }}
	 */
	public {{ Type }} ${{ Name.CamelCase }};
{{ /Properties }}
}
`)
	data := []byte(`
{
  "id": 2,
  "first_name": "Tam",
  "last_name": "Le-Good",
  "email": "tlegood1@so-net.ne.jp",
  "gender": "Bigender",
  "ip_address": "2.92.36.184",
  "addresses": [
    {
      "name": "Home",
      "city": "Test",
      "street": "Test st.",
      "house": "1",
      "coordinates": {
        "lon": 77.2123124,
        "lat": 43.2123124
      }
    }
  ],
  "skills": [
    "go",
    "php"
  ]
}
`)

	srv := gendata.NewService(nil)
	rsp, err := srv.Gen(context.Background(), &gendata.GenRequest{
		Tmpl: tmpl,
		Data: data,
		Config: &gendata.Config{
			Lang:          "php",
			DataFormat:    "json",
			RootClassName: "Gen",
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	for _, file := range rsp.RenderedFiles {
		buf := new(bytes.Buffer)
		_, _ = buf.ReadFrom(file.Content)
		fmt.Println("Filename: ", file.FileName)
		fmt.Println(buf.String())
	}
}