funcmap.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 urlValues url.Values
  107. urlValues = make(url.Values)
  108. urlValues.Set("format", "html")
  109. for i := 0; i < len(values); i += 2 {
  110. urlValues.Add(values[i], values[i+1])
  111. }
  112. return template.URL(urlValues.Encode())
  113. }
  114. func convertDate(value interface{}) string {
  115. t, ok := value.(time.Time)
  116. if !ok {
  117. return ""
  118. }
  119. return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
  120. }
  121. func modelPath(model string, action string, id uint) string {
  122. var q template.URL
  123. action = strings.ToLower(action)
  124. plural := inflection.Plural(strings.ToLower(model))
  125. if action != "" {
  126. switch action {
  127. case "show":
  128. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  129. return fmt.Sprintf("/%s/%d?%s", plural, id, q)
  130. case "update":
  131. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
  132. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  133. case "create":
  134. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
  135. return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
  136. case "delete":
  137. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  138. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  139. }
  140. }
  141. q = query("tpl_layout", "base", "tpl_content", plural)
  142. return fmt.Sprintf("/%s?%s", plural, q)
  143. }
  144. func all(model string) string {
  145. plural := inflection.Plural(strings.ToLower(model))
  146. q := query("tpl_layout", "base", "tpl_content", plural)
  147. return fmt.Sprintf("/%s?%s", plural, q)
  148. }
  149. func create(model string) string {
  150. action := "create"
  151. plural := inflection.Plural(strings.ToLower(model))
  152. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
  153. return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
  154. }
  155. func show(model string, id uint) string {
  156. action := "show"
  157. plural := inflection.Plural(strings.ToLower(model))
  158. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  159. return fmt.Sprintf("/%s/%d?%s", plural, id, q)
  160. }
  161. func execute(model string, id uint) string {
  162. action := "execute"
  163. plural := inflection.Plural(strings.ToLower(model))
  164. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  165. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  166. }
  167. func update(model string, id uint) string {
  168. action := "update"
  169. plural := inflection.Plural(strings.ToLower(model))
  170. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
  171. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  172. }
  173. func delete(model string, id uint) string {
  174. action := "delete"
  175. plural := inflection.Plural(strings.ToLower(model))
  176. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  177. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  178. }