parser

package
v1.1.39 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

Package parser is a library that parses to go structures based on sql and generates the code needed based on the template.

Index

Constants

View Source
const (

	// SubStructKey sub struct key
	SubStructKey = "_sub_struct_"
	// ProtoSubStructKey proto sub struct key
	ProtoSubStructKey = "_proto_sub_struct_"
)
View Source
const (
	// TableName table name
	TableName = "__table_name__"
	// CodeTypeModel model code
	CodeTypeModel = "model"
	// CodeTypeJSON json code
	CodeTypeJSON = "json"
	// CodeTypeDAO update fields code
	CodeTypeDAO = "dao"
	// CodeTypeHandler handler request and respond code
	CodeTypeHandler = "handler"
	// CodeTypeProto proto file code
	CodeTypeProto = "proto"
	// CodeTypeService grpc service code
	CodeTypeService = "service"
	// CodeTypeCrudInfo crud info json data
	CodeTypeCrudInfo = "crud_info"
	// CodeTypeTableInfo table info json data
	CodeTypeTableInfo = "table_info"

	// DBDriverMysql mysql driver
	DBDriverMysql = "mysql"
	// DBDriverPostgresql postgresql driver
	DBDriverPostgresql = "postgresql"
	// DBDriverTidb tidb driver
	DBDriverTidb = "tidb"
	// DBDriverSqlite sqlite driver
	DBDriverSqlite = "sqlite"
	// DBDriverMongodb mongodb driver
	DBDriverMongodb = "mongodb"
)

Variables

This section is empty.

Functions

func ConvertToSQLByMgoFields

func ConvertToSQLByMgoFields(tableName string, fields []*MgoField) (string, map[string]string)

ConvertToSQLByMgoFields convert to mysql table ddl

func ConvertToSQLByPgFields

func ConvertToSQLByPgFields(tableName string, fields PGFields) (string, map[string]string)

ConvertToSQLByPgFields convert to mysql table ddl

func GetMysqlTableInfo

func GetMysqlTableInfo(dsn, tableName string) (string, error)

GetMysqlTableInfo 从 MySQL 数据库中获取表的创建信息

func GetSqliteTableInfo

func GetSqliteTableInfo(dbFile string, tableName string) (string, error)

GetSqliteTableInfo get table info from sqlite

func GetTableInfo

func GetTableInfo(dsn, tableName string) (string, error)

GetTableInfo 从 MySQL 数据库中获取表的创建信息 已废弃:请使用 GetMysqlTableInfo 替代

func MgoFieldToGoStruct

func MgoFieldToGoStruct(name string, fs []*MgoField) string

MgoFieldToGoStruct convert to go struct

func ParseSQL

func ParseSQL(sql string, options ...Option) (map[string]string, error)

ParseSQL generate different usage codes based on sql

func SetJSONTagCamelCase

func SetJSONTagCamelCase()

SetJSONTagCamelCase set json tag format to camel case

func SetJSONTagSnakeCase

func SetJSONTagSnakeCase()

SetJSONTagSnakeCase set json tag format to snake case

func UnMarshalTableInfo added in v1.1.23

func UnMarshalTableInfo(data string) (map[string]interface{}, error)

UnMarshalTableInfo unmarshal the json data to TableInfo struct

Types

type Codes

type Codes struct {
	Model         []string // model code
	UpdateFields  []string // update fields code
	ModelJSON     []string // model json code
	HandlerStruct []string // handler request and respond code
}

Codes content

type CrudInfo added in v1.1.23

