generate

package
v0.0.0-...-eae5a92 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2024 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const (
	TplFuncIsSysField = "isSysField"
)

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "generate",
	Short: "Generate code based on templates",
	Long:  `Generate code for different layers like module, model, and API based on predefined templates.`,
	Run: func(cmd *cobra.Command, args []string) {

		if cfg == nil {
			workDir, _ := os.Getwd()
			configFilepath := filepath.Join(workDir, "config", "config.yaml")
			conf.LoadConfig(configFilepath, &cfg)
		}

		if err := glog.NewLogger(&cfg.Log, glog.WithZapOptions(zap.AddCallerSkip(3))); err != nil {
			panic("glog initZapLogger error")
		}

		if MysqlClient == nil {
			mysqlClient, getMysqlClientErr := dbClient.InitMysql(cfg.Mysql)
			if getMysqlClientErr != nil {
				panic("get mysql client error")
			}
			MysqlClient = mysqlClient
		}

		mode, _ := cmd.Flags().GetString("mode")
		workDir, _ := os.Getwd()

		if workDir == "" {
			fmt.Println("Please provide a working directory using --workdir flag")
			return
		}

		switch mode {
		case "module":
			if err := genModule(); err != nil {
				fmt.Printf("Error generating module: %v\n", err)
				return
			}
			fmt.Println("Module generated successfully")
		case "model":
			if err := genModel(); err != nil {
				fmt.Printf("Error generating model: %v\n", err)
				return
			}
			fmt.Println("Model generated successfully")
		case "api":
			if err := genApi(); err != nil {
				fmt.Printf("Error generating api: %v\n", err)
				return
			}

		default:
			fmt.Println("Invalid mode. Available modes are: module, model, api")
		}
	},
}

Cmd represents the generate command

View Source
var MysqlClient *gorm.DB

Functions

func CopyEmbeddedTemplatesToTempDir

func CopyEmbeddedTemplatesToTempDir(embeddedFS embed.FS, root string) (string, error)

CopyEmbeddedTemplatesToTempDir 将嵌入的模板文件复制到临时目录,并返回该目录的路径。

func IsSysField

func IsSysField(name string) bool

Types

type ApiConfig

type ApiConfig struct {
	InternalAppRootDir string `yaml:"internal_app_root_dir"` // 项目内当前项目的根目录,如internal/genCode
	ProjectRootDir     string `yaml:"project_root_dir"`      // import目录前缀
	Description        string `yaml:"description"`           // 描述
	PackageName        string `yaml:"package_name"`          // 包名,如user
	SubModuleName      string `yaml:"sub_module_name"`       // 子模块名,如userAccount
	TargetFilename     string `yaml:"target_filename"`       // 目标文件名,生成的代码写入的目标文件名
	HttpMethod         string `yaml:"http_method"`           // http方法
	FunctionName       string `yaml:"function_name"`         // 函数名
	ApiDocTag          string `yaml:"api_doc_tag"`           // api文档tag
	ApiGroup           string `yaml:"api_group"`             // api组,RouterGroup对应的接口路径
	ApiPrefix          string `yaml:"api_prefix"`            // api前缀
	ApiSuffix          string `yaml:"api_suffix"`            // api后缀
}

type ApiExtraParams

type ApiExtraParams struct {
	ServiceName            string
	ProjectRootDir         string
	PackageName            string
	PackagePascalName      string
	Description            string
	TargetFileExist        bool
	IsNewRouter            bool
	HttpMethod             string
	FunctionName           string
	ReceiverTypeName       string
	ReceiverTypePascalName string
	ApiGroup               string
	ApiPrefix              string
	ApiSuffix              string
	ApiDocTag              string
	Template               *template.Template
}

type CodeGen

type CodeGen struct {
	Mode   string       `yaml:"mode"`   // 生成模式,支持:module、model、api
	Module ModuleConfig `yaml:"module"` // 模块生成配置
	Model  ModelConfig  `yaml:"model"`  // 模型生成配置
	Api    ApiConfig    `yaml:"api"`    // 控制器生成配置
}

type Config

type Config struct {
	Mysql   dbClient.MysqlConfig `yaml:"mysql"`
	Log     glog.LoggerConfig    `yaml:"log"`
	CodeGen CodeGen              `yaml:"code_gen"`
}

type ModelConfig

type ModelConfig struct {
	InternalAppRootDir string `yaml:"internal_app_root_dir"` // 项目内当前项目的根目录,如internal/genCode
	ProjectRootDir     string `yaml:"project_root_dir"`      // import目录前缀
	Description        string `yaml:"description"`           // 描述
	PackageName        string `yaml:"package_name"`          // 包名
	TableName          string `yaml:"table_name"`            // 表名
}

type ModelExtraParams

type ModelExtraParams struct {
	ServiceName       string
	ProjectRootDir    string
	PackageName       string
	PackagePascalName string
	TableName         string
	Description       string
	StructName        string
	Template          *template.Template
	ModelFields       []ModelField
}

type ModelField

type ModelField struct {
	FieldName          string // 字段名称
	FieldLowerCaseName string // 字段名称小驼峰
	FieldType          string // 字段数据类型,如int、string
	ColumnName         string // 列名
	ColumnType         string // 列数据类型,如varchar(255)
	Comment            string // 字段注释
	IsPrimaryKey       bool   // 是否是主键
}

type ModuleConfig

type ModuleConfig struct {
	InternalAppRootDir string `yaml:"internal_app_root_dir"` // 项目内当前项目的根目录,如internal/genCode
	ProjectRootDir     string `yaml:"project_root_dir"`      // 项目根目录,如go-gin-web
	Description        string `yaml:"description"`           // 描述
	ApiDocTag          string `yaml:"api_doc_tag"`           // api文档tag
	ApiGroup           string `yaml:"api_group"`             // api分组
	ApiPrefix          string `yaml:"api_prefix"`            // api前缀
	PackageName        string `yaml:"package_name"`          // 包名
	TableName          string `yaml:"table_name"`            // 表名
}

type ModuleExtraParams

type ModuleExtraParams struct {
	ServiceName            string
	ProjectRootDir         string
	PackageName            string
	PackagePascalName      string
	TableName              string
	Description            string
	StructName             string
	ReceiverTypeName       string
	ReceiverTypePascalName string
	ApiGroup               string
	ApiPrefix              string
	ApiDocTag              string
	Template               *template.Template
	ModelFields            []ModelField
}

Jump to

Keyboard shortcuts

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