structt

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const ColumnTagId = "dbColumn"

Identifier of the struct tag for "ColumnTag"

View Source
const MetadataFieldName = "DbMetadata_"
View Source
const MetadataTagId = "dbMetadata"

Identifier of the struct tag for "MetadataTag"

View Source
const PackageName = "git.rpjosh.de/RPJosh/go-ddl-parser"

The name of the root package

Variables

This section is empty.

Functions

func CreateStructs

func CreateStructs(conf *StructConfig, tables []*ddl.Table) error

CreateStructs creates all ".go" files with the structs based on the provided configuration and Tables. For any existing go files we have to patch, it's expected that it was created by this function or the file content was linted with golint

func GetFieldName

func GetFieldName(fieldName string) string

GetFieldName returns the name of a struct or field from a database name

func GetGoFileName

func GetGoFileName(tableName string) string

GetGoFileName returns the name of a go file for a table

func GetJsonName

func GetJsonName(fieldName string) string

GetJsonName returns the json key value for the provided fildName of the database. The json keys are CamelCased

Types

type ColumnTag

type ColumnTag struct {

	// Name of the column in the database
	Name string

	// Weather this field is a primary key
	IsPrimaryKey bool

	// Column to which this column points to via a foreign key in
	// format Schema.Table.Column
	ForeignKeyReference string

	// Column from which this struct was referenced (n:1 relations) in
	// format Schema.Table.Column.
	// If this field is present, all other fields are empty
	PointedKeyReference string

	// Mariadb: weather this field has the property "auto_increment"
	AutoIncrement bool

	// Weather this column has a default value
	HasDefaultValue bool
}

ColumnTag contains information that are stored via a struct tag for a column.

func FromColumnTag

func FromColumnTag(tag string) *ColumnTag

FromColumnTag transforms a struct tag containing a "ColumnTag" to the represended struct

func GetColumnTag

func GetColumnTag(col *ddl.Column) *ColumnTag

GetColumnTag returns a "ColumnTag" struct from a ddl column

func (*ColumnTag) ToTag

func (c *ColumnTag) ToTag() (rtc string)

ToTag transforms this columnTag to a string that can be applied as struct tag

type MetadataTag

type MetadataTag struct {

	// Schema the table belongs to
	Schema string

	// Name of the table this struct represents
	Table string
}

MetadataTag is a generic field that is added to every struct to store some metadata about the table. It's used so we do not need to fetch the table structure during runtime

func FromMetadataTag

func FromMetadataTag(tag string) *MetadataTag

FromMetadataTag transforms a struct tag containing a "MetadataTag" to the represended struct

func (*MetadataTag) ToTag

func (c *MetadataTag) ToTag() string

ToTag transforms this columnTag to a string that can be applied as struct tag

type NullConfig

type NullConfig struct {

	// Disable the use of nullable datatypes
	Disable bool

	// Name of the package to import the types from.
	// Defaulting to [database/sql]
	Package string

	// Prefix to use in front of a type name like "String" or "Int64".
	// Defaulting to "sql.Null"
	Prefix sql.NullString

	// Custom function to get the import name and the type name from
	Custom func(typ ddl.DataType, defaultName string) (typeName, imp string)
}

NullConfig configures how to transform nullable columns into a go struct

type Relationship

type Relationship int
const (
	OneToOne Relationship = iota
	OneToMany
	ManyToMany
)

type StructConfig

type StructConfig struct {

	// Absolute or relative base path to write all files to:
	// '/internal/modules/'
	GenericOutputPath string `yaml:"genericOutputPath"`

	// Name of the Go package used for new files
	PackgeName string `yaml:"packageName"`

	// Suffix to add to the struct name for every table
	Suffix string `yaml:"suffix"`

	// Configuration options for a specific table.
	// The key of this map is either the table name (for any schema)
	// or a combination of "schema.tableName"
	Tableconfig map[string]*TableConfig `yaml:"tableConfig"`

	// Configuration of how to handle nullable columns
	NullConfig NullConfig
}

StructConfig contains options used to customize the behaviour of the conversion from a database column to a struct

type TableConfig

type TableConfig struct {

	// Absolute or relative base path to a ".go" file to write this struct to:
	// '/internal/modules/file.go'
	Path string `yaml:"path"`

	// Name of the Go package used for this file
	PackageName string `yaml:"packageName"`

	// Instead of only including the ID of a FK as a field, a full reference to the
	// struct is used for the speicified column names.
	// This is used for "1:1" relationships.
	// Specifiy a single element '*' to include all structs
	IncludeReferencedStructs []string `yaml:"includeReferencedStructs"`

	// Include additional fields for structs that references this table as an array.
	// This is used for "1:n" relationships.
	// To construct a "n:m" relationship you have to add a extra config for the zwischentabelle
	// that only specifies "IncludeRefrencedStructs" for the other column.
	// Note: you have to provide all referenced tables in "CreateStructs"
	IncludePointedStructs bool `yaml:"includePointedStructs"`

	// Sufix to add to the struct name. Add <empty> for no string and override of the default behaviour
	Suffix string `yaml:"suffix"`
}

TableConfig contains options for a specific table

Jump to

Keyboard shortcuts

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