|
@@ -1,6 +1,7 @@
|
|
package renderer
|
|
package renderer
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "errors"
|
|
"fmt"
|
|
"fmt"
|
|
"html/template"
|
|
"html/template"
|
|
"net/url"
|
|
"net/url"
|
|
@@ -9,6 +10,7 @@ import (
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/jinzhu/inflection"
|
|
"github.com/jinzhu/inflection"
|
|
|
|
+ yml "gopkg.in/yaml.v2"
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -16,10 +18,36 @@ var (
|
|
"query": query,
|
|
"query": query,
|
|
"convertDate": convertDate,
|
|
"convertDate": convertDate,
|
|
"modelPath": modelPath,
|
|
"modelPath": modelPath,
|
|
- "slice": slice,
|
|
|
|
|
|
+ "dict": dict,
|
|
|
|
+ "yaml": yaml,
|
|
|
|
+ "create": create,
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+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 {
|
|
func slice(values ...string) []string {
|
|
var result []string
|
|
var result []string
|
|
result = append(result, values...)
|
|
result = append(result, values...)
|
|
@@ -76,3 +104,19 @@ func modelPath(model string, action string, id uint) string {
|
|
q = query("tpl_layout", "base", "tpl_content", plural)
|
|
q = query("tpl_layout", "base", "tpl_content", plural)
|
|
return fmt.Sprintf("/%s?%s", plural, q)
|
|
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)
|
|
|
|
+}
|