type CrudInfo struct {
	TableNameCamel          string `json:"tableNameCamel"`          // camel case, example: FooBar
	TableNameCamelFCL       string `json:"tableNameCamelFCL"`       // camel case and first character lower, example: fooBar
	TableNamePluralCamel    string `json:"tableNamePluralCamel"`    // plural, camel case, example: FooBars
	TableNamePluralCamelFCL string `json:"tableNamePluralCamelFCL"` // plural, camel case, example: fooBars

	ColumnName               string `json:"columnName"`               // column name, example: first_name
	ColumnNameCamel          string `json:"columnNameCamel"`          // column name, camel case, example: FirstName
	ColumnNameCamelFCL       string `json:"columnNameCamelFCL"`       // column name, camel case and first character lower, example: firstName
	ColumnNamePluralCamel    string `json:"columnNamePluralCamel"`    // column name, plural, camel case, example: FirstNames
	ColumnNamePluralCamelFCL string `json:"columnNamePluralCamelFCL"` // column name, plural, camel case and first character lower, example: firstNames

	GoType       string `json:"goType"`       // go type, example: string, uint64
	GoTypeFCU    string `json:"goTypeFCU"`    // go type, first character upper, example: String, Uint64
	ProtoType    string `json:"protoType"`    // proto type, example: string, uint64
	IsStringType bool   `json:"isStringType"` // go type is string or not

	PrimaryKeyColumnName string `json:"PrimaryKeyColumnName"` // primary key, example: id
	IsCommonType         bool   `json:"isCommonType"`         // custom primary key name and type
	IsStandardPrimaryKey bool   `json:"isStandardPrimaryKey"` // standard primary key id
}

CrudInfo crud info for cache, dao, handler, service, protobuf, error

func (*CrudInfo) CheckCommonType added in v1.1.23

func (info *CrudInfo) CheckCommonType() bool

func (*CrudInfo) GetGRPCProtoValidation added in v1.1.23

func (info *CrudInfo) GetGRPCProtoValidation() string

func (*CrudInfo) GetWebProtoValidation added in v1.1.23

func (info *CrudInfo) GetWebProtoValidation() string

type Field added in v1.1.23

type Field struct {
	ColumnName         string // original column name, example: foo_bar
	ColumnNameCamel    string // first character lower, example: FooBar
	ColumnNameCamelFCL string // first character lower, example: fooBar

	ColumnComment string // column comment
	IsPrimaryKey  bool   // is primary key

	GoType string // convert to go type
	Tag    string // tag for model struct field, default gorm tag
}

Field is the struct for column information

type MgoField

type MgoField struct {
	Name           string `json:"name"`
	Type           string `json:"type"`
	Comment        string `json:"comment"`
	ObjectStr      string `json:"objectStr"`
	ProtoObjectStr string `json:"protoObjectStr"`
}

MgoField mongo field

func GetMongodbTableInfo

func GetMongodbTableInfo(dsn string, tableName string) ([]*MgoField, error)

GetMongodbTableInfo get table info from mongodb

type NullStyle

type NullStyle int

NullStyle 定义了处理 SQL 中 NULL 值的不同风格

const (
	NullDisable   NullStyle = iota // 不处理 NULL 值
	NullInSql                      // 在 SQL 语句中使用 NULL
	NullInPointer                  // 使用指针来表示 NULL 值
)

nolint 忽略某些 linter 规则

type Option

type Option func(*options)

Option 是一个函数类型,用于设置选项

func WithCharset

func WithCharset(charset string) Option

WithCharset 设置字符集

func WithCollation

func WithCollation(collation string) Option

WithCollation 设置排序规则

func WithColumnPrefix

func WithColumnPrefix(p string) Option

WithColumnPrefix 设置列名前缀

func WithCustomTemplate added in v1.1.23

func WithCustomTemplate() Option

WithCustomTemplate 设置是否使用自定义扩展模板

func WithDBDriver

func WithDBDriver(driver string) Option

WithDBDriver 设置数据库驱动

func WithEmbed

func WithEmbed() Option

WithEmbed 设置是否嵌入 gorm.Model

func WithExtendedAPI

func WithExtendedAPI() Option

WithExtendedAPI 设置是否生成扩展 API

func WithFieldTypes

func WithFieldTypes(fieldTypes map[string]string) Option

WithFieldTypes 设置字段类型映射

