zerogen

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

zerogen

辅助crud

功能

  • 只能生成没有关联关系的crud

todo

  • 正在尝试提示词生成

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateApiService

func GenerateApiService(
	homeDir string,
	tableName string,
	schema []ColumnSchema,
	typeMappings map[string]TypeMapping,
	prefix string,
	group string,
	serviceName string,
	resourceName string) (string, error)

func GenerateGetByIdLogic

func GenerateGetByIdLogic(
	homeDir string,
	packageName,
	serviceImport,
	modelImport,
	typeImport,
	logicName,
	logicDescription,
	logicFunctionName,
	requestType,
	responseType,
	modelStruct,
	modelInstanceName,
	errorMessage string) (string, error)

func GenerateGormModel

func GenerateGormModel(
	modelPkgName string,
	homeDir string,
	tableName string,
	columns []ColumnSchema,
	typeMappings map[string]TypeMapping) (string, error)

GenerateStruct generates a struct based on the schema info using Go templates

func GetCopyTemplate

func GetCopyTemplate(homeDir string) ([]byte, error)

func GetCreateTemplate

func GetCreateTemplate(homeDir string) ([]byte, error)

func GetDeleteTemplate

func GetDeleteTemplate(homeDir string) ([]byte, error)

func GetGetByIdTemplate

func GetGetByIdTemplate(homeDir string) ([]byte, error)

func GetGetListTemplate

func GetGetListTemplate(homeDir string) ([]byte, error)

func GetGoZeroApiTemplate

func GetGoZeroApiTemplate(homeDir string) ([]byte, error)

func GetGormModelTemplate

func GetGormModelTemplate(homeDir string) ([]byte, error)

func GetTypeMappings

func GetTypeMappings(homeDir string) (map[string]TypeMapping, error)

func GetUpdateTemplate

func GetUpdateTemplate(homeDir string) ([]byte, error)

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase converts snake_case to CamelCase

func ToLowerCamelCase

func ToLowerCamelCase(s string) string

ToLowerCamelCase converts snake_case to lowerCamelCase

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase converts a string from camelCase or PascalCase to snake_case using a state machine approach

func WriteToFile

func WriteToFile(dir string, fileName string, data []byte) error

Types

type ApiService

type ApiService struct {
	Prefix       string       // 路由前缀
	Group        string       // API 分组
	ServiceName  string       // 服务名称
	ModelName    string       // 数据模型名
	ResourceName string       // 路由资源名
	Structs      []StructInfo // 数据表对应的结构体
}

type ColumnSchema

type ColumnSchema struct {
	ColumnName   string  // Column name, e.g., "id", "created_at"
	ColumnType   string  // Column type, e.g., "uuid", "text"
	Nullable     bool    // Column nullable, e.g., true, false
	Length       int64   // Column length, e.g., 128, 9223372036854775807
	DefaultValue *string // Default value of the column, e.g., "now()"
	Comment      string  // Column comment, e.g., "Primary key", "Created time"
}

ColumnSchema represents the schema of a database column

func GetTableSchema

func GetTableSchema(db *gorm.DB, tableName string) ([]ColumnSchema, error)

type Field

type Field struct {
	FieldName        string // Go 结构体字段名(驼峰)
	FieldType        string // Go 类型
	JSONName         string // JSON 序列化字段名
	SnakeCaseName    string // Tag name in snake_case format
	CopyGoZeroToGorm string `yaml:"copyGoZeroToGorm"`
	CopyGormToGoZero string `yaml:"copyGormToGoZero"`
	Nullable         bool
	Comment          string // Column comment
}

type StructInfo

type StructInfo struct {
	StructName   string   // Go 结构体名
	TableName    string   // 数据库表名
	Fields       []Field  // 表字段信息
	GoImportPath []string //
}

type TypeMapping

