Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add resource for routes #210

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
107 changes: 107 additions & 0 deletions rest/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package rest

import (
"github.com/gertd/go-pluralize"
"github.com/gin-gonic/gin"
"github.com/gobeam/stringy"
"net/http"
"regexp"
"strings"
)

type ResourceHandlers map[string]gin.HandlerFunc

type ResourceOptions struct {
Param string
Exclude []string
}

func Resource(group *gin.RouterGroup, handlers ResourceHandlers, options ResourceOptions) *gin.RouterGroup {
name := getNameFromPath(group.BasePath())
options.Param = getRouteParam(name, options.Param)

handleResourceMethod(group, &handlers, options)

return group
}

type routeMethod struct {
Name string
HttpMethod string
Param string
}

type routeMethodList map[string]routeMethod

func allowedRouteMethods(param string) routeMethodList {
return routeMethodList{
"Index": routeMethod{Name: "Index", HttpMethod: http.MethodGet},
"Create": routeMethod{Name: "Create", HttpMethod: http.MethodPost},
"Show": routeMethod{Name: "Show", HttpMethod: http.MethodGet, Param: param},
"Update": routeMethod{Name: "Update", HttpMethod: http.MethodPut, Param: param},
"Delete": routeMethod{Name: "Delete", HttpMethod: http.MethodDelete, Param: param},
Comment on lines +38 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd go with having enum-style indexes, rather than strings, to avoid possibility of having a typo:

const (
	MethodIndex = iota
	MethodCreate
	MethodShow
	MethodUpdate
	MethodDelete
)

But I agree your way the code looks more readable.

}
}

func handleResourceMethod(group *gin.RouterGroup, handlers *ResourceHandlers, options ResourceOptions) {
for _, method := range allowedRouteMethods(options.Param) {
handler := (*handlers)[method.Name]

if handler != nil && !isExcludedMethod(method.Name, options.Exclude) {
param := ""
if method.Param != "" {
param += "/:" + method.Param
}

group.Handle(
method.HttpMethod,
param,
handler,
)
}
}
}

func isExcludedMethod(methodName string, excluded []string) bool {
if len(excluded) < 1 {
return false
}

for _, em := range excluded {
if em == methodName {
return true
}
}

return false
}

func getNameFromPath(path string) (name string) {
if !strings.Contains(path, "/") || path == "" {
return
}

r := regexp.MustCompile(`\w+$`)
name = r.FindStringSubmatch(path)[0]

return
}

func getRouteParam(name string, param string) string {
if param != "" {
return param
}

return getParamFromName(name)
}

func getParamFromName(name string) string {
if name == "" {
return "id"
}

param := stringy.New(name).KebabCase().ToLower()
paramSingular := pluralize.NewClient().Singular(param)
Comment on lines +103 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why you did this magic on routeMethod.Param. I guess you probably were thinking about the safety and bad names specified by users? I would remove this and keep this up to the users to think about what is allowed and what is not in the query string.
Also, this adds two unnecessary dependencies on 3rd-party packages:

"github.com/gertd/go-pluralize"
"github.com/gobeam/stringy"


return paramSingular
}