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 ¶
var ( // LoopLimit specified the max number of iterations of replacing variables // with values to prevent an infinite loop. LoopLimit = 100 )