base

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CommandNameModel   = "gen:model"
	CommandNameService = "gen:service"
)
View Source
const (
	OptBaseDir        = "base-dir"
	OptBaseDirDefault = "./"
	OptBaseDirDesc    = "Application working directory"
	OptBaseDirShort   = 'b'

	OptDsn     = "dsn"
	OptDsnDesc = "Database source name for connection, e.g. root:pass@tcp(127.0.0.1:3306)/test"

	OptExport      = "export"
	OptExportDesc  = "Specify export names, split with comma"
	OptExportShort = 'e'

	OptModelPath        = "model-path"
	OptModelPathDefault = "app/models"
	OptModelPathDesc    = "Generated model files location"
	OptModelPathShort   = 'm'

	OptOverride     = "override"
	OptOverrideDesc = "Override existing files"

	OptPrefix      = "prefix"
	OptPrefixDesc  = "Prefix name for table"
	OptPrefixShort = 'p'

	OptTargetPath      = "target-path"
	OptTargetPathDesc  = "Generated files saved to"
	OptTargetPathShort = 't'
)

Variables

View Source
var (
	RegexStartLetter         = regexp.MustCompile(`^(\S)`)
	RegexStartWithUnderline  = regexp.MustCompile(`^_+`)
	RegexMiddleWithUnderline = regexp.MustCompile(`_+(\S)`)
	RegexMatchUpperLetter    = regexp.MustCompile(`([A-Z])`)
	RegexMatchManyUnderlines = regexp.MustCompile(`_+`)

	RegexMatchColumnExtraAutoIncrement = regexp.MustCompile(`auto_increment`)
	RegexMatchColumnType               = regexp.MustCompile(`^([_a-zA-Z0-9]+)`)
)

Functions

This section is empty.

Types

type Column

type Column struct {
	Field     ItemName      `xorm:"Field"`
	Collation ItemCollation `xorm:"Collation"`
	Comment   ItemComment   `xorm:"Comment"`
	Type      ColumnType    `xorm:"Type"`
	Default   ColumnDefault `xorm:"Default"`
	Key       ColumnKey     `xorm:"Key"`
	Null      ColumnNull    `xorm:"Null"`
	Extra     ColumnExtra   `xorm:"Extra"`

	// Related on table.
	Table *Table `xorm:"-"`

	// Info fields.
	CommentList []template.HTML `xorm:"-"`
	Datetime    template.HTML   `xorm:"-"`
	Script      template.HTML   `xorm:"-"`

	// Name and type fields.
	ExportName string `xorm:"-"` // for field name. e.g. Id, UserId etc
	ExportType string `xorm:"-"` // for field type. e.g. int, db.Date, db.Datetime etc
	ExportJson string `xorm:"-"` // for field tag. e.g. id, user_id etc
	ExportOrm  string `xorm:"-"` // for field tag. e.g. id pk autoincr, user_id etc
	ExportPkg  string `xorm:"-"` // for field tag. e.g. models, services etc

	IsDate             bool `xorm:"-"`
	IsDatetime         bool `xorm:"-"`
	IsDatetimeOnUpdate bool `xorm:"-"`
	IsPrimaryKey       bool `xorm:"-"`
}

Column is a component for table definition (DDL).

type ColumnDefault

type ColumnDefault string

ColumnDefault is a type for field name of a table.

func (ColumnDefault) String

func (o ColumnDefault) String() string

type ColumnExtra

type ColumnExtra string

ColumnExtra is a type for field name of a table.

func (ColumnExtra) Convert

func (o ColumnExtra) Convert(field ItemName) string

Convert field name to exported orm name.

.Convert("id")          // return "id pk autoincr"
.Convert("user_id")     // return "user_id"

func (ColumnExtra) IsDatetimeOnUpdate

func (o ColumnExtra) IsDatetimeOnUpdate() bool

func (ColumnExtra) String

func (o ColumnExtra) String() string

type ColumnKey

type ColumnKey string

ColumnKey is a type for field name of a table.

func (ColumnKey) IsPrimary

func (o ColumnKey) IsPrimary() bool

func (ColumnKey) String

