Skip to content

Commit

Permalink
Merge pull request #24 from themue/master
Browse files Browse the repository at this point in the history
Reduced handleJob complexity and finished fix
  • Loading branch information
Frank Mueller authored Jun 19, 2017
2 parents 6434dc3 + cecde87 commit d717ec1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 62 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ The library earlier has been known as `web` package of the

I hope you like it. ;)

[![GitHub release](https://img.shields.io/github/release/tideland/gorest.svg)](https://github.com/tideland/gorest)
[![GitHub license](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://raw.githubusercontent.com/tideland/gorest/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/tideland/gorest?status.svg)](https://godoc.org/github.com/tideland/gorest)
[![Sourcegraph](https://sourcegraph.com/github.com/tideland/gorest/-/badge.svg)](https://sourcegraph.com/github.com/tideland/gorest?badge)
[![Go Report Card](https://goreportcard.com/badge/github.com/tideland/gorest)](https://goreportcard.com/report/github.com/tideland/gorest)

## Version

Version 2.14.0
Version 2.15.1

## Packages

Expand Down
153 changes: 95 additions & 58 deletions rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,74 +116,111 @@ type InfoResourceHandler interface {
// passed handler. It always tries the nativ method first, then
// the alias method according to the REST conventions.
func handleJob(handler ResourceHandler, job Job) (bool, error) {
id := func() string {
return fmt.Sprintf("%s@%s/%s", handler.ID(), job.Domain(), job.Resource())
}
switch job.Request().Method {
case http.MethodGet:
grh, ok := handler.(GetResourceHandler)
if ok {
return grh.Get(job)
}
rrh, ok := handler.(ReadResourceHandler)
if ok {
return rrh.Read(job)
}
return false, errors.New(ErrNoGetHandler, errorMessages, id())
return handleGetJob(handler, job)
case http.MethodHead:
hrh, ok := handler.(HeadResourceHandler)
if ok {
return hrh.Head(job)
}
return false, errors.New(ErrNoHeadHandler, errorMessages, id())
return handleHeadJob(handler, job)
case http.MethodPut:
prh, ok := handler.(PutResourceHandler)
if ok {
return prh.Put(job)
}
urh, ok := handler.(UpdateResourceHandler)
if ok {
return urh.Update(job)
}
return false, errors.New(ErrNoPutHandler, errorMessages, id())
return handlePutJob(handler, job)
case http.MethodPost:
prh, ok := handler.(PostResourceHandler)
if ok {
return prh.Post(job)
}
crh, ok := handler.(CreateResourceHandler)
if ok {
return crh.Create(job)
}
return false, errors.New(ErrNoPostHandler, errorMessages, id())
return handlePostJob(handler, job)
case http.MethodPatch:
prh, ok := handler.(PatchResourceHandler)
if ok {
return prh.Patch(job)
}
mrh, ok := handler.(ModifyResourceHandler)
if ok {
return mrh.Modify(job)
}
return false, errors.New(ErrNoPatchHandler, errorMessages, id())
return handlePatchJob(handler, job)
case http.MethodDelete:
drh, ok := handler.(DeleteResourceHandler)
if ok {
return drh.Delete(job)
}
return false, errors.New(ErrNoDeleteHandler, errorMessages, id())
return handleDeleteJob(handler, job)
case http.MethodOptions:
orh, ok := handler.(OptionsResourceHandler)
if ok {
return orh.Options(job)
}
irh, ok := handler.(InfoResourceHandler)
if ok {
return irh.Info(job)
}
return false, errors.New(ErrNoOptionsHandler, errorMessages, id())
return handleOptionsJob(handler, job)
}
return false, errors.New(ErrMethodNotSupported, errorMessages, job.Request().Method)
}

// handleGetJob handles a job containing a GET request.
func handleGetJob(handler ResourceHandler, job Job) (bool, error) {
grh, ok := handler.(GetResourceHandler)
if ok {
return grh.Get(job)
}
rrh, ok := handler.(ReadResourceHandler)
if ok {
return rrh.Read(job)
}
return false, errors.New(ErrNoGetHandler, errorMessages, jobDescription(handler, job))
}

// handleHeadJob handles a job containing a HEAD request.
func handleHeadJob(handler ResourceHandler, job Job) (bool, error) {
hrh, ok := handler.(HeadResourceHandler)
if ok {
return hrh.Head(job)
}
return false, errors.New(ErrNoHeadHandler, errorMessages, jobDescription(handler, job))
}

// handlePutJob handles a job containing a PUT request.
func handlePutJob(handler ResourceHandler, job Job) (bool, error) {
prh, ok := handler.(PutResourceHandler)
if ok {
return prh.Put(job)
}
urh, ok := handler.(UpdateResourceHandler)
if ok {
return urh.Update(job)
}
return false, errors.New(ErrNoPutHandler, errorMessages, jobDescription(handler, job))
}

// handlePostJob handles a job containing a POST request.
func handlePostJob(handler ResourceHandler, job Job) (bool, error) {
prh, ok := handler.(PostResourceHandler)
if ok {
return prh.Post(job)
}
crh, ok := handler.(CreateResourceHandler)
if ok {
return crh.Create(job)
}
return false, errors.New(ErrNoPostHandler, errorMessages, jobDescription(handler, job))
}

// handlePatchJob handles a job containing a PATCH request.
func handlePatchJob(handler ResourceHandler, job Job) (bool, error) {
prh, ok := handler.(PatchResourceHandler)
if ok {
return prh.Patch(job)
}
mrh, ok := handler.(ModifyResourceHandler)
if ok {
return mrh.Modify(job)
}
return false, errors.New(ErrNoPatchHandler, errorMessages, jobDescription(handler, job))
}

// handleDeleteJob handles a job containing a DELETE request.
func handleDeleteJob(handler ResourceHandler, job Job) (bool, error) {
drh, ok := handler.(DeleteResourceHandler)
if ok {
return drh.Delete(job)
}
return false, errors.New(ErrNoDeleteHandler, errorMessages, jobDescription(handler, job))
}

// handleOptionsJob handles a job containing an OPTIONS request.
func handleOptionsJob(handler ResourceHandler, job Job) (bool, error) {
orh, ok := handler.(OptionsResourceHandler)
if ok {
return orh.Options(job)
}
irh, ok := handler.(InfoResourceHandler)
if ok {
return irh.Info(job)
}
return false, errors.New(ErrNoOptionsHandler, errorMessages, jobDescription(handler, job))
}

// jobDescription returns a description for possible errors.
func jobDescription(handler ResourceHandler, job Job) string {
return fmt.Sprintf("%s@%s/%s", handler.ID(), job.Domain(), job.Resource())
}

// EOF
6 changes: 3 additions & 3 deletions rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ func TestMethodNotSupported(t *testing.T) {
mux := newMultiplexer(assert)
ts := restaudit.StartServer(mux, assert)
defer ts.Close()
err := mux.Register("test", "method", NewTestHandler("method", assert))
err := mux.Register("test", "no-options", NewTestHandler("no-options", assert))
assert.Nil(err)
// Perform test requests.
req := restaudit.NewRequest("OPTIONS", "/base/test/method")
req := restaudit.NewRequest("OPTIONS", "/base/test/no-options")
resp := ts.DoRequest(req)
resp.AssertBodyContains("OPTIONS")
resp.AssertBodyContains("no-options")
}

// TestRESTHandler tests the mapping of requests to the REST methods
Expand Down

0 comments on commit d717ec1

Please sign in to comment.