funcmap.go 4.2 KB

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