func (o ColumnKey) String() string

type ColumnNull

type ColumnNull string

ColumnNull is a type for field name of a table.

type ColumnType

type ColumnType string

ColumnType is a type for field name of a table.

func (ColumnType) IsDate

func (o ColumnType) IsDate() bool

func (ColumnType) IsDatetime

func (o ColumnType) IsDatetime() bool

func (ColumnType) Origin

func (o ColumnType) Origin() string

func (ColumnType) String

func (o ColumnType) String() string

type Convertor

type Convertor struct {
	Pkg  string `yaml:"pkg"`
	Type string `yaml:"type"`
}

Convertor is a component used to defined exported type for model generator.

type ItemCollation

type ItemCollation string

ItemCollation is a type for table and field name.

type ItemComment

type ItemComment string

ItemComment is a type for table and field name.

func (ItemComment) String

func (o ItemComment) String() string

String returns a comment string without new line.

func (ItemComment) Strings

func (o ItemComment) Strings() []string

Strings returns a comment string list with new line.

type ItemName

type ItemName string

ItemName is a type for table and field name.

func (ItemName) Allow

func (o ItemName) Allow() bool

Allow return true if start with an a-z letter.

Output:

return true  // user
return false // _user

func (ItemName) FileName

func (o ItemName) FileName(prefix string) (name string)

FileName convert to file name without extension name.

Rule:

  • table name : `cdb_user_info`
  • prefix : `cdb_`
  • file name : `user_info`

Output:

return "user_info"

func (ItemName) String

func (o ItemName) String() string

String returns the origin name with any formats.

Output:

return "user_info"
return "UserInfo"
return "userInfo"

func (ItemName) StructName

func (o ItemName) StructName(prefix string) (name string)

StructName convert to struct name that starts with an upper-case letter.

Rule:

  • table name : `cdb_user_info`
  • prefix : `cdb_`
  • struct name : `UserInfo`

Output:

return "UserInfo"

func (ItemName) ToLargeCamel

func (o ItemName) ToLargeCamel() string

ToLargeCamel returns a large camel that equals to struct name.

Output:

return "UserInfo"

func (ItemName) ToSmallCamel

func (o ItemName) ToSmallCamel() string

ToSmallCamel returns a small-camel format name.

Output:

return "userInfo"

func (ItemName) ToSnake

func (o ItemName) ToSnake() string

ToSnake returns a snake format name.

Output:

return "user_info"

type Table

type Table struct {
	// Normal fields.
	Engine    string        `xorm:"Engine"`
	Format    string        `xorm:"Row_format"`
	Name      ItemName      `xorm:"Name"`
	Comment   ItemComment   `xorm:"Comment"`
	Collation ItemCollation `xorm:"Collation"`

	// Belongs to.
	Columns       []*Column `xorm:"-"`
	PackageList   []string
	PackageMapper map[string]bool

	// Info fields.
	CommentList []template.HTML `xorm:"-"`
	Datetime    template.HTML   `xorm:"-"`
	Script      template.HTML   `xorm:"-"`

	// Execution fields.
	ModelName   string `xorm:"-"`
	ModelPkg    string `xorm:"-"`
	ServiceName string `xorm:"-"`
	ServicePkg  string `xorm:"-"`

	// Primary prop.
	PrimaryName      string `xorm:"-"`
	PrimaryFieldName string `xorm:"-"`
	PrimaryType      string `xorm:"-"`
}

Table is a component for table definition (DDL).

func (*Table) Add

func (o *Table) Add(pkg string)

type Target

type Target string

Target is a type name for generated file path.

Example:

Target("/data/sketch/app/models/example.go")
Target("/data/sketch/app/services/example_service.go")

func (Target) Can

func (o Target) Can(override bool) (bool, error)

Can return true if target file can be saved.

func (Target) Save

func (o Target) Save(_ context.Context, text string, result any) (err error)

Save template text to target file.

func (Target) String

func (o Target) String() string

String returns a path string.

Output:

/data/sketch/app/models/example.go
/data/sketch/app/services/example_service.go

Jump to

Keyboard shortcuts

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