-
Notifications
You must be signed in to change notification settings - Fork 277
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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}, | ||
} | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why you did this magic on "github.com/gertd/go-pluralize"
"github.com/gobeam/stringy" |
||
|
||
return paramSingular | ||
} |
There was a problem hiding this comment.
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:
But I agree your way the code looks more readable.