funcmap.go 3.0 KB

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