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

fix(util): add escape system so '*', '[]' can be escaped #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

achimoraites
Copy link

References

Issue(s): #205

Description

Add escape system to enable walk.Path to use * [ ] in paths

Examples:

  • object\*field
  • array[][].field\\[]
  • array[][].field\\[tx\\]

Copy link
Member

@System-Glitch System-Glitch left a comment

Choose a reason for hiding this comment

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

Thank you very much for your PR!

The approach is good, but there are some cases that are not covered in your current implementation.

path.String() should also return the path with the escape characters (\) shown to avoid confusion.

I'm not sure exactly how we should do that. Maybe we can add a Original string field in the Path struct. This would break a ton of tests though.

Alternatively, we could keep the original path segment in the Name field and removeEscapeChars when walking in path.Walk. Other uses of this field (in the validation package) will need to be updated.

If you have a better suggestion, feel free to suggest!

},
}, path)

path, err = Parse("array[][].field\\[tx\\]")
Copy link
Member

Choose a reason for hiding this comment

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

This is great that this use-case works well! We should recommend the use of backquotes to avoid having to write double slashes, just like regexes.

Comment on lines +442 to +446
escape := map[rune]bool{
'*': true,
'[': true,
']': true,
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we escape the dot (.) too?

}

func TestEscapedParse(t *testing.T) {
path, err := Parse(`object\*field`)
Copy link
Member

Choose a reason for hiding this comment

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

* has an effect only if it's a full segment (e.g.: object.* <=> path.Name = "*"). Your test here is still correct and you should keep it. However, you should add one that makes sure it is possible to escape a "*" segment and that path.Walk() doesn't iterate on all keys in that case.

@@ -436,6 +439,12 @@ func Depth(p string) uint {
}

func createPathScanner(path string) *bufio.Scanner {
escape := map[rune]bool{
Copy link
Member

Choose a reason for hiding this comment

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

For a tiny memory optimization, can you change this to a map[rune]struct{} please?

// Skips the next character
i += width
continue
}
if isValidSyntax(r, next) {
Copy link
Member

@System-Glitch System-Glitch Jan 6, 2025

Choose a reason for hiding this comment

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

You need to check in this function that a backslash (\) is followed by either another backslash or a character that can be escaped. You should also allow escaping the backslash character. path\to\element should be an invalid path, while path\\to\\element should be valid.

This will create a behavior that is more intuitive and closer to what developers are used to when escaping characters in general.

This function is badly named, it should be isSyntaxInvalid or something like that.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 12604780714

Details

  • 21 of 21 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.007%) to 97.505%

Totals Coverage Status
Change from base Build 12292911853: 0.007%
Covered Lines: 6449
Relevant Lines: 6614

💛 - Coveralls

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.

3 participants