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

feat: support google custome verb #413

Merged
merged 6 commits into from Sep 11, 2019
Merged

Conversation

ShiningRush
Copy link
Contributor

@ShiningRush ShiningRush commented Sep 5, 2019

support google custome verb
resolve this issue. #394

{"/{userId}:init", "/{userId}"},
{"/{userId:init}", "/{userId:init}"},
{"/users/{id:init}:init", "/users/{id:init}"},
{"/users/{id}", "/users/{id}"},
Copy link
Owner

Choose a reason for hiding this comment

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

this will fail: "/init/users/{id:init}:init" , "/init/users/{id:init}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i added it to case, and run success, is something wrong?

	testCase := []struct{
		path string
		expectedPath string
	}{
		{"/{userId}:init", "/{userId}"},
		{"/{userId:init}", "/{userId:init}"},
		{"/users/{id:init}:init", "/users/{id:init}"},
		{"/users/{id}", "/users/{id}"},
		{"/init/users/{id:init}:init" , "/init/users/{id:init}"},
	}

Copy link
Owner

Choose a reason for hiding this comment

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

tbh, i did not execute this test. i should have said "will this fail?". I came up with this test because I saw "ReplaceAllString" in removeCustomVerb. And this case "/:init/users:init/" is probably not a valid path, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

emmm, it likes "/users/{id:init}:init", so i think the case is not necessary.
and replace regex is ":customVerb$", it will just replace last word.

Copy link
Owner

Choose a reason for hiding this comment

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

ok, good!

custom_verb.go Outdated Show resolved Hide resolved
curly.go Outdated
@@ -72,6 +72,15 @@ func (c CurlyRouter) matchesRouteByPathTokens(routeTokens, requestTokens []strin
return false, 0, 0
}
requestToken := requestTokens[i]
if hasCustomVerb(routeToken){
Copy link
Owner

Choose a reason for hiding this comment

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

the hasCustomVerb is doing a regexp operation. this will affect performance. for this reason, I would like to introduce a field in the Router called CustomVerbEnabled with an default value of false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@emicklei, now it will calculate if a route has custom verb when build route

Copy link
Owner

Choose a reason for hiding this comment

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

so, we don't need the CustomVerbEnabled !

@@ -29,6 +29,11 @@ func (d defaultPathProcessor) ExtractParameters(r *Route, _ *WebService, urlPath
} else {
value = urlParts[i]
}
if hasCustomVerb(key) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is here need to check CustomVerbEnabled? @emicklei

Copy link
Owner

Choose a reason for hiding this comment

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

yes, do you agree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it should be checked

}

// Initialize for Route
func (r *Route) postBuild() {
r.pathParts = tokenizePath(r.Path)
r.hasCustomVerb = hasCustomVerb(r.Path)
Copy link
Owner

Choose a reason for hiding this comment

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

nice!

@emicklei
Copy link
Owner

emicklei commented Sep 6, 2019

to complete the PR, we should have an entry in the features list on the README refering to https://cloud.google.com/apis/design/custom_methods and a minimal example in the /examples folder.

@ShiningRush
Copy link
Contributor Author

@emicklei , i have added a example (examples/restful-google-custom-method.go) and updated readme.md,

// This example shows how to create a Route with google custom method
// Requires the use of a CurlyRouter and path should end with the custom method
//
// GET http://localhost:8080/resource/some-resource-id:init
Copy link
Owner

Choose a reason for hiding this comment

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

resource -> basepath

func main() {
DefaultContainer.Router(CurlyRouter{})
ws := new(WebService)
ws.Route(ws.GET("/basepath/{resourceId}:init").To(fromPathParam))
Copy link
Owner

Choose a reason for hiding this comment

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

i think it is good to add another Route to demonstrate that the verb is causing another function to be called.

so next to :init have :reset or some other verb you can think of.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@emicklei , well, i have already fixed example

@emicklei
Copy link
Owner

i think we are ready to merge

@ShiningRush
Copy link
Contributor Author

ShiningRush commented Sep 10, 2019

ok, lets do it, and we can resolve #394

@ShiningRush
Copy link
Contributor Author

ShiningRush commented Sep 10, 2019

i found the issue #374 , and in this commit i added a go.mod file to support go mod , is here would be any problems? @emicklei

@emicklei
Copy link
Owner

well module support was quite a challenge with subpackages not importing a version. I will have a look again. Would be better to separate that from this PR.

@ShiningRush
Copy link
Contributor Author

ShiningRush commented Sep 11, 2019

@emicklei ok, i removed the go.mod file, but it is worth to say that go mod replace can not import a local package which without go.mod, this maybe cause some inconvenience to go-restfuls contributors when they are debugging in local environment.

@emicklei
Copy link
Owner

yes, i understand. It will be one of the first changes next to do.

@ShiningRush
Copy link
Contributor Author

ShiningRush commented Sep 11, 2019

good! and now we can merge it, right?

@emicklei emicklei merged commit 95fec02 into emicklei:master Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants