schema.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. Method string `json:"method"`
  53. Body string `json:"body"`
  54. ContentType string `json:"content_type"`
  55. Columns []string `json:"columns"`
  56. }
  57. EnumValue struct {
  58. Label string `json:"label"`
  59. Value string `json:"value"`
  60. Color string `json:"color"`
  61. }
  62. VisibleCondition struct {
  63. Column string `json:"column"`
  64. Values []interface{} `json:"values"`
  65. }
  66. Rule struct {
  67. Min int `json:"min"`
  68. Max int `json:"max"`
  69. Type string `json:"type"`
  70. Unique bool `json:"unique"`
  71. Required []string `json:"required"`
  72. Regular string `json:"regular"`
  73. }
  74. Attribute struct {
  75. Match string `json:"match"` //匹配模式
  76. PrimaryKey bool `json:"primary_key"` //是否为主键
  77. DefaultValue string `json:"default_value"` //默认值
  78. Readonly []string `json:"readonly"` //只读场景
  79. Disable []string `json:"disable"` //禁用场景
  80. Visible []VisibleCondition `json:"visible"` //可见条件
  81. Values []EnumValue `json:"values"` //值
  82. Live LiveValue `json:"live"` //延时加载配置
  83. Icon string `json:"icon"` //显示图标
  84. Sort bool `json:"sort"` //是否允许排序
  85. Suffix string `json:"suffix"` //追加内容
  86. Tooltip string `json:"tooltip"` //字段提示信息
  87. Description string `json:"description"` //字段说明信息
  88. }
  89. Scenarios []string
  90. Schema struct {
  91. Id uint64 `json:"id" gorm:"primary_key"`
  92. CreatedAt int64 `json:"created_at" gorm:"autoCreateTime"` //创建时间
  93. UpdatedAt int64 `json:"updated_at" gorm:"autoUpdateTime"` //更新时间
  94. Namespace string `json:"namespace" gorm:"column:namespace;type:char(60);index"` //域
  95. ModuleName string `json:"module_name" gorm:"column:module_name;type:varchar(60);index"` //模块名称
  96. TableName string `json:"table_name" gorm:"column:table_name;type:varchar(120);index"` //表名称
  97. Enable uint8 `json:"enable" gorm:"column:enable;type:int(1)"` //是否启用
  98. Column string `json:"column" gorm:"type:varchar(120)"` //字段名称
  99. Label string `json:"label" gorm:"type:varchar(120)"` //显示名称
  100. Type string `json:"type" gorm:"type:varchar(120)"` //字段类型
  101. Format string `json:"format" gorm:"type:varchar(120)"` //字段格式
  102. Native uint8 `json:"native" gorm:"type:int(1)"` //是否为原生字段
  103. IsPrimaryKey uint8 `json:"is_primary_key" gorm:"type:int(1)"` //是否为主键
  104. Expression string `json:"expression" gorm:"type:varchar(526)"` //计算规则
  105. Scenarios Scenarios `json:"scenarios" gorm:"type:varchar(120)"` //场景
  106. Rule Rule `json:"rule" gorm:"type:varchar(2048)"` //字段规则
  107. Attribute Attribute `json:"attribute" gorm:"type:varchar(4096)"` //字段属性
  108. Position int `json:"position"` //字段排序位置
  109. }
  110. )
  111. func (n Scenarios) Has(str string) bool {
  112. for _, v := range n {
  113. if v == str {
  114. return true
  115. }
  116. }
  117. return false
  118. }
  119. // Scan implements the Scanner interface.
  120. func (n *Scenarios) Scan(value interface{}) error {
  121. if value == nil {
  122. return nil
  123. }
  124. switch s := value.(type) {
  125. case string:
  126. return json.Unmarshal([]byte(s), n)
  127. case []byte:
  128. return json.Unmarshal(s, n)
  129. }
  130. return errDbTypeUnsupported
  131. }
  132. // Value implements the driver Valuer interface.
  133. func (n Scenarios) Value() (driver.Value, error) {
  134. return json.Marshal(n)
  135. }
  136. // Scan implements the Scanner interface.
  137. func (n *Attribute) Scan(value interface{}) error {
  138. if value == nil {
  139. return nil
  140. }
  141. switch s := value.(type) {
  142. case string:
  143. return json.Unmarshal([]byte(s), n)
  144. case []byte:
  145. return json.Unmarshal(s, n)
  146. }
  147. return errDbTypeUnsupported
  148. }
  149. // Value implements the driver Valuer interface.
  150. func (n Attribute) Value() (driver.Value, error) {
  151. return json.Marshal(n)
  152. }
  153. // Scan implements the Scanner interface.
  154. func (n *Rule) Scan(value interface{}) error {
  155. if value == nil {
  156. return nil
  157. }
  158. switch s := value.(type) {
  159. case string:
  160. return json.Unmarshal([]byte(s), n)
  161. case []byte:
  162. return json.Unmarshal(s, n)
  163. }
  164. return errDbTypeUnsupported
  165. }
  166. // Value implements the driver Valuer interface.
  167. func (n Rule) Value() (driver.Value, error) {
  168. return json.Marshal(n)
  169. }