一、背景介绍
config
工具:非常契合工具程序的参数配置程序。同时,也是平台开发技术整合的一部分。
gencode_*
工具:基于gorm的代码生成器,用于生成gorm的model代码,并支持grapql schema的生成。
二、代码生成步骤
- 执行
.\main_config.exe -path_project blog_server_v5 -path_modules modules -path_webserver webserver -path_config config_blog_server -path_output blog_server
PS E:\personal\golang\config\build2.0> .\main_config.exe -path_project blog_server_v5 -path_modules modules -path_webserver webserver -path_config config_blog_server -path_output blog_server welcome to use `isqijing/config`, you can contact to qijing by qijing.cloud@qq.com. modules/config_blog_server/proto modules blog_server/modules NOT FINISHED, please execute command below: protoc -I="." --go_out=./blog_server/modules/config_blog_server ./blog_server/modules/config_blog_server/proto/proto_my_config/*.proto protoc -I="." --go-grpc_out=./blog_server/modules/config_blog_server ./blog_server/modules/config_blog_server/proto/proto_my_config/*proto
- 执行
protoc -I="." --go_out=./blog_server/modules/config_blog_server ./blog_server/modules/config_blog_server/proto/proto_my_config/*.proto
和protoc -I="." --go-grpc_out=./blog_server/modules/config_blog_server ./blog_server/modules/config_blog_server/proto/proto_my_config/*proto
PS E:\personal\golang\config\build2.0> protoc -I="." --go_out=./blog_server/modules/config_blog_server ./blog_server/modules/config_blog_server/proto/proto_my_config/*.proto PS E:\personal\golang\config\build2.0> protoc -I="." --go-grpc_out=./blog_server/modules/config_blog_server ./blog_server/modules/config_blog_server/proto/proto_my_config/*proto
- 编写服务端相关实体的结构体,eg:
type DimensionReading struct { ID string `json:"id" gorm:"primary_key"` Author string `json:"author" gorm:"not null"` //作者 Location string `json:"location" gorm:"not null"` //出处 BaseDimension BaseDimension `json:"base_dimension" gorm:"embedded"` EcoComments []*EcoComment `json:"eco_comments" gorm:"many2many:dimension_reading__eco_comment;constraint:OnDelete:CASCADE;comment:这个维度的评论"` Users []*User `json:"users" gorm:"many2many:user__dimension_reading;comment:这个维度属于谁"` }
- 执行
.\main_gencode_models.exe -path_home "E:\personal\golang\blog_server_v5\models"
补全结构体,eg:
type DimensionReading struct { ID string `json:"id" gorm:"primary_key"` Author string `json:"author" gorm:"not null"` //作者 Location string `json:"location" gorm:"not null"` //出处 BaseDimension BaseDimension `json:"base_dimension" gorm:"embedded"` EcoComments []*EcoComment `json:"eco_comments" gorm:"many2many:dimension_reading__eco_comment;constraint:OnDelete:CASCADE;comment:这个维度的评论"` Users []*User `json:"users" gorm:"many2many:user__dimension_reading;comment:这个维度属于谁"` } func (dimensionReading *DimensionReading) BeforeCreate(tx *gorm.DB) error { return BeforeCreateUpdateID(tx) } func InitDimensionReading() { err := DB.AutoMigrate(&DimensionReading{}) if err != nil { log.Panic(err) } }
- 执行
.\main_gencode_schema.exe -path_home "E:\personal\golang\blog_server_v5\models"
生成graphql代码,eg:
var dimensionReadingType = graphql.NewObject(graphql.ObjectConfig{ Name: "DimensionReading", Fields: graphql.Fields{ "id": &graphql.Field{Type: graphql.ID}, "author": &graphql.Field{Type: graphql.String}, "location": &graphql.Field{Type: graphql.String}, "base_dimension": &graphql.Field{Type: baseDimensionType}, "eco_comments": &graphql.Field{Type: graphql.NewList(ecoCommentType)}, "users": &graphql.Field{Type: graphql.NewList(userType)}, }, Description: "dimensionReadingType", })
服务端框ORM代码构建完成