funcmap.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package renderer
  2. import (
  3. "errors"
  4. "fmt"
  5. "html/template"
  6. "net/url"
  7. "reflect"
  8. "strings"
  9. "time"
  10. "github.com/jinzhu/inflection"
  11. yml "gopkg.in/yaml.v2"
  12. )
  13. var (
  14. funcMap = template.FuncMap{
  15. "query": query,
  16. "convertDate": convertDate,
  17. "modelPath": modelPath,
  18. "dict": dict,
  19. "yaml": yaml,
  20. "create": create,
  21. "update": update,
  22. "delete": delete,
  23. "show": show,
  24. "all": all,
  25. "execute": execute,
  26. "isSlice": isSlice,
  27. "toSlice": toSlice,
  28. "string": callString,
  29. "incr": incr,
  30. "mod2": mod2,
  31. "toLower": toLower,
  32. "anchor": anchor,
  33. "html": html,
  34. "field": field,
  35. }
  36. )
  37. func field(name string, value interface{}) interface{} {
  38. if value != nil {
  39. s := reflect.ValueOf(value).Elem()
  40. return s.FieldByName(name).Interface()
  41. } else {
  42. return nil
  43. }
  44. }
  45. func html(content string) template.HTML {
  46. return template.HTML(content)
  47. }
  48. func anchor(text, url string) template.HTML {
  49. return template.HTML(fmt.Sprintf("<a href=\"%s\">%s</a>", url, text))
  50. }
  51. func toLower(text string) string {
  52. return strings.ToLower(text)
  53. }
  54. func mod2(value int) bool {
  55. return value%2 == 0
  56. }
  57. func incr(value int) int {
  58. return value + 1
  59. }
  60. func callString(value interface{}) string {
  61. if value != nil {
  62. return reflect.ValueOf(value).MethodByName("String").Interface().(func() string)()
  63. } else {
  64. return ""
  65. }
  66. }
  67. func isSlice(value interface{}) bool {
  68. return reflect.TypeOf(value).Kind() == reflect.Slice
  69. }
  70. func yaml(content string) (interface{}, error) {
  71. var result interface{}
  72. err := yml.Unmarshal([]byte(content), &result)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return result, nil
  77. }
  78. func dict(values ...interface{}) (map[string]interface{}, error) {
  79. if len(values)%2 != 0 {
  80. return nil, errors.New("invalid dict call")
  81. }
  82. dict := make(map[string]interface{}, len(values)/2)
  83. for i := 0; i < len(values); i += 2 {
  84. key, ok := values[i].(string)
  85. if !ok {
  86. return nil, errors.New("dict keys must be strings")
  87. }
  88. dict[key] = values[i+1]
  89. }
  90. return dict, nil
  91. }
  92. func toSlice(values ...string) interface{} {
  93. var result []string
  94. result = append(result, values...)
  95. return result
  96. }
  97. func getType(myvar interface{}) (res string) {
  98. t := reflect.TypeOf(myvar)
  99. for t.Kind() == reflect.Ptr {
  100. t = t.Elem()
  101. res += "*"
  102. }
  103. return res + t.Name()
  104. }
  105. func query(values ...string) template.URL {
  106. var (
  107. urlValues url.Values
  108. format bool
  109. )
  110. urlValues = make(url.Values)
  111. for i := 0; i < len(values); i += 2 {
  112. if values[i] == "format" {
  113. format = true
  114. }
  115. urlValues.Add(values[i], values[i+1])
  116. }
  117. if !format {
  118. urlValues.Set("format", "html")
  119. }
  120. return template.URL(urlValues.Encode())
  121. }
  122. func convertDate(value interface{}) string {
  123. t, ok := value.(time.Time)
  124. if !ok {
  125. return ""
  126. }
  127. return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
  128. }
  129. func modelPath(model string, action string, id uint) string {
  130. var q template.URL
  131. action = strings.ToLower(action)
  132. plural := inflection.Plural(strings.ToLower(model))
  133. if action != "" {
  134. switch action {
  135. case "show":
  136. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  137. return fmt.Sprintf("/%s/%d?%s", plural, id, q)
  138. case "update":
  139. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
  140. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  141. case "create":
  142. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
  143. return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
  144. case "delete":
  145. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  146. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  147. }
  148. }
  149. q = query("tpl_layout", "base", "tpl_content", plural)
  150. return fmt.Sprintf("/%s?%s", plural, q)
  151. }
  152. func all(model string) string {
  153. plural := inflection.Plural(strings.ToLower(model))
  154. q := query("tpl_layout", "base", "tpl_content", plural)
  155. return fmt.Sprintf("/%s?%s", plural, q)
  156. }
  157. func create(model string) string {
  158. action := "create"
  159. plural := inflection.Plural(strings.ToLower(model))
  160. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
  161. return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
  162. }
  163. func show(model string, id uint, format ...string) string {
  164. action := "show"
  165. plural := inflection.Plural(strings.ToLower(model))
  166. args := []string{"tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action)}
  167. if len(format) > 0 {
  168. args = append(args, []string{"format", format[0]}...)
  169. }
  170. // q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  171. q := query(args...)
  172. return fmt.Sprintf("/%s/%d?%s", plural, id, q)
  173. }
  174. func execute(model string, id uint) string {
  175. action := "execute"
  176. plural := inflection.Plural(strings.ToLower(model))
  177. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  178. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  179. }
  180. func update(model string, id uint) string {
  181. action := "update"
  182. plural := inflection.Plural(strings.ToLower(model))
  183. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
  184. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  185. }
  186. func delete(model string, id uint) string {
  187. action := "delete"
  188. plural := inflection.Plural(strings.ToLower(model))
  189. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  190. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  191. }