Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

feat: parse path template using regexes#823

Merged
xiaozhenliu-gg5 merged 10 commits intomasterfrom
non-slash-resource
May 21, 2020
Merged

feat: parse path template using regexes#823
xiaozhenliu-gg5 merged 10 commits intomasterfrom
non-slash-resource

Conversation

@xiaozhenliu-gg5
Copy link
Copy Markdown
Contributor

@xiaozhenliu-gg5 xiaozhenliu-gg5 commented May 14, 2020

  1. rewrite the parse path template logic in Typescript, using regexes.
  2. remove pegjs grammar.
  3. remove pegjs generated pathTemplateParser.js
  4. remove parserExtras.ts.
  5. remove pegjs related unit tests.
  6. no need to copy src/*.js for compile in package.json

PathTemplate class has same functionality as before, but the size/segments have been changed their meanings (not sure what they do originally, we don't use them anywhere, but removing them will be a breaking change).

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 14, 2020
@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2020

Codecov Report

Merging #823 into master will decrease coverage by 0.42%.
The diff coverage is 83.12%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #823      +/-   ##
==========================================
- Coverage   90.05%   89.63%   -0.43%     
==========================================
  Files          48       46       -2     
  Lines        8134     7408     -726     
  Branches      493      549      +56     
==========================================
- Hits         7325     6640     -685     
+ Misses        806      765      -41     
  Partials        3        3              
Impacted Files Coverage Δ
src/pathTemplate.ts 87.76% <83.12%> (-10.63%) ⬇️
.mocharc.js 78.57% <0.00%> (-14.29%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d53e169...ab68599. Read the comment docs.

Copy link
Copy Markdown
Contributor

@JustinBeckwith JustinBeckwith left a comment

Choose a reason for hiding this comment

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

Looks fine to me. Can we also drop the pegjs dependency?

Comment thread src/pathTemplate.ts Outdated
} else {
out.push(segment);
if (Object.keys(bindings).length !== Object.keys(this.bindings).length) {
throw new TypeError('The number of variables does not match');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This error message is not actionable. The previous one, 'Value for key %s is not provided in %s', gives some clue about what's wrong. Can we improve it here?

Please note that all the errors thrown from here will be user visible. Expect to receive a client library issue for all the error messages that are just a little bit unclear :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure, here it will complain if the given bindings do not have the same variable number with the needed variables.
This sounds better maybe:

        `The number of variables ${
          Object.keys(bindings).length
        } does not match the number of needed variables ${
          Object.keys(this.bindings).length
        }`

Comment thread src/pathTemplate.ts Outdated
// if the path contains a wildcard, then the length may differ by 1.
if (!this.data.includes('**')) {
throw new TypeError(
`This path ${path} does not match, the parameter number is not same.`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This path does not match what exactly? ("match" is something that has 2 parameters, "a matches b", and it makes sense to have both in the error message)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

reasonable, i have include this.data (original path template) in the error messages.

Comment thread src/pathTemplate.ts
}
// {project} / {project=*} -> segments.push('{project=*}');
// -> bindings['project'] = '*'
else if (segment.match(/(?<={)[0-9a-zA-Z-.~_]+(=\*)?(?=})/)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Look behinds are nice! :)

I will need some time to review this logic, please don't merge just yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sounds great! Appreciate that!

Comment thread src/pathTemplate.ts Outdated
throw new TypeError(msg);
} else if (this.segments[index].includes('*')) {
const segment = this.segments[index];
const variable = segment.match(/(?<={)[$0-9a-zA-Z_]+(?==.*})/g);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just in case, please use minimal quantifier here (.*? instead of .*). In an (invalid) case of a path template having two =......} occurrences, we want to make sure .* eats the minimum possible match, hence .*?.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! applying the changes now.
This PR is only for rewriting the logic, and make sure all original unit tests pass. I will make a separate one to add non-slash-resource logic.

Copy link
Copy Markdown
Contributor

@alexander-fenster alexander-fenster left a comment

Choose a reason for hiding this comment

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

Let's try that :)

@xiaozhenliu-gg5 xiaozhenliu-gg5 merged commit 392a392 into master May 21, 2020
@xiaozhenliu-gg5 xiaozhenliu-gg5 deleted the non-slash-resource branch May 21, 2020 00:48
gcf-merge-on-green Bot pushed a commit that referenced this pull request May 21, 2020
🤖 I have created a release \*beep\* \*boop\* 
---
## [2.4.0](https://www.github.com/googleapis/gax-nodejs/compare/v2.3.1...v2.4.0) (2020-05-21)


### Features

* parse path template using regexes ([#823](https://www.github.com/googleapis/gax-nodejs/issues/823)) ([392a392](https://www.github.com/googleapis/gax-nodejs/commit/392a3920df6d78981ac43741f15048c84102b046))
* support non-slash resource in path template ([#833](https://www.github.com/googleapis/gax-nodejs/issues/833)) ([76696fc](https://www.github.com/googleapis/gax-nodejs/commit/76696fc48c8a5e21c3c1cde56822b7a37585e41c))


### Bug Fixes

* new typescript, strict types ([#824](https://www.github.com/googleapis/gax-nodejs/issues/824)) ([90034ce](https://www.github.com/googleapis/gax-nodejs/commit/90034ce6a8c9b635942fedb23345105264979416))
* typescript 3.9.3 compilation ([#831](https://www.github.com/googleapis/gax-nodejs/issues/831)) ([d53e169](https://www.github.com/googleapis/gax-nodejs/commit/d53e16988aa0fa260c91f84e6fc0ceae2fdecc26))
---


This PR was generated with [Release Please](https://github.com/googleapis/release-please).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants