Documentation ¶
Index ¶
- Variables
- func CleanTableName(path string) string
- func ContainsNonLeadingArray(path string) bool
- func EscapePath(s string) string
- func GenerateColumnsSQL(f map[string]Field) string
- func GenerateReferenceSQL(table string) string
- func GenerateSourceSQL(project, table string) string
- func GenerateTagsSQL(project, table string) string
- func Main() int
- func NormaliseKey(s string) string
- func UnmarshalJSONFromCUE(c cue.Value) (cue.Value, error)
- func Unpack(t *Table, c cue.Value, opts ...NameOption)
- type Column
- type Field
- type Model
- type Models
- type NameOption
- type SQLTemplate
- type Source
- type Sources
- type Table
- type Test
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var SnowflakeTypes = map[string]string{
"string": "STRING",
"int": "INTEGER",
"float": "FLOAT",
"struct": "OBJECT",
"list": "ARRAY",
"null": "VARCHAR",
"bool": "BOOLEAN",
}
SnowflakeTypes is a map of CUE types to Snowflake types.
CUE Lang Type Reference: https://cuelang.org/docs/references/spec/#types.
Snowflake Type Reference: https://docs.snowflake.com/en/sql-reference/data-types.html.
Functions ¶
func CleanTableName ¶
CleanTableName derives a table name from a file name in a Snowflake-friendly format.
func ContainsNonLeadingArray ¶
Example ¶
package main import ( "fmt" "github.com/mr-joshcrane/templater" ) func main() { path1 := "meta.mass_edit_custom_type_ids[123]" path2 := "meta.mass_edit_custom_type_ids" path3 := "[123]meta.mass_edit_custom_type_ids" fmt.Println(templater.ContainsNonLeadingArray(path1)) fmt.Println(templater.ContainsNonLeadingArray(path2)) fmt.Println(templater.ContainsNonLeadingArray(path3)) }
Output: true false false
func EscapePath ¶
EscapePath escapes each section of the path into "delimited identifiers". If we didn't do this, we'd end up with a path like: foo.bar.baz instead of "foo"."bar"."baz", which would cause errors.
Reference: https://docs.snowflake.com/en/sql-reference/identifiers-syntax.html#delimited-identifiers.
func GenerateColumnsSQL ¶
Generate the SQL required to declare, rename and typecast the columns in a table in a DBT Project Model.
Example ¶
package main import ( "fmt" "github.com/mr-joshcrane/templater" ) func main() { fields := map[string]templater.Field{ "Team": { Path: "Team", Node: "Team", InferredType: "STRING", }, "Payroll(millions)": { Path: "Payroll(millions)", Node: "Payroll(millions)", InferredType: "FLOAT", }, "Wins": { Path: "Wins", Node: "Wins", InferredType: "INTEGER", }, } fmt.Println(templater.GenerateColumnsSQL(fields)) }
Output: "Payroll(millions)"::FLOAT AS PAYROLL_MILLIONS ,"Team"::STRING AS TEAM ,"Wins"::INTEGER AS WINS
func GenerateReferenceSQL ¶
GenerateReferenceSQL generates a relation for a source table in a DBT Project Model.
Reference: https://docs.getdbt.com/reference/dbt-jinja-functions/ref.
func GenerateSourceSQL ¶
GenerateSourceSQL generates a relation for a source table in a DBT Project Model.
Reference: https://docs.getdbt.com/reference/dbt-jinja-functions/source
Example ¶
package main import ( "fmt" "github.com/mr-joshcrane/templater" ) func main() { PROJECT := "A_ProjectName" TABLE := "A_TableName" fmt.Println(templater.GenerateSourceSQL(PROJECT, TABLE)) }
Output: {{ source('A_PROJECTNAME', 'A_TABLENAME') }}
func GenerateTagsSQL ¶
GenerateTagsSQL generates the config block tags suitable for use in a DBT Project Model.
Reference: https://docs.getdbt.com/reference/resource-configs/tags
Example ¶
package main import ( "fmt" "github.com/mr-joshcrane/templater" ) func main() { PROJECT := "A_ProjectName" TABLE := "A_TableName" fmt.Println(templater.GenerateTagsSQL(PROJECT, TABLE)) }
Output: {{ config(tags=['A_PROJECTNAME', 'A_TABLENAME']) }}
func Main ¶
func Main() int
Main is the entrypoint for the templater. Working in the context of the current working directory as a fs.FS and taking os.Args as a list of fields to unpack it will generate a the following artifacts:
- A [transform] directory containing the DBT SQL transformations of the tables.
- A [public] directory containing the DBT SQL clone transforms.
- Schemas for source, transform, and public models.
func NormaliseKey ¶
NormaliseKey takes a key and attempts to normalise it to a Snowflake-friendly format. It will:
- Convert from camelCase to SCREAMING_SNAKE_CASE
- Cast to uppercase
- Remove any non-underscore/non-alphanumeric characters
- Remove any leading/trailing spaces
- Convert spaces to underscores
- Replace any double underscores with single underscores
- Replace any dots with double underscores
Example ¶
package main import ( "fmt" "github.com/mr-joshcrane/templater" ) func main() { rule_uppercased := "thisisakey" rule_spaces_to_underscore := "this is a key" rule_nonalphanumeric_stripped := "this%^@is``a()*key" rule_dot_separaters_to_double_underscore := "json.payload.and_children" rule_leading_and_trailing_space_trimmed := " THISISAKEY " rule_parenthesised_words_considered_word_boundaries := "(THIS)IS(A)KEY" rule_camel_case_considered_separate_words := "thisIsAKey" fmt.Println(templater.NormaliseKey(rule_uppercased)) fmt.Println(templater.NormaliseKey(rule_spaces_to_underscore)) fmt.Println(templater.NormaliseKey(rule_nonalphanumeric_stripped)) fmt.Println(templater.NormaliseKey(rule_dot_separaters_to_double_underscore)) fmt.Println(templater.NormaliseKey(rule_leading_and_trailing_space_trimmed)) fmt.Println(templater.NormaliseKey(rule_parenthesised_words_considered_word_boundaries)) fmt.Println(templater.NormaliseKey(rule_camel_case_considered_separate_words)) }
Output: THISISAKEY THIS_IS_A_KEY THIS_IS_A_KEY JSON__PAYLOAD__AND_CHILDREN THISISAKEY THIS_IS_A_KEY THIS_IS_A_KEY
func UnmarshalJSONFromCUE ¶ added in v1.4.1
UnmarshalJSONFromCUE takes a cue.Value that is assumed to be a JSON string and attempts to marshal it to JSON, returning an error if unable to do so.
func Unpack ¶
func Unpack(t *Table, c cue.Value, opts ...NameOption)
Unpack constructs a Field from a cue.Value and adds it to the Table. A NameOption can be passed to modify the path of the field. Objects are recursively unpacked. Arrays are not.
Types ¶
type Column ¶
type Column struct { Name string `yaml:"name"` Description *string `yaml:"description, omitempty"` Tests []string `yaml:"tests, omitempty"` }
Column: DBT Reference: https://docs.getdbt.com/reference/resource-properties/columns.
type Field ¶
A Field represents a column and information about how it should be transformed.
Node: Represents the post-transformation target identifier in Snowflake.
Path: Represents the pre-transformation path to the data in the source table.
InferType: Represents the current best guess at Snowflake type inferred from exemplars.
type Model ¶
type Model struct { Name string `yaml:"name"` Description *string `yaml:"description, omitempty"` Tests []Test `yaml:"tests, omitempty"` Columns []Column `yaml:"columns"` }
Models: DBT Reference: https://docs.getdbt.com/docs/dbt-cloud-apis/metadata-schema-model.
type Models ¶
Models: DBT Reference: https://docs.getdbt.com/docs/dbt-cloud-apis/metadata-schema-model.
func GenerateProjectModel ¶
GenerateProject: Generate the Models required in _models_schema.yaml files that help define a (potentially multi-table) DBT project.
type NameOption ¶
type SQLTemplate ¶
SQLTemplate is an intermediate data structure that represents the table to be rendered as a SQL Model in a DBT Project.
type Source ¶
type Source struct { Name string `yaml:"name"` Schema string `yaml:"schema"` Tables []Column `yaml:"tables, omitempty"` }
Sources: DBT Reference: https://docs.getdbt.com/reference/dbt-jinja-functions/source.
type Sources ¶
Sources: DBT Reference: https://docs.getdbt.com/reference/dbt-jinja-functions/source.
type Table ¶
type Table struct { Name string Project string Fields map[string]Field // contains filtered or unexported fields }
A Table represents a source table. It is the intermediate representation of the untyped semi-structured data.
func (*Table) InferFields ¶
InferFields takes a cue.Iterator and walks through it, adding fields to the table. It will also unpack any JSON fields where the column name matches the (optional) unpackPath.
type Test ¶
type Test struct {
Test string `yaml:"test, omitempty"`
}
Test: DBT Reference: https://docs.getdbt.com/reference/resource-properties/tests.