funcmap.go 4.8 KB

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