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")
	tmplFromWithDelims := func(filename string, delim [2]string) (*template.Template, error) {
		if delim[0] == "" || delim[1] == "" {
			delim = [2]string{"{{", "}}"}
		}
		return template.New(filename).Delims(delim[0], delim[1]).ParseFiles(filepath.Join(tmplDir, filename))
	}
	viewType = optimizeFieldView(field, viewType)
	switch viewType {
	case "checkbox":
		tmpl, err = tmplFromWithDelims("gen-checkbox.tmpl", [2]string{})
	case "custom":
		tmpl, err = tmplFromWithDelims("gen-custom.tmpl", [2]string{})
	case "file":
		tmpl, err = tmplFromWithDelims("gen-file.tmpl", [2]string{})
	case "hidden":
		tmpl, err = tmplFromWithDelims("gen-hidden.tmpl", [2]string{})
	case "input", "text":
		tmpl, err = tmplFromWithDelims("gen-input.tmpl", [2]string{})
	case "richtext":
		tmpl, err = tmplFromWithDelims("gen-richtext.tmpl", [2]string{})
	case "select":
		tmpl, err = tmplFromWithDelims("gen-select.tmpl", [2]string{})
	case "textarea":
		tmpl, err = tmplFromWithDelims("gen-textarea.tmpl", [2]string{})
	case "tags":
		tmpl, err = tmplFromWithDelims("gen-tags.tmpl", [2]string{})
	case "input-repeater":
		tmpl, err = tmplFromWithDelims("gen-input-repeater.tmpl", [2]string{})
	case "select-repeater":
		tmpl, err = tmplFromWithDelims("gen-select-repeater.tmpl", [2]string{})
	case "file-repeater":
		tmpl, err = tmplFromWithDelims("gen-file-repeater.tmpl", [2]string{})
	case "reference":
		tmpl, err = tmplFromWithDelims("gen-reference.tmpl", [2]string{"[[", "]]"})
		if err != nil {
			return err
		}
	case "reference-repeater":
		tmpl, err = tmplFromWithDelims("gen-reference-repeater.tmpl", [2]string{"[[", "]]"})
		if err != nil {
			return err
		}
	default:
		msg := fmt.Sprintf("'%s' is not a recognized view type. Using 'input' instead.", viewType)
		fmt.Println(msg)
		tmpl, err = tmplFromWithDelims("gen-input.tmpl", [2]string{})
	}
	if err != nil {
		return err
	}
	err = tmpl.Execute(buf, field)
	if err != nil {
		return err
	}
	field.View = buf.String()
	return nil
}
set the specified view inside the editor field for a generated field for a type
package main
import (
	"bytes"
	"fmt"
	"go/format"
	"os"
	"path/filepath"
	"strings"
	"text/template"
	"github.com/spf13/cobra"
)
type generateType struct {
	Name          string
	Initial       string
	Fields        []generateField
	HasReferences bool
}
type generateField struct {
	Name     string
	Initial  string
	TypeName string
	JSONName string
	View     string
	IsReference       bool
	ReferenceName     string
	ReferenceJSONTags []string
}
var reservedFieldNames = map[string]string{
	"uuid":      "UUID",
	"item":      "Item",
	"id":        "ID",
	"slug":      "Slug",
	"timestamp": "Timestamp",
	"updated":   "Updated",
}
func legalFieldNames(fields ...generateField) (bool, map[string]string) {
	conflicts := make(map[string]string)
	for _, field := range fields {
		for jsonName, fieldName := range reservedFieldNames {
			if field.JSONName == jsonName || field.Name == fieldName {
				conflicts[jsonName] = fieldName
			}
		}
	}
	if len(conflicts) > 0 {
		return false, conflicts
	}
	return true, conflicts
}
func parseType(args []string) (generateType, error) {
	t := generateType{
		Name: fieldName(args[0]),
	}
	t.Initial = strings.ToLower(string(t.Name[0]))
	fields := args[1:]
	for _, field := range fields {
		f, err := parseField(field, &t)
		if err != nil {
			return generateType{}, err
		}
		f.Initial = t.Initial
		t.Fields = append(t.Fields, f)
	}
	if ok, nameConflicts := legalFieldNames(t.Fields...); !ok {
		for jsonName, fieldName := range nameConflicts {
			fmt.Println(fmt.Sprintf("reserved field name: %s (%s)", jsonName, fieldName))
		}
		count := len(nameConflicts)
		var c = "conflict"
		if count > 1 {
			c = "conflicts"
		}
		return generateType{}, fmt.Errorf("You have (%d) naming %s - please rename and try again", count, c)
	}
	return t, nil
}
func parseField(raw string, gt *generateType) (generateField, error) {
	if !strings.Contains(raw, ":") {
		return generateField{}, fmt.Errorf("Invalid generate argument. [%s]", raw)
	}
	data := strings.Split(raw, ":")
	field := generateField{
		Name:     fieldName(data[0]),
		Initial:  gt.Initial,
		JSONName: fieldJSONName(data[0]),
	}
	setFieldTypeName(&field, data[1], gt)
	viewType := "input"
	if len(data) == 3 {
		viewType = data[2]
	}
	err := setFieldView(&field, viewType)
	if err != nil {
		return generateField{}, err
	}
	return field, nil
}
func setFieldTypeName(field *generateField, fieldType string, gt *generateType) {
	if !strings.Contains(fieldType, "@") {
		field.TypeName = strings.ToLower(fieldType)
		field.IsReference = false
		return
	}
	if strings.Contains(fieldType, ",") {
		referenceConf := strings.Split(fieldType, ",")
		fieldType = referenceConf[0]
		field.ReferenceJSONTags = referenceConf[1:]
	}
	var referenceType string
	if strings.HasPrefix(fieldType, "[]") {
		referenceType = strings.TrimPrefix(fieldType, "[]@")
		fieldType = "[]string"
	} else {
		referenceType = strings.TrimPrefix(fieldType, "@")
		fieldType = "string"
	}
	field.TypeName = strings.ToLower(fieldType)
	field.ReferenceName = fieldName(referenceType)
	field.IsReference = true
	gt.HasReferences = true
	return
}
func fieldName(name string) string {
	if name[0] == '-' || name[0] == '_' {
		name = name[1:]
	}
	if name[len(name)-1] == '-' || name[len(name)-1] == '_' {
		name = name[:len(name)-1]
	}
	name = strings.ToUpper(string(name[0])) + name[1:]
	for i := 0; i < len(name); i++ {
		r := rune(name[i])
		if isUnderscore(r) || isHyphen(r) {
			up := strings.ToUpper(string(name[i+1]))
			name = name[:i] + up + name[i+2:]
		}
	}
	return name
}
func fieldJSONName(name string) string {
	if name[0] == '-' || name[0] == '_' {
		name = name[1:]
	}
	name = strings.ToLower(string(name[0])) + name[1:]
	for i := 0; i < len(name); i++ {
		r := rune(name[i])
		if isUpper(r) {
			low := strings.ToLower(string(r))
			if name[i-1] == '_' || name[i-1] == '-' {
				name = name[:i] + low + name[i+1:]
			} else {
				name = name[:i] + "_" + low + name[i+1:]
			}
		}
	}
	return name
}
func optimizeFieldView(field *generateField, viewType string) string {
	viewType = strings.ToLower(viewType)
	if field.IsReference {
		viewType = "reference"
	}
	repeaterElements := []string{"input", "select", "file", "reference"}
	if strings.HasPrefix(field.TypeName, "[]") {
		for _, el := range repeaterElements {
			if viewType == el {
				viewType = viewType + "-repeater"
			}
		}
	} else {
		if strings.HasSuffix(viewType, "-repeater") {
			field.TypeName = "[]" + field.TypeName
		}
	}
	return viewType
}