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: add escape sequence for terminal characters #165

Merged
merged 5 commits into from
May 14, 2022

Conversation

mochja
Copy link
Contributor

@mochja mochja commented May 3, 2022

allows filtering properties that contains special characters ,*()/

use \ (backslash) for escaping such characters:

  • foo\/bar, will filter JSON property that matches foo/bar

fixes #164

@mochja
Copy link
Contributor Author

mochja commented May 3, 2022

I think this would introduce breaking change for keys that contain *, e.g.: foo* will now have to be escaped foo\\*, this is due to * character not being considered as a terminal character.

Please let me know if you consider this as an issue.

@nemtsov
Copy link
Owner

nemtsov commented May 4, 2022

@mochja 🙌 — that's a beautiful PR! Thank you so much for your contribution. I think the escaping looks solid: both in a functional test and statically. The only piece that I'm unsure about is making * a terminal. I have a strong feeling there's a reason you converted it, but I'm not sure what test would fail if you didn't. Can you talk a bit about what issue that solves, please?

@nemtsov
Copy link
Owner

nemtsov commented May 4, 2022

@mochja heads up—since I was in the code, I did a bit of cleaning and (a) renamed the master branch to main (so you may need to update your local), (b) switched away from the defunct travis-ci.org to GH-actions (so you'll now notice a tiny linting issue in this PR), (c) fixed all the dev-dependencies security issues. So, please rebase off the latest main branch.

@mochja
Copy link
Contributor Author

mochja commented May 4, 2022

@mochja 🙌 — that's a beautiful PR! Thank you so much for your contribution. I think the escaping looks solid: both in a functional test and statically. The only piece that I'm unsure about is making * a terminal. I have a strong feeling there's a reason you converted it, but I'm not sure what test would fail if you didn't. Can you talk a bit about what issue that solves, please?

Thank you!

It allows for escaping * symbol, so you can properly mask object that has star as a key. I believe it can be done differently as well, but will require special handling of the star. I felt like that is really an edge case that is not worth additional complexity.

allows filtering properties that contains special characters `,*()/`

use `\` (backslash) for escaping such characters:

foo\/bar, will filter JSON property that matches foo/bar.
@mochja
Copy link
Contributor Author

mochja commented May 4, 2022

@nemtsov I reverted the change for making * as a terminal, also fixed issue with escaping backslash keys.

Copy link
Owner

@nemtsov nemtsov left a comment

Choose a reason for hiding this comment

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

This looks really solid. The only blocker I have is the double-escaping of the reverse solidus, which I mentioned below. I opened a PR with your changes in it, and a fix here: #167 — if that looks good to you, I'll merge it. If you want to make some more changes, please feel free to rebase on top of that or implement it your own way, and I'll review. Thank you!

lib/compiler.js Show resolved Hide resolved
maybePushName()
tokens.push({ tag: ch })
} else if (ch === WILDCARD_CHAR && !name.length && !escape) {
Copy link
Owner

@nemtsov nemtsov May 10, 2022

Choose a reason for hiding this comment

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

@mochja q: the !name.length is for stars that follow characters (e.g. a*), what about stars that precede them (e.g. *a)? Is it intentional that you must escape them now?

(e.g. $ echo '{"*a":1}' | node ./bin/json-mask.js '*a' doesn't work and creates a compiled mask: { '*': { type: 'object', properties: { a: [Object] }, filter: true } } and yet $ echo '{"a*":1}' | node ./bin/json-mask.js 'a*' does work and has a mask: { 'a*': { type: 'object' } })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, this part would have to reworked for this to work, maybe we could use something from #167?

Copy link
Owner

Choose a reason for hiding this comment

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

Made a few adjustments and I think it's working fully in #167
How does it look to you @mochja ?

@nemtsov nemtsov merged commit 72c2510 into nemtsov:main May 14, 2022
@nemtsov
Copy link
Owner

nemtsov commented May 14, 2022

@mochja this has been published in v2.0.0 (https://github.com/nemtsov/json-mask/releases/tag/v2.0.0)
Thank you for an awesome 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.

How to get keys that contains ",/()"?
2 participants