0


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

开源代码:

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

uml

swagger

配置参数

配置参数

factroy:
  author: [email protected]
  
  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)

}

生成结果

生成代码:

生成测试代码:

下载 文件

标签: 重构 go

本文转载自: https://blog.csdn.net/leijmdas/article/details/140062866
版权归原作者 leijmdas 所有, 如有侵权,请联系我们删除。

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

还没有评论