formily.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package config
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "time"
  7. "m7s.live/engine/v4/log"
  8. )
  9. type Property struct {
  10. Type string `json:"type"`
  11. Title string `json:"title"`
  12. Description string `json:"description"`
  13. Enum []struct {
  14. Label string `json:"label"`
  15. Value any `json:"value"`
  16. } `json:"enum,omitempty"`
  17. Items *Object `json:"items,omitempty"`
  18. Properties map[string]any `json:"properties,omitempty"`
  19. Default any `json:"default,omitempty"`
  20. Decorator string `json:"x-decorator"`
  21. DecoratorProps map[string]any `json:"x-decorator-props,omitempty"`
  22. Component string `json:"x-component"`
  23. ComponentProps map[string]any `json:"x-component-props,omitempty"`
  24. Index int `json:"x-index"`
  25. }
  26. type Card struct {
  27. Type string `json:"type"`
  28. Properties map[string]any `json:"properties,omitempty"`
  29. Component string `json:"x-component"`
  30. ComponentProps map[string]any `json:"x-component-props,omitempty"`
  31. Index int `json:"x-index"`
  32. }
  33. type Object struct {
  34. Type string `json:"type"`
  35. Properties map[string]any `json:"properties"`
  36. }
  37. func (config *Config) schema(index int) (r any) {
  38. defer func() {
  39. err := recover()
  40. if err != nil {
  41. log.Error(err)
  42. }
  43. }()
  44. if config.props != nil {
  45. r := Card{
  46. Type: "void",
  47. Component: "Card",
  48. Properties: make(map[string]any),
  49. Index: index,
  50. }
  51. r.ComponentProps = map[string]any{
  52. "title": config.name,
  53. }
  54. for i, v := range config.props {
  55. if strings.HasPrefix(v.tag.Get("desc"), "废弃") {
  56. continue
  57. }
  58. r.Properties[v.name] = v.schema(i)
  59. }
  60. return r
  61. } else {
  62. p := Property{
  63. Title: config.name,
  64. Default: config.GetValue(),
  65. DecoratorProps: map[string]any{
  66. "tooltip": config.tag.Get("desc"),
  67. },
  68. ComponentProps: map[string]any{},
  69. Decorator: "FormItem",
  70. Index: index,
  71. }
  72. if config.Modify != nil {
  73. p.Description = "已动态修改"
  74. } else if config.Env != nil {
  75. p.Description = "使用环境变量中的值"
  76. } else if config.File != nil {
  77. p.Description = "使用配置文件中的值"
  78. } else if config.Global != nil {
  79. p.Description = "已使用全局配置中的值"
  80. }
  81. p.Enum = config.Enum
  82. switch config.Ptr.Type() {
  83. case regexpType:
  84. p.Type = "string"
  85. p.Component = "Input"
  86. p.DecoratorProps["addonAfter"] = "正则表达式"
  87. str := config.GetValue().(Regexp).String()
  88. p.ComponentProps = map[string]any{
  89. "placeholder": str,
  90. }
  91. p.Default = str
  92. case durationType:
  93. p.Type = "string"
  94. p.Component = "Input"
  95. str := config.GetValue().(time.Duration).String()
  96. p.ComponentProps = map[string]any{
  97. "placeholder": str,
  98. }
  99. p.Default = str
  100. p.DecoratorProps["addonAfter"] = "时间,单位:s,m,h,d,例如:100ms, 10s, 4m, 1h"
  101. default:
  102. switch config.Ptr.Kind() {
  103. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
  104. p.Type = "number"
  105. p.Component = "InputNumber"
  106. p.ComponentProps = map[string]any{
  107. "placeholder": fmt.Sprintf("%v", config.GetValue()),
  108. }
  109. case reflect.Bool:
  110. p.Type = "boolean"
  111. p.Component = "Switch"
  112. case reflect.String:
  113. p.Type = "string"
  114. p.Component = "Input"
  115. p.ComponentProps = map[string]any{
  116. "placeholder": config.GetValue(),
  117. }
  118. case reflect.Slice:
  119. p.Type = "array"
  120. p.Component = "Input"
  121. p.ComponentProps = map[string]any{
  122. "placeholder": config.GetValue(),
  123. }
  124. p.DecoratorProps["addonAfter"] = "数组,每个元素用逗号分隔"
  125. case reflect.Map:
  126. var children []struct {
  127. Key string `json:"mkey"`
  128. Value any `json:"mvalue"`
  129. }
  130. p := Property{
  131. Type: "array",
  132. Component: "ArrayTable",
  133. Decorator: "FormItem",
  134. Properties: map[string]any{
  135. "addition": map[string]string{
  136. "type": "void",
  137. "title": "添加",
  138. "x-component": "ArrayTable.Addition",
  139. },
  140. },
  141. Index: index,
  142. Title: config.name,
  143. Items: &Object{
  144. Type: "object",
  145. Properties: map[string]any{
  146. "c1": Card{
  147. Type: "void",
  148. Component: "ArrayTable.Column",
  149. ComponentProps: map[string]any{
  150. "title": config.tag.Get("key"),
  151. "width": 300,
  152. },
  153. Properties: map[string]any{
  154. "mkey": Property{
  155. Type: "string",
  156. Decorator: "FormItem",
  157. Component: "Input",
  158. },
  159. },
  160. Index: 0,
  161. },
  162. "c2": Card{
  163. Type: "void",
  164. Component: "ArrayTable.Column",
  165. ComponentProps: map[string]any{
  166. "title": config.tag.Get("value"),
  167. },
  168. Properties: map[string]any{
  169. "mvalue": Property{
  170. Type: "string",
  171. Decorator: "FormItem",
  172. Component: "Input",
  173. },
  174. },
  175. Index: 1,
  176. },
  177. "operator": Card{
  178. Type: "void",
  179. Component: "ArrayTable.Column",
  180. ComponentProps: map[string]any{
  181. "title": "操作",
  182. },
  183. Properties: map[string]any{
  184. "remove": Card{
  185. Type: "void",
  186. Component: "ArrayTable.Remove",
  187. },
  188. },
  189. Index: 2,
  190. },
  191. },
  192. },
  193. }
  194. iter := config.Ptr.MapRange()
  195. for iter.Next() {
  196. children = append(children, struct {
  197. Key string `json:"mkey"`
  198. Value any `json:"mvalue"`
  199. }{
  200. Key: iter.Key().String(),
  201. Value: iter.Value().Interface(),
  202. })
  203. }
  204. p.Default = children
  205. return p
  206. default:
  207. }
  208. }
  209. if len(p.Enum) > 0 {
  210. p.Component = "Radio.Group"
  211. }
  212. return p
  213. }
  214. }
  215. func (config *Config) GetFormily() (r Object) {
  216. var fromItems = make(map[string]any)
  217. r.Type = "object"
  218. r.Properties = map[string]any{
  219. "layout": Card{
  220. Type: "void",
  221. Component: "FormLayout",
  222. ComponentProps: map[string]any{
  223. "labelCol": 4,
  224. "wrapperCol": 20,
  225. },
  226. Properties: fromItems,
  227. },
  228. }
  229. for i, v := range config.props {
  230. if strings.HasPrefix(v.tag.Get("desc"), "废弃") {
  231. continue
  232. }
  233. fromItems[v.name] = v.schema(i)
  234. }
  235. return
  236. }