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

Handle rendering of URL Query Parameters in renderURL #11

Merged
merged 1 commit into from
Apr 28, 2021

Conversation

atreya2011
Copy link
Contributor

@atreya2011 atreya2011 commented Apr 13, 2021

Resolves #9

TODO:

  • Create a TypeScript function in template to build URL query parameters
  • Handle nested message types.
  • Handle primitive repeated fields.

generator/template.go Outdated Show resolved Hide resolved
registry/message.go Outdated Show resolved Hide resolved
@atreya2011 atreya2011 force-pushed the support-url-query-params branch 3 times, most recently from 9d41bd3 to 35f5a45 Compare April 21, 2021 04:45
@atreya2011 atreya2011 marked this pull request as ready for review April 21, 2021 04:47
@atreya2011 atreya2011 force-pushed the support-url-query-params branch 2 times, most recently from 54cae23 to d5f9d1e Compare April 21, 2021 07:15
@atreya2011 atreya2011 requested a review from lyonlai April 21, 2021 07:16
@atreya2011
Copy link
Contributor Author

atreya2011 commented Apr 21, 2021

@lyonlai

What I finally ended up doing was:

  1. Create a TypeScript function, in the template to build URL query parameters, that handles nested message types & primitive repeated fields along with optional fields.
  2. First I pass the deeply nested object and the list of fields present in the URL path to renderURLSearchParams function present within the template.
  3. In this function, the deeply nested object is flattened using the flattenObject function and then the query string is built using the rules present in the http.proto while excluding the list of fields present in the URL path as parameters.

It returns an empty string if there are no URL query parameters to be rendered.

I have also written a small integration test to check if the query parameters are being rendered and passed properly.

Looking forward to your feedback 🙏🏼

P.S. I have added the following to gen-server-proto.sh to ensure that the proper tool binaries will always be installed before generating the server-side go files.

# remove binaries to ensure that binaries present in tools.go are installed
rm -f $GOBIN/protoc-gen-go $GOBIN/protoc-gen-grpc-gateway $GOBIN/protoc-gen-swagger

go install \
	github.com/golang/protobuf/protoc-gen-go \
	github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
	github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

@atreya2011 atreya2011 changed the title WIP: Handle rendering of URL Query Parameters in renderURL Handle rendering of URL Query Parameters in renderURL Apr 21, 2021
@atreya2011 atreya2011 force-pushed the support-url-query-params branch 8 times, most recently from fd1d843 to 86a3893 Compare April 22, 2021 00:24
Copy link
Collaborator

@lyonlai lyonlai left a comment

Choose a reason for hiding this comment

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

I like the flattenObject idea, this makes runtime to pickup the fields being set. And not to grow the generated asset size too much.

Suggestions have been made. Looking forward to it.

generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
@atreya2011
Copy link
Contributor Author

atreya2011 commented Apr 25, 2021

@lyonlai
I have revised the code based on your feedback and suggestions. However, I had difficulty in implementing Object.keys(flattenedObject).filter(keysInPath).map() because unfortunately, it becomes complicated to return an array of type string[][] to be used in new URLSearchParams(), so I simplified the Object.reduce function.

For example, lets take the following request payload where we filter out key a assuming it is present in the path as a parameter:

const req = {
  a: 10,
  postReq: {
    b: 11
  },
  c: [23, 25],
  extMsg: {
    d: 12
  }
};

After flattening and filtering key a it becomes like this:

{
  "postReq.b": 11,
  c: [23, 25],
  "extMsg.d": 12
};

The above flattened request payload needs to be converted like this to use as input in new URLSearchParams():

[["postReq.b", 11], ["c", 23], ["c", 25], ["extMsg.d", 12]]

However, it becomes complicated to use just Array.map on the filtered and flattened to return an array of type string[][] like above.

This is the implementation I tried which does not return [["postReq.b", 11], ["c", 23], ["c", 25], ["extMsg.d", 12]]:
It returns [["postReq.b", 11], [["c", 23], ["c", 25]], ["extMsg.d", 12]] which is not useable in new URLSearchParams()

Object.keys(flattenedRequestPayload)
    .filter(key => !urlPathParams.includes(key))
    .map(key => {
      const value = flattenedRequestPayload[key];
      if (Array.isArray(value)) {
        return value.map(v => [key, v.toString()]);
      }
      return [key, value.toString()];
    });

Looking forward to your feedback 🙇🏼

@atreya2011 atreya2011 requested a review from lyonlai April 25, 2021 04:51
Copy link
Collaborator

@lyonlai lyonlai left a comment

Choose a reason for hiding this comment

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

it definitely looks nicer on this pass. there are few changes & cases to cover as per comment. Good job.

generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Show resolved Hide resolved
generator/template.go Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
@atreya2011 atreya2011 requested a review from lyonlai April 27, 2021 00:54
@atreya2011 atreya2011 force-pushed the support-url-query-params branch from ba9ba25 to f96ecd4 Compare April 27, 2021 03:11
@atreya2011
Copy link
Contributor Author

