modelgen

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: Apache-2.0 Imports: 12 Imported by: 3

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 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

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`: 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

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
  • `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.

Jump to

Keyboard shortcuts

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