summaryrefslogtreecommitdiff
path: root/cmd/generate/templates.go.tmpl
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-04-10 19:06:03 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-04-10 19:06:03 +0200
commitb9bf8a23c75db82e1aff8295a97dcfdf789735f3 (patch)
treeb6c57cb6747e9a1d56f243dd07f64cd15f1a9541 /cmd/generate/templates.go.tmpl
parent2778c52e4da52fd33f2df7fc9024252c2470b172 (diff)
downloadhh-b9bf8a23c75db82e1aff8295a97dcfdf789735f3.tar.gz
target is a module, not file; support path params; add function to mount all routes
Diffstat (limited to 'cmd/generate/templates.go.tmpl')
-rw-r--r--cmd/generate/templates.go.tmpl47
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