summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2017-04-01 11:01:42 -0700
committerSteve Manuel <nilslice@gmail.com>2017-04-01 11:01:42 -0700
commit3b91d4be44b7e7814494a0a58e6491d4c5983dbe (patch)
tree88e176ba9019630671eaecf218b365b496337b85
parentc78e6d7adec86a7799f9143f021784635f7665a2 (diff)
adding initial cases to generate input types for field views from command line
-rw-r--r--cmd/ponzu/generate.go75
-rw-r--r--cmd/ponzu/templates/gen-content.tmpl (renamed from cmd/ponzu/contentType.tmpl)8
-rw-r--r--cmd/ponzu/templates/gen-input.tmpl5
3 files changed, 76 insertions, 12 deletions
diff --git a/cmd/ponzu/generate.go b/cmd/ponzu/generate.go
index e98e3fe..8c4bab4 100644
--- a/cmd/ponzu/generate.go
+++ b/cmd/ponzu/generate.go
@@ -18,10 +18,16 @@ type generateType struct {
type generateField struct {
Name string
+ Initial string
TypeName string
JSONName string
+ View string
}
+// following the type, an optional third param could designate the editor.Input-like
+// func to call which would output different text based on the element returned
+// blog title:string Author:string PostCategory:string content:string:richtext some_thing:int
+
// blog title:string Author:string PostCategory:string content:string some_thing:int
func parseType(args []string) (generateType, error) {
t := generateType{
@@ -35,6 +41,10 @@ func parseType(args []string) (generateType, error) {
if err != nil {
return generateType{}, err
}
+ // NEW
+ // set initial (1st character of the type's name) on field so we don't need
+ // to set the template variable like was done in prior version
+ f.Initial = t.Initial
t.Fields = append(t.Fields, f)
}
@@ -43,16 +53,26 @@ func parseType(args []string) (generateType, error) {
}
func parseField(raw string) (generateField, error) {
- // title:string
+ // contents:string or // contents:string:richtext
if !strings.Contains(raw, ":") {
return generateField{}, fmt.Errorf("Invalid generate argument. [%s]", raw)
}
- pair := strings.Split(raw, ":")
+ data := strings.Split(raw, ":")
+ view := `nil`
+
field := generateField{
- Name: fieldName(pair[0]),
- TypeName: strings.ToLower(pair[1]),
- JSONName: fieldJSONName(pair[0]),
+ Name: fieldName(data[0]),
+ TypeName: strings.ToLower(data[1]),
+ JSONName: fieldJSONName(data[0]),
+ View: view,
+ }
+
+ if len(data) == 3 {
+ err := setFieldView(&field, data[2])
+ if err != nil {
+ return generateField{}, err
+ }
}
return field, nil
@@ -116,6 +136,49 @@ func fieldJSONName(name string) string {
return name
}
+// set the specified view inside the editor field for a generated field for a type
+func setFieldView(field *generateField, viewType string) error {
+ var err error
+ var tmpl *template.Template
+ buf := &bytes.Buffer{}
+
+ pwd, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+
+ tmplDir := filepath.Join(pwd, "cmd", "ponzu", "templates")
+ tmplFrom := func(filename string) (*template.Template, error) {
+ return template.ParseFiles(filepath.Join(tmplDir, filename))
+ }
+
+ switch strings.ToLower(viewType) {
+ case "textarea":
+ case "richtext":
+ case "select":
+ case "input":
+ tmpl, err = tmplFrom("gen-input.tmpl")
+ case "checkbox":
+ case "file":
+ case "tags":
+ default:
+ tmpl, err = tmplFrom("gen-input.tmpl")
+ }
+
+ if err != nil {
+ return err
+ }
+
+ err = tmpl.Execute(buf, field)
+ if err != nil {
+ return err
+ }
+
+ field.View = buf.String()
+
+ return nil
+}
+
func isUpper(char rune) bool {
if char >= 'A' && char <= 'Z' {
return true
@@ -163,7 +226,7 @@ func generateContentType(args []string) error {
return fmt.Errorf("Failed to parse type args: %s", err.Error())
}
- tmplPath := filepath.Join(pwd, "cmd", "ponzu", "contentType.tmpl")
+ tmplPath := filepath.Join(pwd, "cmd", "ponzu", "templates", "gen-content.tmpl")
tmpl, err := template.ParseFiles(tmplPath)
if err != nil {
return fmt.Errorf("Failed to parse template: %s", err.Error())
diff --git a/cmd/ponzu/contentType.tmpl b/cmd/ponzu/templates/gen-content.tmpl
index c16cfd6..2d92b88 100644
--- a/cmd/ponzu/contentType.tmpl
+++ b/cmd/ponzu/templates/gen-content.tmpl
@@ -20,13 +20,9 @@ func ({{ .Initial }} *{{ .Name }}) MarshalEditor() ([]byte, error) {
view, err := editor.Form({{ .Initial }},
// Take note that the first argument to these Input-like functions
// is the string version of each {{ .Name }} field, and must follow
- // this pattern for auto-decoding and auto-encoding reasons:{{ $initial := .Initial }}
+ // this pattern for auto-decoding and auto-encoding reasons:
{{ range .Fields }}editor.Field{
- View: editor.Input("{{ .Name }}", {{ $initial }}, map[string]string{
- "label": "{{ .Name }}",
- "type": "text",
- "placeholder": "Enter the {{ .Name }} here",
- }),
+ {{ .View }}
},
{{ end }}
)
diff --git a/cmd/ponzu/templates/gen-input.tmpl b/cmd/ponzu/templates/gen-input.tmpl
new file mode 100644
index 0000000..8bea12a
--- /dev/null
+++ b/cmd/ponzu/templates/gen-input.tmpl
@@ -0,0 +1,5 @@
+View: editor.Input("{{ .Name }}", {{ .Initial }}, map[string]string{
+ "label": "{{ .Name }}",
+ "type": "text",
+ "placeholder": "Enter the {{ .Name }} here",
+}), \ No newline at end of file