funcmap.go 3.7 KB

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