Skip to content

Commit

Permalink
feat: add path parameter parser
Browse files Browse the repository at this point in the history
closes: #4
  • Loading branch information
crhntr committed Aug 23, 2024
1 parent 87bda1e commit f01e813
Show file tree
Hide file tree
Showing 5 changed files with 848 additions and 30 deletions.
168 changes: 168 additions & 0 deletions cmd/muxt/testdata/generate/path_param_typed.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
muxt generate --receiver-static-type=T

cat template_routes.go

exec go test -cover

-- template.gohtml --

{{- define "GET /bool/{value} PassBool(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /int/{value} PassInt(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /int16/{value} PassInt16(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /int32/{value} PassInt32(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /int64/{value} PassInt64(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /int8/{value} PassInt8(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /uint/{value} PassUint(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /uint16/{value} PassUint16(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /uint32/{value} PassUint32(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /uint64/{value} PassUint64(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}
{{- define "GET /uint8/{value} PassUint8(value)" -}} <p>{{- printf "%[1]#v %[1]T" . -}}</p> {{- end -}}

-- go.mod --
module server

go 1.22

-- template.go --
package server

import (
"embed"
"html/template"
)

//go:embed *.gohtml
var formHTML embed.FS

var templates = template.Must(template.ParseFS(formHTML, "*"))

type T struct{}

func (T) PassInt(in int) int { return in }
func (T) PassInt64(in int64) int64 { return in }
func (T) PassInt32(in int32) int32 { return in }
func (T) PassInt16(in int16) int16 { return in }
func (T) PassInt8(in int8) int8 { return in }
func (T) PassUint(in uint) uint { return in }
func (T) PassUint64(in uint64) uint64 { return in }
func (T) PassUint32(in uint32) uint32 { return in }
func (T) PassUint16(in uint16) uint16 { return in }
func (T) PassUint8(in uint8) uint8 { return in }
func (T) PassBool(in bool) bool { return in }
func (T) PassByte(in byte) byte { return in }
func (T) PassRune(in rune) rune { return in }

-- template_test.go --
package server

import (
"net/http"
"net/http/httptest"
"testing"
)

func Test(t *testing.T) {
mux := http.NewServeMux()

routes(mux, T{})

t.Run("int", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/int/123", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("int64", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/int64/52", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("int32", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/int32/51", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("int16", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/int16/50", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("int8", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/int8/50", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("uint", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/uint/12", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("uint64", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/uint64/11", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("uint32", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/uint32/11", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("uint16", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/uint16/7", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("uint8", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/uint8/5", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
t.Run("bool", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/bool/true", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}
})
}
Loading

0 comments on commit f01e813

Please sign in to comment.