schema.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package rest
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. )
  7. var (
  8. errDbTypeUnsupported = errors.New("database type unsupported")
  9. )
  10. const (
  11. ScenarioCreate = "create"
  12. ScenarioUpdate = "update"
  13. ScenarioDelete = "delete"
  14. ScenarioSearch = "search"
  15. ScenarioExport = "export"
  16. ScenarioList = "list"
  17. ScenarioView = "view"
  18. ScenarioMapping = "mapping"
  19. )
  20. const (
  21. MatchExactly = "exactly" //精确匹配
  22. MatchFuzzy = "fuzzy" //模糊匹配
  23. )
  24. const (
  25. LiveTypeDropdown = "dropdown"
  26. LiveTypeCascader = "cascader"
  27. )
  28. const (
  29. TypeInteger = "integer"
  30. TypeFloat = "float"
  31. TypeBoolean = "boolean"
  32. TypeString = "string"
  33. )
  34. const (
  35. FormatInteger = "integer"
  36. FormatFloat = "float"
  37. FormatBoolean = "boolean"
  38. FormatString = "string"
  39. FormatText = "text"
  40. FormatDropdown = "dropdown"
  41. FormatDatetime = "datetime"
  42. FormatDate = "date"
  43. FormatTime = "time"
  44. FormatTimestamp = "timestamp"
  45. FormatPassword = "password"
  46. )
  47. type (
  48. LiveValue struct {
  49. Enable bool `json:"enable"`
  50. Type string `json:"type"`
  51. Url string `json:"url"`
  52. Columns []string `json:"columns"`
  53. }
  54. EnumValue struct {
  55. Label string `json:"label"`
  56. Value string `json:"value"`
  57. Color string `json:"color"`
  58. }
  59. VisibleCondition struct {
  60. Column string `json:"column"`
  61. Values []interface{} `json:"values"`
  62. }
  63. Rule struct {
  64. Min int `json:"min"`
  65. Max int `json:"max"`
  66. Type string `json:"type"`
  67. Unique bool `json:"unique"`
  68. Required []string `json:"required"`
  69. Regular string `json:"regular"`
  70. }
  71. Attribute struct {
  72. Match string `json:"match"` //匹配模式
  73. PrimaryKey bool `json:"primary_key"` //是否为主键
  74. DefaultValue string `json:"default_value"` //默认值
  75. Readonly []string `json:"readonly"` //只读场景
  76. Disable []string `json:"disable"` //禁用场景
  77. Visible []VisibleCondition `json:"visible"` //可见条件
  78. Values []EnumValue `json:"values"` //值
  79. Live LiveValue `json:"live"` //延时加载配置
  80. Icon string `json:"icon"` //显示图标
  81. Sort bool `json:"sort"` //是否允许排序
  82. Suffix string `json:"suffix"` //追加内容
  83. Tooltip string `json:"tooltip"` //字段提示信息
  84. Description string `json:"description"` //字段说明信息
  85. }
  86. Scenarios []string
  87. Schema struct {
  88. Id uint64 `json:"id" gorm:"primary_key"`
  89. CreatedAt int64 `json:"created_at" gorm:"autoCreateTime"` //创建时间
  90. UpdatedAt int64 `json:"updated_at" gorm:"autoUpdateTime"` //更新时间
  91. Namespace string `json:"namespace" gorm:"column:namespace;type:char(60);index"` //域
  92. ModuleName string `json:"module_name" gorm:"column:module_name;type:varchar(60);index"` //模块名称
  93. TableName string `json:"table_name" gorm:"column:table_name;type:varchar(120);index"` //表名称
  94. Enable uint8 `json:"enable" gorm:"column:enable;type:int(1)"` //是否启用
  95. Column string `json:"column" gorm:"type:varchar(120)"` //字段名称
  96. Label string `json:"label" gorm:"type:varchar(120)"` //显示名称
  97. Type string `json:"type" gorm:"type:varchar(120)"` //字段类型
  98. Format string `json:"format" gorm:"type:varchar(120)"` //字段格式
  99. Native uint8 `json:"native" gorm:"type:int(1)"` //是否为原生字段
  100. IsPrimaryKey uint8 `json:"is_primary_key" gorm:"type:int(1)"` //是否为主键
  101. Expression string `json:"expression" gorm:"type:varchar(526)"` //计算规则
  102. Scenarios Scenarios `json:"scenarios" gorm:"type:varchar(120)"` //场景
  103. Rule Rule `json:"rule" gorm:"type:varchar(2048)"` //字段规则
  104. Attribute Attribute `json:"attribute" gorm:"type:varchar(4096)"` //字段属性
  105. Position int `json:"position"` //字段排序位置
  106. }
  107. )
  108. func (n Scenarios) Has(str string) bool {
  109. for _, v := range n {
  110. if v == str {
  111. return true
  112. }
  113. }
  114. return false
  115. }
  116. // Scan implements the Scanner interface.
  117. func (n *Scenarios) Scan(value interface{}) error {
  118. if value == nil {
  119. return nil
  120. }
  121. switch s := value.(type) {
  122. case string:
  123. return json.Unmarshal([]byte(s), n)
  124. case []byte:
  125. return json.Unmarshal(s, n)
  126. }
  127. return errDbTypeUnsupported
  128. }
  129. // Value implements the driver Valuer interface.
  130. func (n Scenarios) Value() (driver.Value, error) {
  131. return json.Marshal(n)
  132. }
  133. // Scan implements the Scanner interface.
  134. func (n *Attribute) Scan(value interface{}) error {
  135. if value == nil {
  136. return nil
  137. }
  138. switch s := value.(type) {
  139. case string:
  140. return json.Unmarshal([]byte(s), n)
  141. case []byte:
  142. return json.Unmarshal(s, n)
  143. }
  144. return errDbTypeUnsupported
  145. }
  146. // Value implements the driver Valuer interface.
  147. func (n Attribute) Value() (driver.Value, error) {
  148. return json.Marshal(n)
  149. }
  150. // Scan implements the Scanner interface.
  151. func (n *Rule) Scan(value interface{}) error {
  152. if value == nil {
  153. return nil
  154. }
  155. switch s := value.(type) {
  156. case string:
  157. return json.Unmarshal([]byte(s), n)
  158. case []byte:
  159. return json.Unmarshal(s, n)
  160. }
  161. return errDbTypeUnsupported
  162. }
  163. // Value implements the driver Valuer interface.
  164. func (n Rule) Value() (driver.Value, error) {
  165. return json.Marshal(n)
  166. }