type TypeMapping struct {
	Default          string `yaml:"default"`
	Gorm             string `yaml:"gorm"`
	Gozero           string `yaml:"gozero"`
	GormImportPath   string `yaml:"gormImportPath"`
	CopyGoZeroToGorm string `yaml:"copyGoZeroToGorm"`
	CopyGormToGoZero string `yaml:"copyGormToGoZero"`
	CopyPath         string `yaml:"copyPath"`
}

func GoType

func GoType(columnType string,
	nullable bool,
	typeMappings map[string]TypeMapping,
	framework string,
	usePtr bool,
) (string, string, TypeMapping)

GoType returns the Go type for a given database column type based on the framework type mapping, using "default" mapping if no specific framework mapping exists.

type ZeroConfig

type ZeroConfig struct {
	Version string                 `yaml:"version"`
	Global  zeroConfigWithAction   `yaml:"global"`
	Local   []zeroConfigWithAction `yaml:"local"`
}

type ZeroGen

type ZeroGen struct {
	ConfigFile string `clop:"-f;long" usage:"local configuration file path"`
	ZeroGenCore
}

func (*ZeroGen) GenerateCRUDLogic

func (z *ZeroGen) GenerateCRUDLogic(
	columnSchema []ColumnSchema,
	packageName string) (string, error)

GenerateCRUDLogic generates CRUD logic code for a given model

func (*ZeroGen) GenerateCopyFuncs

func (z *ZeroGen) GenerateCopyFuncs(homeDir string, copyDir string, tableName string, schema []ColumnSchema, typeMappings map[string]TypeMapping) (string, error)

func (*ZeroGen) Run

func (z *ZeroGen) Run() error

type ZeroGenCore

type ZeroGenCore struct {
	Dsn              string `clop:"long" usage:"database dsn" yaml:"dsn"`
	ModelDir         string `clop:"long" usage:"gorm model output directory" yaml:"modelDir"`
	GoZeroApiDir     string `clop:"long" usage:"go zero api output directory" yaml:"goZeroApiDir"`
	CopyDir          string `clop:"long" usage:"copy functions output directory" yaml:"copyDir"`
	CrudLogicDir     string `clop:"long" usage:"crud logic output directory" yaml:"crudLogicDir"`
	Table            string `clop:"long" usage:"table name" yaml:"table"`
	Home             string `clop:"long" usage:"template home directory" yaml:"home"`
	Debug            bool   `clop:"long" usage:"debug mode" yaml:"debug"`
	ModelPkgName     string `clop:"long" usage:"gorm model package name" default:"models" yaml:"modelPkgName"`
	ApiPrefix        string `clop:"long" usage:"go zero api file name prefix" yaml:"apiPrefix"`
	ServiceName      string `clop:"long" usage:"go zero api service name" yaml:"serviceName"`
	ApiGroup         string `clop:"long" usage:"go zero api group name" yaml:"apiGroup"`
	ImportPathPrefix string `clop:"long" usage:"copy module import path prefix" yaml:"importPathPrefix"`
	ApiUrlPrefix     string `clop:"long" usage:"api url prefix" default:"/api/v1" yaml:"apiUrlPrefix"`
	CreateHook       bool   `clop:"long" usage:"generate create hook" yaml:"createHook"`
	UpdateHook       bool   `clop:"long" usage:"generate update hook" yaml:"updateHook"`
	GetListHook      bool   `clop:"long" usage:"generate get list hook" yaml:"getListHook"`
	GetByIdHook      bool   `clop:"long" usage:"generate get by id hook" yaml:"getByIdHook"`
	SaveModels       *bool  `clop:"long" usage:"save models to file" yaml:"saveModels"`
	SaveApi          *bool  `clop:"long" usage:"save api to file" yaml:"saveApi"`
	SaveCopy         *bool  `clop:"long" usage:"save copy to file" yaml:"saveCopy"`
	SaveCrudLogic    *bool  `clop:"long" usage:"save crud logic to file" yaml:"saveCrudLogic"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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