func WithForceTableName

func WithForceTableName() Option

WithForceTableName 设置是否强制使用表名

func WithGormType

func WithGormType() Option

WithGormType 设置是否在 GORM 标签中写入类型

func WithJSONTag

func WithJSONTag(namedType int) Option

WithJSONTag 设置是否生成 JSON 标签以及命名类型

func WithNoNullType

func WithNoNullType() Option

WithNoNullType 设置是否禁用 NULL 类型处理

func WithNullStyle

func WithNullStyle(s NullStyle) Option

WithNullStyle 设置处理 NULL 值的风格

func WithPackage

func WithPackage(pkg string) Option

WithPackage 设置生成代码的包名

func WithTablePrefix

func WithTablePrefix(p string) Option

WithTablePrefix 设置表名前缀

func WithWebProto

func WithWebProto() Option

WithWebProto 设置是否生成包含路由路径和 Swagger 信息的 proto 文件

type PGField

type PGField struct {
	Name         string `gorm:"column:name;" json:"name"`
	Type         string `gorm:"column:type;" json:"type"`
	Comment      string `gorm:"column:comment;" json:"comment"`
	Length       int    `gorm:"column:length;" json:"length"`
	Lengthvar    int    `gorm:"column:lengthvar;" json:"lengthvar"`
	Notnull      bool   `gorm:"column:notnull;" json:"notnull"`
	IsPrimaryKey bool   `gorm:"column:is_primary_key;" json:"is_primary_key"`
}

PGField postgresql field

type PGFields added in v1.1.23

type PGFields []*PGField

func GetPostgresqlTableInfo

func GetPostgresqlTableInfo(dsn string, tableName string) (PGFields, error)

GetPostgresqlTableInfo get table info from postgres

type PrimaryKey added in v1.1.23

type PrimaryKey struct {
	Name               string // primary key name, example: foo_bar
	NameCamel          string // primary key name, camel case, example: FooBar
	NameCamelFCL       string // primary key name, camel case and first character lower, example: fooBar
	NamePluralCamel    string // primary key name, plural, camel case, example: FooBars
	NamePluralCamelFCL string // primary key name, plural, camel case and first character lower, example: fooBars

	GoType    string // go type, example:  int, string
	GoTypeFCU string // go type, first character upper, example: Int64, String

	IsStringType bool // go type is string or not
}

PrimaryKey is the struct for primary key information, it used for generate CRUD code

type SqliteField added in v1.1.23

type SqliteField struct {
	Cid          int    `gorm:"column:cid" json:"cid"`
	Name         string `gorm:"column:name" json:"name"`
	Type         string `gorm:"column:type" json:"type"`
	Notnull      int    `gorm:"column:notnull" json:"notnull"`
	DefaultValue string `gorm:"column:dflt_value" json:"dflt_value"`
	Pk           int    `gorm:"column:pk" json:"pk"`
}

SqliteField sqlite field struct

type SqliteFields added in v1.1.23

type SqliteFields []*SqliteField

SqliteFields sqlite fields

type TableInfo added in v1.1.23

type TableInfo struct {
	TableNamePrefix string // table name prefix, example: t_

	TableName               string // original table name, example: foo_bar
	TableNameCamel          string // camel case, example: FooBar
	TableNameCamelFCL       string // camel case and first character lower, example: fooBar
	TableNamePluralCamel    string // plural, camel case, example: FooBars
	TableNamePluralCamelFCL string // plural, camel case and first character lower, example: fooBars
	TableNameSnake          string // snake case, example: foo_bar

	TableComment string // table comment

	Columns    []Field     // columns of the table
	PrimaryKey *PrimaryKey // primary key information

	DBDriver string // database driver, example: mysql, postgresql, sqlite3, mongodb

	ColumnSubStructure string // column sub structure for model
	ColumnSubMessage   string // sub message for protobuf
}

TableInfo is the struct for extend template

Jump to

Keyboard shortcuts

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