funcmap.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(value interface{}) string {
  114. t, ok := value.(time.Time)
  115. if !ok {
  116. return ""
  117. }
  118. return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
  119. }
  120. func modelPath(model string, action string, id uint) string {
  121. var q template.URL
  122. action = strings.ToLower(action)
  123. plural := inflection.Plural(strings.ToLower(model))
  124. if action != "" {
  125. switch action {
  126. case "show":
  127. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  128. return fmt.Sprintf("/%s/%d?%s", plural, id, q)
  129. case "update":
  130. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
  131. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  132. case "create":
  133. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
  134. return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
  135. case "delete":
  136. q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  137. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  138. }
  139. }
  140. q = query("tpl_layout", "base", "tpl_content", plural)
  141. return fmt.Sprintf("/%s?%s", plural, q)
  142. }
  143. func all(model string) string {
  144. plural := inflection.Plural(strings.ToLower(model))
  145. q := query("tpl_layout", "base", "tpl_content", plural)
  146. return fmt.Sprintf("/%s?%s", plural, q)
  147. }
  148. func create(model string) string {
  149. action := "create"
  150. plural := inflection.Plural(strings.ToLower(model))
  151. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
  152. return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
  153. }
  154. func show(model string, id uint) string {
  155. action := "show"
  156. plural := inflection.Plural(strings.ToLower(model))
  157. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  158. return fmt.Sprintf("/%s/%d?%s", plural, id, q)
  159. }
  160. func update(model string, id uint) string {
  161. action := "update"
  162. plural := inflection.Plural(strings.ToLower(model))
  163. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
  164. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  165. }
  166. func delete(model string, id uint) string {
  167. action := "delete"
  168. plural := inflection.Plural(strings.ToLower(model))
  169. q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
  170. return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
  171. }