funcmap.go 3.0 KB

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