goweb实战全家桶:gowebfactroy-v2代码工厂重构之二:工厂创建代码过程

开源代码:

git clone https://gitee.com/ichub/gowebfactroy-v2.git

uml

swagger

目录

配置参数

读取元数据

转换模型数据

填充模板参数

获取代码模板文件

更新文件参数内容生成代码

更新文件内容名

保存代码


配置参数

factroy:
  author: leijianming@163.com
  
  pkgNow: gitee.com/ichub/gowebfactroynew/mysql/
  pkgNew: gitee.com/ichub/gowebfactroynew/code/mysql/db/

标准接口

type Iface interface {
    Build() *dto.TableFile
    Istest() bool
    BuildFile() *dto.TableFile
    BuildCode() string
    BuildFiles(genpath string, suffux string, content string) *dto.TableFile

    SetTableFactroy(tf *table.TableFactroy)
    GetTableFactroy() *table.TableFactroy
}

代码模板文件:

元数据转换模型参数:

 
func (model *ModelFactroy) Build() *dto.TableFile {

    model.ModelFactors = model.BuildModel()
    return  model.BuildFiles(model.GendirModel, ".go", model.BuildCode())
     

}
func (this *TableFactroy) BuildModel() *dto.ModelFactors {
    if len(this.Tables) == 0 {
       this.FindTables()
    }
    var columns = this.FindMetadata().Columns

    var models = this.makeModelBodyGo(columns)
    var modelEntities = this.makeModelDtoGo(columns)
    var modelParams = this.makeModelParamsGo(columns)
    var modelProtos = this.MakeModelProtoBody(columns)

    var m = &dto.ModelFactors{
       Columns:       columns,
       Models:        models,
       ModelEntities: modelEntities,
       ModelParams:   modelParams,
       Protos:        modelProtos,
    }
    m.InitProxy(m)
    return m

}

填充模板参数

func (s *ServiceFactroy) BuildCode() string {

    dbTemplate := s.DbTemplate
    dbTemplate.FileKey = filetemplate.KEY_SERVICE
    dbTemplate.TableDto = s.TableFactroy.TableDto
    dbTemplate.Put("ModelName", stringutils.Case2Camel(s.TableFactroy.TableName))

    if s.TableFactroy.FindGoType(s.TableFactroy.GetPkInfo().PkeyType) == "string" {
       dbTemplate.Put("PkeyValue", `""`)
       dbTemplate.Put("fmtType", "%s")
    }else{
       dbTemplate.Put("PkeyValue", "0")
       dbTemplate.Put("fmtType", "%d")

    }

    return s.Parse()
}

更新文件内容:

s.Parse()

生成代码文件

func (model *BuildFactroy) BuildFiles(genpath string, suffux string, content string) *dto.TableFile {
    model.TableFile = dto.Default()

    tablebase := stringutils.Lcfirst(model.TableName)
    // model.TableFile.Dir = model.TransDir(genpath, tablebase)
    //确定目录     
    model.TableFile.Dir = model.TransDir(genpath, "")
    model.TableFile.File = tablebase + suffux
    model.TableFile.Content = content

    model.FilePaths.RegisterModelFile(model.TableName, model.TableFile.FullPathFile())
    model.AppenTableFile(model.TableFile)
    model.WriteFileModule(model.TableFile)
    goutils.Info(model.TableFile)
    return model.TableFile
}
func (this *FileFactroy) WriteFileModule(tableFile *dto.TableFile) {
     //更新包名
    tableFile.Content = this.ReplacePkg(tableFile.Content)
    this.WriteFile(tableFile)

}

生成结果

生成代码:

生成测试代码:

下载 文件

  • 46
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值