I have refactored the code according to your suggestions and added a test for checking falsy values as well 🙇🏼

generator/template.go Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
@atreya2011 atreya2011 force-pushed the support-url-query-params branch from 850070f to a64e415 Compare April 27, 2021 05:20
@atreya2011 atreya2011 requested a review from lyonlai April 27, 2021 05:22
@atreya2011
Copy link
Contributor Author

atreya2011 commented Apr 27, 2021

Thank you once again for taking your time in reviewing this. I have furthered fixed the code according to your suggestions 🙇🏼
Let me know if I have missed anything 🙏🏼

generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
@atreya2011 atreya2011 requested a review from lyonlai April 27, 2021 08:05
@atreya2011 atreya2011 force-pushed the support-url-query-params branch from a04e142 to b36cd8e Compare April 27, 2021 08:34
Copy link
Collaborator

@lyonlai lyonlai left a comment

Choose a reason for hiding this comment

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

Last two comments. This is looking really good. sorry for missing the fieldsInPath construction before.

Also when you finished, can you also please rebase to a single commit and push up?

generator/template.go Outdated Show resolved Hide resolved
generator/template.go Outdated Show resolved Hide resolved
@atreya2011 atreya2011 requested a review from lyonlai April 27, 2021 12:02
@atreya2011 atreya2011 force-pushed the support-url-query-params branch from b36cd8e to f2e7591 Compare April 27, 2021 12:30
@@ -42,10 +41,69 @@ Turn Ton logging to stderr. Default to false.
### `loglevel`
Defines the logging levels. Default to info. Valid values are: debug, info, warn, error

### Notes:
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 have added the notes here. Looking forward to your feedback 🙇🏼

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is too long. The note section is supposed to note the things people unexpected to find, and also supposed to be concise. In the http get case, it is about the field value matches the default value won't be presented in url params. and a short example like the following will be enough .

{fieldA: "A", fieldB: "" fieldC:1} will turned in the /path?filedA=A&fieldC=1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... ok I will try my best to make it more concise. Thank you for the feedback 🙏🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay. I have simplified the notes section 🙇🏼

@atreya2011 atreya2011 force-pushed the support-url-query-params branch 3 times, most recently from abd5e54 to 50b588a Compare April 27, 2021 13:15
@@ -42,10 +41,69 @@ Turn Ton logging to stderr. Default to false.
### `loglevel`
Defines the logging levels. Default to info. Valid values are: debug, info, warn, error

### Notes:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is too long. The note section is supposed to note the things people unexpected to find, and also supposed to be concise. In the http get case, it is about the field value matches the default value won't be presented in url params. and a short example like the following will be enough .

{fieldA: "A", fieldB: "" fieldC:1} will turned in the /path?filedA=A&fieldC=1

README.md Outdated
### Notes:
This library generates URL Search Parameters (if required) at runtime for RPCs annotated as GET requests. For example, given the following proto definition

```proto
Copy link
Collaborator

Choose a reason for hiding this comment

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

protos in the integration test already covers this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So maybe we can point out in the notes to look for the integration test example?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think so. just point a link to the integration test service.proto will do the work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood. Thanks for the feedback 🙇🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Completed 🙏🏼

@@ -57,6 +57,32 @@ message HttpDeleteRequest {
int32 a = 1;
}

message HTTPGetWithURLSearchParamsRequest {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It will be good to have the url example here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok, you mean the URL example from the notes section in the README?

Copy link
Collaborator

Choose a reason for hiding this comment

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

just a url example will be ok. no need to move the whole thing here.

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 understand, just a simple request and response message and a simple RPC implementation.

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 have modified the tests with the URL example. Please let me know if this good or if I have missed something 🙇🏼

@atreya2011 atreya2011 force-pushed the support-url-query-params branch from db26415 to 2994137 Compare April 28, 2021 07:40
@atreya2011 atreya2011 force-pushed the support-url-query-params branch from 2994137 to 2c992f8 Compare April 28, 2021 07:44
@atreya2011 atreya2011 requested a review from lyonlai April 28, 2021 07:45
@atreya2011
Copy link
Contributor Author

Last two comments. This is looking really good. sorry for missing the fieldsInPath construction before.

Also when you finished, can you also please rebase to a single commit and push up?

Also rebased to a single commit 🙇🏼

Copy link
Collaborator

@lyonlai lyonlai left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the contribution @atreya2011

@lyonlai lyonlai merged commit 142798a into grpc-ecosystem:master Apr 28, 2021
@atreya2011 atreya2011 deleted the support-url-query-params branch April 28, 2021 10:52
@atreya2011
Copy link
Contributor Author

atreya2011 commented Apr 28, 2021

LGTM, thanks for the contribution @atreya2011

@lyonlai
Likewise, thank you so much for your patience in reviewing my PR and letting me make this contribution 🙇🏼

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.

Handle rendering of URL Query Parameters in renderURL
2 participants