-
Notifications
You must be signed in to change notification settings - Fork 22
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
#4367: Add support for spaces in "Copy Property Path" #4371
Conversation
Codecov Report
@@ Coverage Diff @@
## main #4371 +/- ##
==========================================
+ Coverage 49.73% 49.74% +0.01%
==========================================
Files 899 899
Lines 26511 26518 +7
Branches 5407 5409 +2
==========================================
+ Hits 13185 13192 +7
Misses 12404 12404
Partials 922 922
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
|
||
expectMatch(["user"], "user"); | ||
expectMatch(["users", 0], "users.0"); | ||
expectMatch(["users", 0, "name"], "users.0.name"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: lodash appears to suggest using a[0].b
instead of a.0.b
, but I have not done that because:
- we're already using the latter
- the latter is better looking
- lodash.toPath supports both and has no difference in output, both produce exactly
["a", "0", "b"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I don't really like that we have a second form of array access like this. It makes me feel like there's something down the line like a tool or library or something where this will break. It feels like one of those things where we're arbitrarily breaking a common standard because it sorta works and things just kinda ended up that way. Is there a good reason we shouldn't keep things standardized to [0]
for array access and property paths?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a good reason we shouldn't keep things standardized to [0] for array access and property paths?
IIRC, it was a limitation with mustache not supporting bracket syntax, e.g., see janl/mustache.js#158
My preference would be for the runtime to support both syntax (dot notations works because arrays are objects...), but to standardize what syntax PixieBrix generates/suggests.
I agree that brackets is likely the way to go for arrays to be consistent with the syntax most people are familiar with. However, I would vote ["a", "0", "b"]
should still generate a.0.b
because the type of the "0"
is a string. (I would avoid being fancy with implicit conversion)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So no change, right? ["a", "0", "b"]
and ["a", 0, "b"]
are the same since all keys/indexes are strings: [4,8,16]["1"] === 8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My two cents. A nested property path is not exactly the same as accessing a property in code. I prefer a.0.b
when it comes to property path (frankly speaking I'd like to see something like a.0."User Location".b
if it was supported). Reasons:
- consistency. the only property separator is
.
. always (or as often as possible). - needless complication of logic. to build an accessor that is specific for an array (
a[0]
instead ofa.0
) we'll need to actually find out that the container is an array, but for the sake of getting a value of a deeply nested property it doesn't matter whether a container is an object or an array. - numbered properties can belong to objects.
{ 1: 'user'}
is not an array. Formik's error tree is an example of such container, it's an object that may have integer keys. we can end up with paths likea.1.b
anda[1].b
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
A couple of corrections though:
- It never matters, arrays are objects:
obj[1]
is exactly the same asarr[1]
- There's no such thing as integer keys.
{1: 'str'}
is casted toobject with key '1' and value 'str'
. Even array keys are effectively strings, tryObject.keys([9,9,9])
Adding @BLoe as reviewer since he had investigated a bit on Wednesday. DM thread: https://pixiebrix.slack.com/archives/D029R8902JF/p1664400635838989. |
The issue is that we use the The problem here is:
|
I think we'll likely have to keep track of if there's an unclosed quotation mark? You'd have to write a function for that since IIRC regexes can't count As a reminder, this only applies if the expression starts with a Does lodash have a validation library (or lexer/parser) we can use? Would be great to use the same logic that the expression interpreter is using |
I think that's an issue, UIs should not ignore or revert the user’s input. Our toggles (and underlying blueprints) should default to an "auto" mode that picks the right type until the user picks one. It makes sense to update the type as the user types, but not at the expense of their explicit choice. Pasting
The logic is here, but As for this PR, it doesn't actually change the described behavior, which has always existed and should be tracked/fixed separately as it's a UI problem. |
* @example getPathFromArray("user", "name") // => "user.name" | ||
* @example getPathFromArray("title", "Divine Comedy") // => "title["Divine Comedy"]" | ||
*/ | ||
export function getPathFromArray(parts: Array<string | number>): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reuse joinName with 'space' added to the specialCharsRegex
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks right except it uses compact and it seems specific to formik. While there's some overlap, the logic is too simple to add further conditions.
However potentially "getPathFromArray" could be used inside "joinName" after the compact call and error throw
@fregante regarding pixiebrix-extension/src/utils.ts Lines 83 to 93 in 3cce78f
I think there's no need to change this function. It is meant to quickly concatenate string parts into a pixiebrix-extension/src/utils.ts Line 57 in 3cce78f
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR has been in review for quite a while. It seems that we're good with the update and no functionality is broken by the changes. If this is the case I believe we can merge it.
What does this PR do?
Discussion
_.toPath
Demo
Screen.Recording.2.mov
Future work
Do these also need to be updated/replaced?
pixiebrix-extension/src/utils/debugUtils.ts
Line 31 in 3cce78f
pixiebrix-extension/src/utils.ts
Lines 90 to 93 in 3cce78f
Checklist