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 returns a new ClientDBModel template. It includes the following other templates that can be overridden to customize the generated file:
- `header`: to include a comment as a header before package definition
- `preDBDefinitions`: to include code after package definition
- `postDBDefinitions`: to include code at the end
It is designed 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`: override the comment as header before package definition
- `preStructDefinitions`: deprecated in favor of `extraImports`
- `extraImports`: include additional imports
- `structComment`: override the comment generated for the table
- `extraFields`: add extra fields to the table
- `extraTags`: add tags to the extra fields
- `deepCopyExtraFields`: copy extra fields when copying a table
- `equalExtraFields`: compare extra fields when comparing a table
- `postStructDefinitions`: deprecated in favor of `extraDefinitions`
- `extraDefinitions`: include additional definitions like functions etc.
It is designed 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 type 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 Option ¶ added in v0.6.0
type Option func(o *options) error
func WithDryRun ¶ added in v0.6.0
func WithDryRun() Option
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
- `TPackageName`: (string) the package name
- `TStructName`: (string) the struct name
- `TFields`: []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
func (TableTemplateData) WithExtendedGen ¶ added in v0.7.0
func (t TableTemplateData) WithExtendedGen(val bool)
WithExtendedGen configures whether the Template should generate code to deep copy models.