Making generated code worky better

but straying from the TDD path ... which is sad...
This commit is contained in:
Dan Buch 2012-12-25 00:06:55 -05:00
parent 0bb83f358d
commit c8b4612c2e
2 changed files with 48 additions and 21 deletions

View File

@ -17,30 +17,53 @@ const (
var ( var (
simplateGenFileTmpl = template.Must(template.New("smpltgen").Parse(strings.Replace(` simplateGenFileTmpl = template.Must(template.New("smpltgen").Parse(strings.Replace(`
/* GENERATED FILE - DO NOT EDIT */ /* GENERATED FILE - DO NOT EDIT */
/* Rebuild with simplate filesystem parsing thingy! */ /* Rebuild with simplate filesystem parsing thingy! */
package smpltgen package smpltgen
import ( import (
"text/template" "bytes"
) "net/http"
"text/template"
{{.InitPage.Body}} "github.com/meatballhat/box-o-sand/gotime/smplt"
)
const ( {{.InitPage.Body}}
SIMPLATE_TMPL_{{.ConstName}} = __BACKTICK__{{.TemplatePage.Body}}__BACKTICK__
)
var ( {{if .HasTemplatePage}}
simplateTmpl{{.FuncName}} = template.Must(template.New("{{.FuncName}}").Parse(SIMPLATE_TMPL_{{.ConstName}})) const (
) SIMPLATE_TMPL_{{.ConstName}} = __BACKTICK__{{.TemplatePage.Body}}__BACKTICK__
)
func SimplateHandlerFunc{{.FuncName}}(w http.ResponseWriter, req *http.Request) { var (
{{range .LogicPages}} simplateTmpl{{.FuncName}} = template.Must(template.New("{{.FuncName}}").Parse(SIMPLATE_TMPL_{{.ConstName}}))
)
{{end}}
func SimplateHandlerFunc{{.FuncName}}(w http.ResponseWriter, req *http.Request) {
var err error
ctx := make(map[string]interface{})
{{range .LogicPages}}
{{.Body}} {{.Body}}
{{end}} {{end}}
}
{{if .HasTemplatePage}}
var tmplBuf bytes.Buffer
err = simplateTmpl{{.FuncName}}.Execute(&tmplBuf, ctx)
if err != nil {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusInternalServerError)
w.Write(smplt.HTTP_500_RESPONSE)
return
}
w.Header().Set("Content-Type", "{{.ContentType}}")
w.WriteHeader(http.StatusOK)
w.Write(tmplBuf.Bytes())
{{end}}
}
`, "__BACKTICK__", "`", -1))) `, "__BACKTICK__", "`", -1)))
) )
@ -131,3 +154,7 @@ func (me *Simplate) ConstName() string {
uppered := strings.ToUpper(escaped) uppered := strings.ToUpper(escaped)
return strings.Replace(uppered, "-", "_", -1) return strings.Replace(uppered, "-", "_", -1)
} }
func (me *Simplate) HasTemplatePage() bool {
return len(me.TemplatePage.Body) > 0
}

View File

@ -25,7 +25,7 @@ type Dance struct {
When time.Time When time.Time
} }
D := &Dance{ ctx["D"] = &Dance{
Who: "Everybody", Who: "Everybody",
When: time.Now(), When: time.Now(),
} }
@ -45,11 +45,11 @@ type Dance struct {
When time.Time When time.Time
} }
D := &Dance{ ctx["D"] = &Dance{
Who: "Everybody", Who: "Everybody",
When: time.Now(), When: time.Now(),
response.SetBody(ctx["D"])
} }
response.SetBody(D)
` `
BASIC_NEGOTIATED_SIMPLATE = ` BASIC_NEGOTIATED_SIMPLATE = `
import ( import (
@ -61,7 +61,7 @@ type Dance struct {
When time.Time When time.Time
} }
D := &Dance{ ctx["D"] = &Dance{
Who: "Everybody", Who: "Everybody",
When: time.Now(), When: time.Now(),
} }