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

Add query/reference as a first-class VRL type. #25

Open
fuchsnj opened this issue Sep 9, 2022 · 3 comments
Open

Add query/reference as a first-class VRL type. #25

fuchsnj opened this issue Sep 9, 2022 · 3 comments
Labels
type: feature A value-adding code addition that introduce new functionality. vrl: compiler Changes to the compiler

Comments

@fuchsnj
Copy link
Member

fuchsnj commented Sep 9, 2022

A note for the community

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Use Cases

Today the syntax for "reading a value from an object", and "a query path referencing a field of an object" are identical.

Examples

query an object

. = {"foo": {"bar": 3}}
x = .foo.bar
# x is 3

delete a field

. = {"foo": {"bar": 3}}

# the function arg is treated as a "query". The value 3 is not passed into the function.
del(.foo.bar)

Having a distinct syntax for creating references vs accessing a field could allow more elegant code and reduce confusion here.
As an example, a vector user attempted to delete multiple fields in an object by using the following code (which doesn't work):

fields = [".source_type", ".kubernetes.pod_ip"]
for_each(fields) -> |_index, value| {
  del(value)
}

fields here just contains an array of strings, which fails to compile since del expects "queries". If fields was switched to
fields = [.source_type, .kubernetes.pod_ip], then it would just contain an array of the values that the query paths point to.

I am proposing that additional syntax is added to create "references", which just point to specific fields/indices of objects/arrays.
This is just a standard query path with & appended. For example, a = &.foo.bar would mean that the local variable a is a reference to .foo.bar. The above invalid example could be re-written with these changes so it would work as expected.

fields = [&.source_type, &.kubernetes.pod_ip]
for_each(fields) -> |_index, value| {
  del(value)
}

Note that this would require some additional work before it could be it could be implemented:

Version

0.24.0

@fuchsnj fuchsnj added type: feature A value-adding code addition that introduce new functionality. vrl: compiler Changes to the compiler labels Sep 9, 2022
@fuchsnj fuchsnj transferred this issue from vectordotdev/vector Mar 28, 2023
@ayZagen
Copy link

ayZagen commented Sep 10, 2023

are there any plans for this to happen? our vrl functions became hard to maintain as we play around json a lot. maybe title should be changed to receive more attention as no one would search it like that.

@jszwedko
Copy link
Member

are there any plans for this to happen? our vrl functions became hard to maintain as we play around json a lot. maybe title should be changed to receive more attention as no one would search it like that.

👍 what keywords do you think would help people discover it?

@ayZagen
Copy link

ayZagen commented Sep 12, 2023

After re-reading the issue description and why I have commented that, I realized this issue is just describing a new type for query path "as the title describes" 😅

The feature we need is Allow using strings as json paths which is related this issue. That's why I made that comment.

Sorry for that, as creating a new issue makes more sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A value-adding code addition that introduce new functionality. vrl: compiler Changes to the compiler
Projects
None yet
Development

No branches or pull requests

3 participants