Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Add ResourceName to RouteInfo struct #1798

Merged
merged 6 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/gobuffalo/buffalo
go 1.13

require (
cloud.google.com/go v0.36.0 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/cockroachdb/cockroach-go v0.0.0-20190925194419-606b3d062051 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.7.0
github.com/gobuffalo/attrs v0.1.0
Expand All @@ -29,6 +31,7 @@ require (
github.com/google/go-cmp v0.3.1
github.com/gorilla/mux v1.7.3
github.com/gorilla/sessions v1.2.0
github.com/jackc/pgconn v1.0.1 // indirect
github.com/karrick/godirwalk v1.10.12
github.com/markbates/grift v1.1.0
github.com/markbates/oncer v1.0.0
Expand All @@ -37,6 +40,7 @@ require (
github.com/markbates/sigtx v1.0.0
github.com/monoculum/formam v0.0.0-20190830100315-7ff9597b1407
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.2.1 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.4.0
Expand Down
17 changes: 9 additions & 8 deletions route_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import (
// RouteInfo provides information about the underlying route that
// was built.
type RouteInfo struct {
Method string `json:"method"`
Path string `json:"path"`
HandlerName string `json:"handler"`
PathName string `json:"pathName"`
Aliases []string `json:"aliases"`
MuxRoute *mux.Route `json:"-"`
Handler Handler `json:"-"`
App *App `json:"-"`
Method string `json:"method"`
Path string `json:"path"`
HandlerName string `json:"handler"`
ResourceName string `json:"resourceName,omitempty"`
PathName string `json:"pathName"`
Aliases []string `json:"aliases"`
MuxRoute *mux.Route `json:"-"`
Handler Handler `json:"-"`
App *App `json:"-"`
}

// String returns a JSON representation of the RouteInfo
Expand Down
30 changes: 22 additions & 8 deletions route_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func (a *App) Resource(p string, r Resource) *App {
}

rt := rv.Type()
rname := fmt.Sprintf("%s.%s", rt.PkgPath(), rt.Name()) + ".%s"
resourceName := rt.Name()
handlerName := fmt.Sprintf("%s.%s", rt.PkgPath(), resourceName) + ".%s"

n := strings.TrimSuffix(rt.Name(), "Resource")
paramName := name.New(n).ParamID().String()
Expand All @@ -171,29 +172,42 @@ func (a *App) Resource(p string, r Resource) *App {
}

spath := path.Join(p, "{"+paramName+"}")
setFuncKey(r.List, fmt.Sprintf(rname, "List"))

indexBeforeResourceAdded := a.routes.Len()

setFuncKey(r.List, fmt.Sprintf(handlerName, "List"))
g.GET(p, r.List)

if n, ok := r.(newable); ok {
setFuncKey(n.New, fmt.Sprintf(rname, "New"))
setFuncKey(n.New, fmt.Sprintf(handlerName, "New"))
g.GET(path.Join(p, "new"), n.New)
}

setFuncKey(r.Show, fmt.Sprintf(rname, "Show"))
setFuncKey(r.Show, fmt.Sprintf(handlerName, "Show"))
g.GET(path.Join(spath), r.Show)

if n, ok := r.(editable); ok {
setFuncKey(n.Edit, fmt.Sprintf(rname, "Edit"))
setFuncKey(n.Edit, fmt.Sprintf(handlerName, "Edit"))
g.GET(path.Join(spath, "edit"), n.Edit)
}

setFuncKey(r.Create, fmt.Sprintf(rname, "Create"))
setFuncKey(r.Create, fmt.Sprintf(handlerName, "Create"))
g.POST(p, r.Create)
setFuncKey(r.Update, fmt.Sprintf(rname, "Update"))

setFuncKey(r.Update, fmt.Sprintf(handlerName, "Update"))
g.PUT(path.Join(spath), r.Update)
setFuncKey(r.Destroy, fmt.Sprintf(rname, "Destroy"))

setFuncKey(r.Destroy, fmt.Sprintf(handlerName, "Destroy"))
g.DELETE(path.Join(spath), r.Destroy)

g.Prefix = path.Join(g.Prefix, spath)

// set route resource names for each routes added
// no length check because cause at least 5 routes have been added so there cannot be an out of range
for i := indexBeforeResourceAdded; i < a.routes.Len(); i++ {
a.routes[i].ResourceName = resourceName
}

return g
}

Expand Down
45 changes: 45 additions & 0 deletions route_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,48 @@ func Test_App_RouteHelpers(t *testing.T) {
r.NoError(err)
r.Equal("/test/1/", string(x))
}

type resourceHandler struct{}

func (r resourceHandler) List(Context) error {
return nil
}

func (r resourceHandler) Show(Context) error {
return nil
}

func (r resourceHandler) Create(Context) error {
return nil
}

func (r resourceHandler) Update(Context) error {
return nil
}

func (r resourceHandler) Destroy(Context) error {
return nil
}

func Test_App_Routes_Resource(t *testing.T) {
r := require.New(t)

a := New(Options{})
r.Nil(a.root)

a.GET("/foo", voidHandler)
a.Resource("/r", resourceHandler{})

routes := a.Routes()
r.Len(routes, 6)
route := routes[0]
r.Equal("GET", route.Method)
r.Equal("/foo/", route.Path)
r.NotZero(route.HandlerName)

for k, v := range routes {
if k > 0 {
r.Equal("resourceHandler", v.ResourceName)
}
}
}