Introduction

Monstarillo is a template based code generator that works against Postgres, MySQL and Oracle.

How it works

Monstarillo runs templates that are executed by golang’s text template. The templates to run are identified in a json file. At a high level this file will identify the templates to execute, and where to put the output of the template. Monstarillo will also connect to a database and make metadata from the database available to your templates. This metadata includes column information (primary keys, column names, Golang, .Net, Java and Javascript datatypes) and table information (columns and relationships).

Monstarillo will connect to the database you provided and gather information about the tables. It then loops through all of the tables and runs each of your templates. If there are 10 tables in your database each of your template will be run 10 times, once for each table. Your templates have access to the Monstarillo context.

type MonstarilloContext struct {
 Tables             []models.Table
 Tags               []models.Tag
 CurrentTable       models.Table
 CurrentGuiTable    models.GuiListTable
 UnitTestValuesFile string
 GuiListTables      models.GuiListTables
}

During the template execution process the CurrentTable holds the information about the table currently being processed.

Tables an array of all the tables being processed. A table includes an array of Columnsaalsdkf Tags an array of all of the tags defined in the templates.json file CurrentTable the table being processed CurrentGuiTable The GuiListTables of the table being processed. GuiListTable is a data structure used to build guis. UnitTestValuesFile The location of the unit test values file. Monstarillo columns have a property that will return a value that can be used to create unit tests. This value is set based on the column’s database type. You can also set this value yourself by passing in a unit test values file. GuiListTables an array of the GuiListTables

Templates.json

templates

The templates.json file includes an array of templates:

"templates": [
{
  "templateFile": "/templats/go-api/struct.tmpl",
  "generatedFileName": "{{ .CurrentTable.GetCamelCaseTableName}}.go",
  "generatedFolderName": "models",
  "minimumGeneratedFileLength": 0,
  "outputPath": "/output/my-project",
  "overwriteFile": true
},

templateFile: The location of the template to run. generatedFileName: The name of the file(s) that will be generated by the template. generatedFolderName: The name of the folder to put the generated files in. minimumGeneratedFileLength: A generated file is only kept if it exceeds this value. outputPath: The location of generated files. overwriteFile: If a generated file already exists should we overwrite it?

A generated file’s path is outputPath + generatedFolderName + generatedFileName

Tags

The templates.json file includes an array of Tags. Tags can be used in your templates and in the templates.json file to define templateFile, generateFileName, generatedFolderName and outputPath.

{
  "tagName": "TemplateRoot",
  "value": "/home/patrick/code/monstarillo-templates"
},

tagName: The name of the tag. value: The value of the tag.

The tag OutputPath could be used in the templates.json file {{getTag .Tags \"OutputPath\"}}

Include Tables

The templates.json file can include a list of tables to include.

 "includeTables": ["person", "job_status", "course_grades"],

When connecting to a database only the tables in this list will be processed by Monstarillo.

IgnoreTables

The templates.json file can include a list of tables to include.

 "ignoreTables": ["person", "job_status", "course_grades"],

When connecting to a database the tables in this list will be not processed by Monstarillo.