Documentation ¶
Overview ¶
Package modelgen provides core functionality to implement Model code generators based on a schema.
It allows to create and customize a text/template that can generate the Model types that libovsdb can work with.
Index ¶
- func AtomicType(atype string) string
- func FieldName(column string) string
- func FieldType(tableName, columnName string, column *ovsdb.ColumnSchema) string
- func FieldTypeWithEnums(tableName, columnName string, column *ovsdb.ColumnSchema) string
- func FileName(table string) string
- func GetDBTemplateData(pkg string, schema *ovsdb.DatabaseSchema) map[string]interface{}
- func NewDBTemplate() *template.Template
- func NewTableTemplate() *template.Template
- func StructName(tableName string) string
- func Tag(column string) string
- type Enum
- type Field
- type Generator
- type Option
- type TableInfo
- type TableTemplateData
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtomicType ¶
AtomicType returns the string type of an AtomicType
func FieldType ¶
func FieldType(tableName, columnName string, column *ovsdb.ColumnSchema) string
FieldType returns the string representation of a column type without enum types expansion
func FieldTypeWithEnums ¶
func FieldTypeWithEnums(tableName, columnName string, column *ovsdb.ColumnSchema) string
FieldTypeWithEnums returns the string representation of a column type where Enums are expanded into their own types
func GetDBTemplateData ¶
func GetDBTemplateData(pkg string, schema *ovsdb.DatabaseSchema) map[string]interface{}
GetDBTemplateData returns the map needed to execute the DBTemplate. It has the following keys: DatabaseName: (string) the database name PackageName : (string) the package name Tables: []Table list of Tables that form the Model
func NewDBTemplate ¶
NewDBTemplate return a new DBModel template It includes the following other templates that can be overridden to customize the generated file "header" "preDBDefinitions" "postDBDefinitions" It is design to be used with a map[string] interface and some defined keys (see GetDBTemplateData)
func NewTableTemplate ¶
NewTableTemplate returns a new table template It includes the following other templates that can be overridden to customize the generated file "header" "preStructDefinitions" "structComment" "extraFields" "extraTags" "postStructDefinitions" It is design to be used with a map[string] interface and some defined keys (see GetTableTemplateData) In addition, the following functions can be used within the template: "PrintVal": prints a field value "FieldName": prints the name of a field based on its column "FieldType": prints the field name based on its column and schema "FieldTypeWithEnums": same as FieldType but with enum type expansion "OvsdbTag": prints the ovsdb tag
Example ¶
schemaString := []byte(` { "name": "MyDB", "version": "0.0.0", "tables": { "table1": { "columns": { "string_column": { "type": "string" }, "some_integer": { "type": "integer" } } } } }`) var schema ovsdb.DatabaseSchema err := json.Unmarshal(schemaString, &schema) if err != nil { panic(err) } base := NewTableTemplate() data := GetTableTemplateData("mypackage", "table1", schema.Table("table1")) // Add a function at after the struct definition // It can access the default data values plus any extra field that is added to data _, err = base.Parse(`{{define "postStructDefinitions"}} func (t {{ index . "StructName" }}) {{ index . "FuncName"}}() string { return "bar" }{{end}}`) if err != nil { panic(err) } data["FuncName"] = "TestFunc" gen, err := NewGenerator(WithDryRun()) if err != nil { panic(err) } err = gen.Generate("generated.go", base, data) if err != nil { panic(err) }
Output:
func StructName ¶
StructName returns the name of the table struct
Types ¶
type Field ¶
type Field struct { Column string Schema *ovsdb.ColumnSchema }
Field represents the field information
type Generator ¶
type Generator interface { Generate(string, *template.Template, interface{}) error Format(*template.Template, interface{}) ([]byte, error) }
Generator is an interface that allows to format code from a template and write it to a file
func NewGenerator ¶
NewGenerator returns a new Generator
type TableTemplateData ¶
type TableTemplateData map[string]interface{}
TableTemplateData represents the data used by the Table Template
func GetTableTemplateData ¶
func GetTableTemplateData(pkg, name string, table *ovsdb.TableSchema) TableTemplateData
GetTableTemplateData returns the TableTemplateData map. It has the following keys: TableName: (string) the table name PackageName : (string) the package name StructName: (string) the struct name Fields: []Field a list of Fields that the struct has
func (TableTemplateData) WithEnumTypes ¶
func (t TableTemplateData) WithEnumTypes(val bool)
WithEnumTypes configures whether the Template should expand enum types or not Enum expansion (true by default) makes the template define an type alias for each enum type and a const for each possible enum value