diff options
| author | Mathias Magnusson <mathias@magnusson.space> | 2026-01-24 00:33:11 +0100 |
|---|---|---|
| committer | Mathias Magnusson <mathias@magnusson.space> | 2026-01-24 00:33:11 +0100 |
| commit | d75bab2ba79d138547a0ea6d3b3be84a89b050a8 (patch) | |
| tree | d00eef48f0332380f66578e761f9d27611f53b66 | |
| parent | 8f29959b87b68e76aadbc32dfa00f938604b1f6d (diff) | |
| download | hh-d75bab2ba79d138547a0ea6d3b3be84a89b050a8.tar.gz | |
take `http.ResponseWriter` as part of parsed request
| -rw-r--r-- | cmd/generate/main.go | 7 | ||||
| -rw-r--r-- | cmd/generate/templates.go.tmpl | 6 | ||||
| -rw-r--r-- | examples/basic.go | 5 |
3 files changed, 13 insertions, 5 deletions
diff --git a/cmd/generate/main.go b/cmd/generate/main.go index feed0f0..eddd4cb 100644 --- a/cmd/generate/main.go +++ b/cmd/generate/main.go @@ -123,7 +123,10 @@ func run() error { if pattern, ok = strings.CutPrefix(hhRoute, "//hh:route "); !ok { continue } - parsedRequestType, ok := f.Type.Params.List[1].Type.(*ast.StructType) + if len(f.Type.Params.List) != 1 { + return errors.New("Handler must take exactly one parameter") + } + parsedRequestType, ok := f.Type.Params.List[0].Type.(*ast.StructType) if !ok { return errors.New("Parsed request type must be a struct") } @@ -165,7 +168,7 @@ func run() error { winner, ) } - if parsedField.TypeDef == "*http.Request" { + if parsedField.TypeDef == "*http.Request" || parsedField.TypeDef == "http.ResponseWriter" { parsedFunction.RequestTypeFields = append(parsedFunction.RequestTypeFields, parsedField) continue } diff --git a/cmd/generate/templates.go.tmpl b/cmd/generate/templates.go.tmpl index ff63337..77e8b7d 100644 --- a/cmd/generate/templates.go.tmpl +++ b/cmd/generate/templates.go.tmpl @@ -47,6 +47,10 @@ func hh_{{ $fn.Name }}(w http.ResponseWriter, r *http.Request) { parsed.{{ $f.Name }} = r {{ continue }} {{ end }} + {{ if eq $f.TypeDef "http.ResponseWriter" }} + parsed.{{ $f.Name }} = w + {{ continue }} + {{ end }} {{ $f.Name }}, {{ $f.Name }}Skipped := {{ extractorName $f.Extractor }}(r, {{ $f.NameInReq | quote }}) {{ if not $f.Optional }} if {{ $f.Name }}Skipped { @@ -62,7 +66,7 @@ func hh_{{ $fn.Name }}(w http.ResponseWriter, r *http.Request) { } } {{ end }} - {{ $fn.Name }}(w, parsed) + {{ $fn.Name }}(parsed) {{ if false }} {{ range $_, $f := $fn.RequestTypeFields -}} {{ $f.Name }}: {{ $f.Name }}, diff --git a/examples/basic.go b/examples/basic.go index 7f09d0f..d8ad6d7 100644 --- a/examples/basic.go +++ b/examples/basic.go @@ -12,7 +12,8 @@ import ( // Big bungus function here! // //hh:route GET /org/{orgID}/users -func adminUsersForm(w http.ResponseWriter, r struct { +func adminUsersForm(r struct { + w http.ResponseWriter raw *http.Request search string `hh:"form"` year int `hh:"optional,form"` @@ -21,7 +22,7 @@ func adminUsersForm(w http.ResponseWriter, r struct { banana uuid.UUID `hh:"optional,form"` nextURL string `hh:"optional,cookie,logout_next_url"` }) { - _, _ = w.Write([]byte("ahahaha")) + _, _ = r.w.Write([]byte("ahahaha")) slog.Info("get admin users form", "search", r.search, "offset", r.offset, "next-url", r.nextURL) } |
