modelgen

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2021 License: Apache-2.0 Imports: 10 Imported by: 2

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicType

func AtomicType(atype string) string

AtomicType returns the string type of an AtomicType

func FieldName

func FieldName(column string) string

FieldName returns the name of a column field

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 FileName

func FileName(table string) string

FileName returns the filename of a table

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

func NewDBTemplate() *template.Template

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

func NewTableTemplate() *template.Template

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

func StructName(tableName string) string

StructName returns the name of the table struct

func Tag

func Tag(column string) string

Tag returns the Tag string of a column

Types

type Enum

type Enum struct {
	Type  string
	Alias string
	Sets  []interface{}
}

Enum represents the enum schema type

func FieldEnum

func FieldEnum(tableName, columnName string, column *ovsdb.ColumnSchema) *Enum

FieldEnum returns the Enum if the column is an enum type

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

func NewGenerator(opts ...Option) (Generator, error)

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 TableInfo

type TableInfo struct {
	TableName  string
	StructName string
}

TableInfo represents the information of a table needed by the Model template

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL