generate

package
v0.0.0-...-ebdf1d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2018 License: MIT Imports: 11 Imported by: 15

Documentation

Overview

Package generate will parse and create files from template pairs. A template pair is a set of template files:

  • default.json - json file
  • default.gen - any type of text file

Both files are template files and are parsed using the Go text/template package. The 'jay generate' tool loops through the first level of key pairs for empty strings. For every empty string, an argument is required to be passed (whether empty or not) to the 'jay generate' command.

Let's look at generate/model/default.json:

{
  "config.type": "single",
  "config.output": "model/{{.package}}/{{.package}}.go",
  "package": "",
  "table": ""
}

Let's break down this command into pieces: jay generate model/default package:automobile table:car

Argument: 'model/default' Specifies generate/model/default.json and generate/model/default.gen are the template pair.

Argument: 'package:automobile' The key, 'package', from default.json will be filled with the value: 'automobile'

Argument: 'table:car' The key, 'table', from default.json will be filled with the value: 'car'

The .json file is actually parsed up to 100 times (LoopLimit of 100 can be changed at the package level) to ensure all variables like '{{.package}}' are set to the correct value.

In the first iteration of parsing, the 'package' key is set to 'car'. In the second iteration of parsing, the '{{.package}}' variables are set to 'car' also since the 'package' key becomes a variable.

All first level keys (info, package, table) become variables after the first iteration of parsing so they can be used without the file. If a variable is misspelled and is never filled, a helpful error will be displayed.

The 'output' key under 'info' is required. It should be the relative output file path to the project root for the generated file.

The folder structure of the templates (model, controller, etc) has no effect on the generation, it's purely to aid with organization of the template pairs.

You must store the path to the env.json file in the environment variable: JAYCONFIG. The file is at project root that is prepended to all relative file paths.

Examples:

jay generate model/default package:car table:car
Generate a new model from variables in model/default.json and applies
those variables to model/default.gen.
jay generate controller/default package:car url:car model:car view:car
Generate a new controller from variables in controller/default.json
and applies those variables to controller/default.gen.

Flags:

Argument 1 - model/default or controller/default
Relative path without an extension to the template pair. Any combination
of folders and files can be used.
Argument 2,3,etc - package:car
Key pair to set in the .json file. Required for every empty key in the
.json file.

Index

Constants

This section is empty.

Variables

View Source
var (
	// LoopLimit specified the max number of iterations of replacing variables
	// with values to prevent an infinite loop.
	LoopLimit = 100
)

Functions

func Run

func Run(args []string, projectFolder string, templateFolder string) error

Run starts the template generation logic

Types

type Container

type Container struct {
	Generation Info `json:"Generation"`
}

Container is an easy way to read only the generation info from the config file.

func (*Container) ParseJSON

func (c *Container) ParseJSON(b []byte) error

ParseJSON unmarshals bytes to structs

type Info

type Info struct {
	// TemplateFolder is the folder path for the code generation templates
	TemplateFolder string `json:"TemplateFolder"`
}

Info holds the config.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL