diff --git a/constructor.go b/constructor.go index d45bad2..c0c30e7 100644 --- a/constructor.go +++ b/constructor.go @@ -2,11 +2,15 @@ package httpwrap import "net/http" -// Constructor is the function signature for unmarshalling an http request into +// RequestReader is the function signature for unmarshalling a *http.Request into // an object. -type Constructor func(http.ResponseWriter, *http.Request, any) error +type RequestReader func(http.ResponseWriter, *http.Request, any) error -// emptyConstructor is the default constructor for new wrappers. +// ResponseWriter is the function signature for marshalling a structured response +// into a standard http.ResponseWriter +type ResponseWriter func(w http.ResponseWriter, r *http.Request, res any, err error) + +// emptyRequestReader is the default constructor for new wrappers. // It is a no-op, and will not parse any http request information to construct endpoint // parameter objects. -func emptyConstructor(http.ResponseWriter, *http.Request, any) error { return nil } +func emptyRequestReader(http.ResponseWriter, *http.Request, any) error { return nil } diff --git a/context.go b/context.go index 6902278..59065ad 100644 --- a/context.go +++ b/context.go @@ -8,7 +8,7 @@ import ( type runctx struct { rw http.ResponseWriter req *http.Request - cons Constructor + cons RequestReader response reflect.Value results map[reflect.Type]param @@ -24,7 +24,7 @@ type param struct { func newRunCtx( rw http.ResponseWriter, req *http.Request, - cons Constructor, + cons RequestReader, ) *runctx { ctx := &runctx{ req: req, diff --git a/examples/simple/main.go b/examples/simple/main.go index 05791e5..65b5dcb 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -162,7 +162,7 @@ func main() { mw := &Middlewares{} wrapper := httpwrap.New(). - WithConstruct(httpwrap.StandardRequestReader()). + WithRequestReader(httpwrap.StandardRequestReader()). Before(mw.checkAPICreds). Finally(mw.sendResponse) diff --git a/standard.go b/standard.go index e9aca28..15c2040 100644 --- a/standard.go +++ b/standard.go @@ -12,9 +12,9 @@ import ( // - path params // - headers // - JSON decoding of the body -func StandardRequestReader() Constructor { +func StandardRequestReader() RequestReader { decoder := NewDecoder() - return func(rw http.ResponseWriter, req *http.Request, obj any) error { + return func(_ http.ResponseWriter, req *http.Request, obj any) error { return decoder.Decode(req, obj) } } @@ -25,8 +25,8 @@ func StandardRequestReader() Constructor { // If the HTTPResponse has a `0` StatusCode, WriteHeader will not be called. // If the error is not an HTTPResponse, a 500 status code will be returned with // the body being exactly the error's string. -func StandardResponseWriter() func(w http.ResponseWriter, res any, err error) { - return func(w http.ResponseWriter, res any, err error) { +func StandardResponseWriter() ResponseWriter { + return func(w http.ResponseWriter, _ *http.Request, res any, err error) { if err != nil { if cast, ok := err.(HTTPResponse); ok { code := cast.StatusCode() @@ -74,6 +74,6 @@ func NewStandardWrapper() Wrapper { constructor := StandardRequestReader() responseWriter := StandardResponseWriter() return New(). - WithConstruct(constructor). + WithRequestReader(constructor). Finally(responseWriter) } diff --git a/wrapper.go b/wrapper.go index 65dd5bf..870eedd 100644 --- a/wrapper.go +++ b/wrapper.go @@ -10,19 +10,19 @@ import ( type Wrapper struct { befores []beforeFn after *afterFn - construct Constructor + construct RequestReader } // New creates a new Wrapper object. This wrapper object will not interact in any way // with the http request and response writer. func New() Wrapper { return Wrapper{ - construct: emptyConstructor, + construct: emptyRequestReader, } } -// WithConstruct returns a new wrapper with the given Constructor function. -func (w Wrapper) WithConstruct(cons Constructor) Wrapper { +// WithRequestReader returns a new wrapper with the given RequestReader function. +func (w Wrapper) WithRequestReader(cons RequestReader) Wrapper { w.construct = cons return w } @@ -44,7 +44,7 @@ func (w Wrapper) Before(fns ...any) Wrapper { } // Finally sets the last function that will execute during a request. This function gets -// // invoked with the response object and the possible error returned from the main +// invoked with the response object and the possible error returned from the main // endpoint function. func (w Wrapper) Finally(fn any) Wrapper { after, err := newAfter(fn) diff --git a/wrapper_test.go b/wrapper_test.go index 92b674e..dc0ddeb 100644 --- a/wrapper_test.go +++ b/wrapper_test.go @@ -17,7 +17,7 @@ func TestWrapper(t *testing.T) { type meta struct{} handler := New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Wrap(func(rw http.ResponseWriter, req *http.Request) error { require.NotNil(t, rw) require.Equal(t, "GET", req.Method) @@ -28,7 +28,7 @@ func TestWrapper(t *testing.T) { rw = httptest.NewRecorder() handler = New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Wrap(func(m *meta, m1 map[int]int, m2 []string) error { require.NotNil(t, m) require.NotNil(t, m1) @@ -47,7 +47,7 @@ func TestWrapper(t *testing.T) { type resp struct{ s string } handler := New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Finally(func(res any, err error) { s := fmt.Sprintf("%v", res) require.True(t, strings.Contains(s, "response")) @@ -67,7 +67,7 @@ func TestWrapper(t *testing.T) { type meta struct{ path string } handler := New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Before(func(req *http.Request) meta { require.NotNil(t, req) require.NotNil(t, req.URL) @@ -87,7 +87,7 @@ func TestWrapper(t *testing.T) { type meta struct{ path string } handler := New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Before(func(req *http.Request) (meta, error) { return meta{req.URL.Path}, fmt.Errorf("failed before") }). @@ -113,7 +113,7 @@ func TestWrapper(t *testing.T) { type resp struct{} type meta struct{ path string } handler := New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Before(func(req *http.Request) (meta, error) { require.NotNil(t, req) require.NotNil(t, req.URL) @@ -139,7 +139,7 @@ func TestWrapper(t *testing.T) { type meta struct{} handler := New(). - WithConstruct(failedConstructor). + WithRequestReader(failedConstructor). Before(func(m meta) { require.FailNow(t, "should not get to before") }). @@ -163,7 +163,7 @@ func TestWrapper(t *testing.T) { type extra struct{ Field1 string } type mainArg struct{ Field2 string } handler := New(). - WithConstruct(jsonBodyConstructor). + WithRequestReader(jsonBodyConstructor). Before(func(m meta) extra { require.Equal(t, "metafield", m.Metafield) return extra{"field1"} @@ -189,7 +189,7 @@ func TestWrapper(t *testing.T) { type body string type extra struct{ Field1 string } handler := New(). - WithConstruct(jsonBodyConstructor). + WithRequestReader(jsonBodyConstructor). Before(func(b body) extra { require.Equal(t, "this is JSON", string(b)) return extra{"field1"} @@ -212,7 +212,7 @@ func TestWrapper(t *testing.T) { rw := httptest.NewRecorder() handler := New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Before(func() *myerr { return &myerr{} }). @@ -227,7 +227,7 @@ func TestWrapper(t *testing.T) { require.Equal(t, http.StatusCreated, rw.Result().StatusCode) handler = New(). - WithConstruct(nopConstructor). + WithRequestReader(nopConstructor). Before(func() *myerr { return nil }).