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

Complex nested parsing with Remap (waninfo) #5852

Closed
binarylogic opened this issue Jan 5, 2021 · 3 comments
Closed

Complex nested parsing with Remap (waninfo) #5852

binarylogic opened this issue Jan 5, 2021 · 3 comments
Labels
domain: parsing Anything related to parsing within Vector domain: vrl Anything related to the Vector Remap Language type: feature A value-adding code addition that introduce new functionality. type: help User support and help.

Comments

@binarylogic
Copy link
Contributor

From a user in our chat:

Given this event:

{
	"waninfo": "name=wan1,bytes=6042294/1139384031,packets=49527/1902253;name=wan2,bytes=6042294/1150716872,packets=49527/1909091;"
}

And this Remap script:

.message.waninfo = split(.message.waninfo, ";")

The following is produced:

{
	"waninfo": [
	      "name=wan1,bytes=6042294/1139384031,packets=49527/1902253",
	      "name=wan2,bytes=6042294/1150716872,packets=49527/1909091",
	      ""
	  ]
}

How would a user go about parsing each n items in the result? Or, better yet, what is the best UX we could provide to parse this?

@binarylogic binarylogic added type: help User support and help. type: feature A value-adding code addition that introduce new functionality. domain: parsing Anything related to parsing within Vector domain: vrl Anything related to the Vector Remap Language labels Jan 5, 2021
@JeanMertz
Copy link
Contributor

JeanMertz commented Jan 5, 2021

I suspect there is a high likelihood that people want to iterate over maps or arrays. I'm not talking about traversing nested maps/arrays, but simply iterating over a single-level map/array.

I'd personally expect something like this to work (written out over multiple lines for clarity):

waninfos = compact(split(.message.waninfo, ";"))
// ["...", "..."]

indexed_waninfos = enumerate(waninfos)
// {"0": "...", "1": "..." }

for index, info in indexed_waninfos {
	index = to_int(index) // 0, 1
	.message.waninfo[index] = parse_key_value(info)
}

Or:

for index, info in enumerate(compact(split(.message.waninfo, ";"))) {
	.message.waninfo[to_int(index)] = parse_key_value(info)
}

For this to work, there are three additions we need to make:

  • add the enumerate function
  • add for-loop statements (I have a draft for this locally)
  • add dynamic path indexing

@JeanMertz JeanMertz self-assigned this Jan 5, 2021
@JeanMertz
Copy link
Contributor

After playing around with it locally, I think we should just "automatically" add an index if a second argument is provided when iterating an array:

array without index:

for value in ["foo", "bar"] {
	value // "foo", "bar"
}

array with index:

for index, value in ["foo", "bar"] {
	index // 0, 1
	value // "foo", "bar"
}

map:

for key, value in { "foo": "bar" } {
	key // "foo"
	value // "bar"
}

@JeanMertz
Copy link
Contributor

Closing this, as we now support iterating arbitrary objects/arrays in VRL (starting with for_each, map_values and map_keys).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain: parsing Anything related to parsing within Vector domain: vrl Anything related to the Vector Remap Language type: feature A value-adding code addition that introduce new functionality. type: help User support and help.
Projects
None yet
Development

No branches or pull requests

2 participants