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

More consistent array traversal semantics #34

Closed
judofyr opened this issue Oct 21, 2020 · 1 comment
Closed

More consistent array traversal semantics #34

judofyr opened this issue Oct 21, 2020 · 1 comment
Assignees

Comments

@judofyr
Copy link
Contributor

judofyr commented Oct 21, 2020

Array traversals aren't yet defined in the spec (see #2), but the intended semantics have been implemented in e.g. groq-js. As I've been thinking and implementing these semantics I've found that they are a bit inconsistent and hard to work with. This issue is intending to make them more consistent and easy to implement.

These are the new proposed rules:

  • Every expression is (at a syntactical level) has a traverse behavior which is either none, implicit or explicit.
  • * and array constructor ([…]) have explicit traverse behavior .
  • The postfix projection operator (base[]) works as follows:
    • If the base has no or implicit traversal behavior, then it converts it to explicit traversal behavior.
    • If the base has explicit traversal behavior, then it applies a flatten operation.
  • The grouping expression ((…)) has no traverse behavior, regardless of what's inside it.
  • Filtering (base[foo > 1]) and slicing (base[1..5]) preserves the traverse behavior of the base.
  • Attribute access (base.foo), dereferencing (base->) and projection (base{bar}) have the following semantics:
    • If the base has either implicit or explicit traverse behavior, then it's being applied for each element in the base (e.g. *{bar} will project {bar} on each element). The returned expression has the same implicit/explicit traverse behavior as the base.
    • If the base has no traverse behavior, then it's being applied directly to the base.
  • Element access (base[5]) has the following semantics:
    • If the base has explicit traverse behavior, then it's being applied for each element in the base.
    • Otherwise, then it's being applied directly to the base while preserving the traverse behavior.
  • All other expressions have no traverse behavior.

Implications:

  • *[_type == "bar"]{baz} still works as expected
  • *[_type == "bar"]{users[]{name}}: The [] is needed when projecting over arrays.
  • Nested traversals still work as expected: *[_type == "bar"]{"avatars": users[].avatar.url}

Implementations:

@judofyr judofyr self-assigned this Oct 21, 2020
@judofyr
Copy link
Contributor Author

judofyr commented Jan 11, 2021

Closed in favor of #42.

@judofyr judofyr closed this as completed Jan 11, 2021
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

No branches or pull requests

1 participant