Customizing Inspection Output Programmatically with Go Templates
Atlas supports templating inspection output using Go templates, similar to tools like kubectl and docker. This lets
you generate custom output at runtime.
If you're new to Go templates, see the Go documentation.
Templates are evaluated against the result of a schema inspection, which is an object with two fields:
URL- the inspected sourceRealm- the schema structure, including schemas, tables, columns, etc.
See SchemaInspect
and Realm for full details.
Example: Generating ORM Models
For complete examples of using Go templates to generate ORM models, see:
Template Functions
Atlas includes several built-in template functions to help format and manipulate the output:
| Name | Description |
|---|---|
fail | Stops the template execution with the given error message. |
assert | Asserts that the condition is true, otherwise stops the template execution with the given error message. |
lower | Converts the given string to lower case. |
upper | Converts the given string to upper case. |
replace | Replaces all occurrences of old in s with new. |
trim | Trims leading and trailing whitespace from the given string. |
join | Joins the elements of the given slice of strings into a single string, separated by the given separator. |
splitBy | Splits the given string by the specified separator and returns a slice of strings. |
trimAll | Trims leading and trailing whitespace from each string in the given slice. |
hasPrefix | Checks if the given string starts with the specified prefix. |
hasSuffix | Checks if the given string ends with the specified suffix. |
trimPrefix | Removes the specified prefix from the given string. |
trimSuffix | Removes the specified suffix from the given string. |
sub | Returns the result of subtracting the second integer from the first. |
add | Returns the sum of the provided integers. If no integers are provided, returns 0. |
inc | Increments the given integer by 1 and returns the result. |
mul | Returns the product of two integers. |
div | Returns the result of dividing the first integer by the second. Returns 0 on division by zero. |
mod | Returns the remainder of dividing the first integer by the second. |
txtar | Parses the given string as a txtar archive and returns it as an Archive object. |
exec | Executes the given template with the provided context and returns the result as a trimmed string. Note, unlike the include function, the exec function returns the result of the template as a trimmed strings. |
include | Executes the named template with the provided context and returns the result as a string. |
columnType | Returns the SQL type of the given column as a string. |
dict | Creates a dictionary from a list of key-value pairs. |
get | Retrieves the value associated with the given key from the dictionary. |
set | Sets the value for the given key in the dictionary and returns the updated dictionary. |
unset | Deletes the key from the dictionary and returns the updated dictionary. |
hasKey | Checks if the dictionary contains the specified key. |
list | Creates a list from the provided values. |
append | Appends the given values to the list and returns a new list. |