Skip to content

Commit

Permalink
other renames
Browse files Browse the repository at this point in the history
  • Loading branch information
apourchet committed Feb 7, 2024
1 parent 88a7a88 commit 5ccfcb7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
12 changes: 8 additions & 4 deletions constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +24,7 @@ type param struct {
func newRunCtx(
rw http.ResponseWriter,
req *http.Request,
cons Constructor,
cons RequestReader,
) *runctx {
ctx := &runctx{
req: req,
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func main() {
mw := &Middlewares{}

wrapper := httpwrap.New().
WithConstruct(httpwrap.StandardRequestReader()).
WithRequestReader(httpwrap.StandardRequestReader()).
Before(mw.checkAPICreds).
Finally(mw.sendResponse)

Expand Down
10 changes: 5 additions & 5 deletions standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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()
Expand Down Expand Up @@ -74,6 +74,6 @@ func NewStandardWrapper() Wrapper {
constructor := StandardRequestReader()
responseWriter := StandardResponseWriter()
return New().
WithConstruct(constructor).
WithRequestReader(constructor).
Finally(responseWriter)
}
10 changes: 5 additions & 5 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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"))
Expand All @@ -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)
Expand All @@ -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")
}).
Expand All @@ -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)
Expand All @@ -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")
}).
Expand All @@ -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"}
Expand All @@ -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"}
Expand All @@ -212,7 +212,7 @@ func TestWrapper(t *testing.T) {
rw := httptest.NewRecorder()

handler := New().
WithConstruct(nopConstructor).
WithRequestReader(nopConstructor).
Before(func() *myerr {
return &myerr{}
}).
Expand All @@ -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
}).
Expand Down

0 comments on commit 5ccfcb7

Please sign in to comment.