123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package renderer
- import (
- "errors"
- "fmt"
- "html/template"
- "net/url"
- "reflect"
- "strings"
- "time"
- "github.com/jinzhu/inflection"
- yml "gopkg.in/yaml.v2"
- )
- var (
- funcMap = template.FuncMap{
- "query": query,
- "convertDate": convertDate,
- "modelPath": modelPath,
- "dict": dict,
- "yaml": yaml,
- "create": create,
- "show": show,
- }
- )
- func yaml(content string) (interface{}, error) {
- var result interface{}
- err := yml.Unmarshal([]byte(content), &result)
- if err != nil {
- return nil, err
- }
- return result, nil
- }
- func dict(values ...interface{}) (map[string]interface{}, error) {
- if len(values)%2 != 0 {
- return nil, errors.New("invalid dict call")
- }
- dict := make(map[string]interface{}, len(values)/2)
- for i := 0; i < len(values); i += 2 {
- key, ok := values[i].(string)
- if !ok {
- return nil, errors.New("dict keys must be strings")
- }
- dict[key] = values[i+1]
- }
- return dict, nil
- }
- func slice(values ...string) []string {
- var result []string
- result = append(result, values...)
- return result
- }
- func getType(myvar interface{}) (res string) {
- t := reflect.TypeOf(myvar)
- for t.Kind() == reflect.Ptr {
- t = t.Elem()
- res += "*"
- }
- return res + t.Name()
- }
- func query(values ...string) template.URL {
- var urlValues url.Values
- urlValues = make(url.Values)
- urlValues.Set("format", "html")
- for i := 0; i < len(values); i += 2 {
- urlValues.Add(values[i], values[i+1])
- }
- return template.URL(urlValues.Encode())
- }
- func convertDate(t time.Time) string {
- return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
- }
- func modelPath(model string, action string, id uint) string {
- var q template.URL
- action = strings.ToLower(action)
- plural := inflection.Plural(strings.ToLower(model))
- if action != "" {
- switch action {
- case "show":
- q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
- return fmt.Sprintf("/%s/%d?%s", plural, id, q)
- case "update":
- q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
- return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
- case "create":
- q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
- return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
- case "delete":
- q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
- return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
- }
- }
- q = query("tpl_layout", "base", "tpl_content", plural)
- return fmt.Sprintf("/%s?%s", plural, q)
- }
- func create(model string) string {
- action := "create"
- plural := inflection.Plural(strings.ToLower(model))
- q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
- return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
- }
- func show(model string, id uint) string {
- action := "show"
- plural := inflection.Plural(strings.ToLower(model))
- q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
- return fmt.Sprintf("/%s/%d?%s", plural, id, q)
- }
|