diff options
Diffstat (limited to 'cmd/generate/templates.go.tmpl')
-rw-r--r-- | cmd/generate/templates.go.tmpl | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/cmd/generate/templates.go.tmpl b/cmd/generate/templates.go.tmpl index 9201ebf..0e684e3 100644 --- a/cmd/generate/templates.go.tmpl +++ b/cmd/generate/templates.go.tmpl @@ -1,13 +1,35 @@ +// WARNING: this file has been automatically generated by +// codeberg.org/fooelevator/hh. DO NOT EDIT MANUALLY! + package {{ .PackageName }} +import ( + "net/http" +) + +func hhMountRoutes[S any](s S, mux *http.ServeMux) { + if mux == nil { + mux = http.DefaultServeMux + } + wrapper := func(handler func(s S, w http.ResponseWriter, r *http.Request)) http.Handler { + return http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) { + handler(s, w, r) + }) + } + + {{ range $_, $fn := .Functions -}} + mux.Handle({{ $fn.Pattern | quote }}, wrapper(hh_{{ $fn.Name }})) + {{ end }} +} + {{ range $_, $fn := .Functions }} func hh_{{ $fn.Name }}[S any](s S, w http.ResponseWriter, r *http.Request) { {{- if $fn.DoParseForm }} - if err := r.ParseForm(); err != nil { - panic("todo: Bad request") - } + if err := r.ParseForm(); err != nil { + panic("todo: Bad request") + } {{ end }} - {{ range $_, $f := $fn.RequestTypeFields }} + {{- range $_, $f := $fn.RequestTypeFields }} {{ if eq $f.TypeDef "*http.Request" }} {{ continue }} {{ end }} @@ -18,7 +40,7 @@ func hh_{{ $fn.Name }}[S any](s S, w http.ResponseWriter, r *http.Request) { {{- if eq $f.Extractor "form" }} {{ $f.Name }}1 := r.Form[{{ $f.NameInReq | quote }}] if len({{ $f.Name }}1) != 0 { - {{ $f.Name }} = {{ $f.Name }}1[0] + {{ $f.Name }}0 = {{ $f.Name }}1[0] } else { {{- if not $f.Optional }} panic("todo: Bad request: form value " + {{ $f.NameInReq | quote }} + " missing") @@ -26,10 +48,21 @@ func hh_{{ $fn.Name }}[S any](s S, w http.ResponseWriter, r *http.Request) { {{ $f.Name }}Skipped = true {{- end }} } + {{ else if eq $f.Extractor "path" }} + {{ $f.Name }}1 := r.PathValue({{ $f.NameInReq | quote }}) + if {{ $f.Name }}1 != "" { + {{ $f.Name }}0 = {{ $f.Name }}1 + } else { + {{- if not $f.Optional }} + panic("todo: Bad request: path value " + {{ $f.NameInReq | quote }} + " missing") + {{- else }} + {{ $f.Name }}Skipped = true + {{- end }} + } {{ else if eq $f.Extractor "cookie" }} {{ $f.Name }}1, _ := r.Cookie({{ $f.NameInReq | quote }}) if {{ $f.Name }}1 != nil { - {{ $f.Name }} = {{ $f.Name }}1.Value + {{ $f.Name }}0 = {{ $f.Name }}1.Value } else { {{- if not $f.Optional }} panic("todo: Bad request: cookie " + {{ $f.NameInReq | quote }} + " missing") @@ -38,7 +71,7 @@ func hh_{{ $fn.Name }}[S any](s S, w http.ResponseWriter, r *http.Request) { {{- end }} } {{ else }} - {{ error }} + {{ error "unknown extractor" }} {{ end -}} {{ if eq $f.TypeDef "string" -}} {{ $f.Name }} := {{ $f.Name }}0 |