sys_auto_code.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package system
  2. import (
  3. "errors"
  4. "go/token"
  5. "log-server/global"
  6. )
  7. // AutoCodeStruct 初始版本自动化代码工具
  8. type AutoCodeStruct struct {
  9. StructName string `json:"structName"` // Struct名称
  10. TableName string `json:"tableName"` // 表名
  11. PackageName string `json:"packageName"` // 文件名称
  12. HumpPackageName string `json:"humpPackageName"` // go文件名称
  13. Abbreviation string `json:"abbreviation"` // Struct简称
  14. Description string `json:"description"` // Struct中文名称
  15. AutoCreateApiToSql bool `json:"autoCreateApiToSql"` // 是否自动创建api
  16. AutoMoveFile bool `json:"autoMoveFile"` // 是否自动移动文件
  17. Fields []*Field `json:"fields,omitempty"`
  18. HasTimer bool
  19. DictTypes []string `json:"-"`
  20. Package string `json:"package"`
  21. PackageT string `json:"-"`
  22. }
  23. // KeyWord 是go关键字的处理加上 _ ,防止编译报错
  24. // Author [SliverHorn](https://github.com/SliverHorn)
  25. func (a *AutoCodeStruct) KeyWord() {
  26. if token.IsKeyword(a.Abbreviation) {
  27. a.Abbreviation = a.Abbreviation + "_"
  28. }
  29. }
  30. type Field struct {
  31. FieldName string `json:"fieldName"` // Field名
  32. FieldDesc string `json:"fieldDesc"` // 中文名
  33. FieldType string `json:"fieldType"` // Field数据类型
  34. FieldJson string `json:"fieldJson"` // FieldJson
  35. DataTypeLong string `json:"dataTypeLong"` // 数据库字段长度
  36. Comment string `json:"comment"` // 数据库字段描述
  37. ColumnName string `json:"columnName"` // 数据库字段
  38. FieldSearchType string `json:"fieldSearchType"` // 搜索条件
  39. DictType string `json:"dictType"` // 字典
  40. }
  41. var AutoMoveErr error = errors.New("创建代码成功并移动文件成功")
  42. type SysAutoCode struct {
  43. global.GVA_MODEL
  44. PackageName string `json:"packageName" gorm:"comment:包名"`
  45. Label string `json:"label" gorm:"comment:展示名"`
  46. Desc string `json:"desc" gorm:"comment:描述"`
  47. }
  48. type AutoPlugReq struct {
  49. PlugName string `json:"plugName"` // 必然大写开头
  50. Snake string `json:"snake"` // 后端自动转为 snake
  51. RouterGroup string `json:"routerGroup"`
  52. HasGlobal bool `json:"hasGlobal"`
  53. HasRequest bool `json:"hasRequest"`
  54. HasResponse bool `json:"hasResponse"`
  55. NeedModel bool `json:"needModel"`
  56. Global []AutoPlugInfo `json:"global,omitempty"`
  57. Request []AutoPlugInfo `json:"request,omitempty"`
  58. Response []AutoPlugInfo `json:"response,omitempty"`
  59. }
  60. func (a *AutoPlugReq) CheckList() {
  61. a.Global = bind(a.Global)
  62. a.Request = bind(a.Request)
  63. a.Response = bind(a.Response)
  64. }
  65. func bind(req []AutoPlugInfo) []AutoPlugInfo {
  66. var r []AutoPlugInfo
  67. for _, info := range req {
  68. if info.Effective() {
  69. r = append(r, info)
  70. }
  71. }
  72. return r
  73. }
  74. type AutoPlugInfo struct {
  75. Key string `json:"key"`
  76. Type string `json:"type"`
  77. Desc string `json:"desc"`
  78. }
  79. func (a AutoPlugInfo) Effective() bool {
  80. return a.Key != "" && a.Type != "" && a.Desc != ""
  81. }