代码拉取完成,页面将自动刷新
// Copyright 2022 marcello<volibearw@gmail.com>. All rights reserved.
package main
import (
"fmt"
"strings"
)
// generateModelStructPart 生成struct部分
func generateModelStructPart(param *CLIParam, tableName string, columns []Columns) string {
var structStr string
if len(columns) == 0 {
// 没有字段的情况
structStr += "type " + toCamelString(tableName) + " struct{\n"
structStr += "}\n"
return structStr
}
structStr += "// " + columns[0].TableNameCame + " 表\n"
structStr += "type " + columns[0].TableNameCame + " struct{\n"
if param.ModelUseGorm && param.ModelIncludeGormModel {
structStr += " gorm.Model\n"
}
// 组装字段部分
var field string
for _, val := range columns {
if param.ModelUseGorm && param.ModelIncludeGormModel {
// 如果使用了 gorm.Model 就必须跳过这几个字段
if isGormDefaultModelField(val.ColumnName) {
continue
}
}
field += " " + val.ColumnNameCame + " " + val.ColumnGoDataType
if param.ModelUseGorm {
var columnAttr []string
// 列名
columnAttr = append(columnAttr, "column:"+val.ColumnName)
// 类型
columnAttr = append(columnAttr, val.ColumnType)
if val.IsNullable == "NO" {
columnAttr = append(columnAttr, "not null")
}
if val.Extra != MySQLExtraAutoIncrement {
// 自增列不太一样
if !val.Default.Valid {
// default is null
columnAttr = append(columnAttr, "default null")
} else {
// default is not null
columnAttr = append(columnAttr, fmt.Sprintf("default '%s'", val.Default.String))
}
}
if val.Extra == MySQLExtraAutoIncrement {
// 自增列表
// AUTO_INCREMENT
columnAttr = append(columnAttr, "autoIncrement")
}
if val.ColumnKey == MySQLColumnKeyPrimaryKey {
// primaryKey
columnAttr = append(columnAttr, "primaryKey")
}
if val.ColumnKey == MySQLColumnKeyUniqueKey {
// unique key
columnAttr = append(columnAttr, "unique")
}
if len(val.Comment) > 0 {
// 备注
columnAttr = append(columnAttr, fmt.Sprintf("comment:'%s'", val.Comment))
}
field += " `gorm:\"" + strings.Join(columnAttr, ";") + "\"`\n"
} else {
field += "\n"
}
}
structStr += field + "}\n\n"
// 生成实例化 New 部分
if param.ModelUseGorm {
structStr += "// TableName 设置表名\n"
structStr += "func (*" + columns[0].TableNameCame + ") TableName() string {\n"
structStr += " return \"" + tableName + "\"\n"
structStr += "}\n\n"
}
return structStr
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。