You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You see, the eval can't get expected value, it just null. Because dot point at current node position after select, no longer point at document root position, and .deploy-info.es.server.es-host not exist, so eval return null.
Is there any way or a sign, can point at document root node absolutely?
Such as !, if I use eval("!.es-host"), it will find by document root absolutely, not relatively.
Thanks.
The text was updated successfully, but these errors were encountered:
This was more complicated than I though. The short answer to your question is that you can use a create a variable to reference the root . as $r - however this by itself it not enough - because the sub command can only take a string to substitute to, not an expression. In the end I used 'capture' to grab the variable name like so:
yq '. as $r | "\$\{(?P<x>.+?)\}(?P<b>.*)" as $reg |
with(.. | select(tag=="!!str" and match($reg)) ;
capture($reg) as $c | . = ($r | eval("." + $c.x) + $b))
Explanation:
. as $r will capture the root into the $r variable
Because I need to use the reg ex twice, I saved that into a $reg variable as well
"\$\{(?P<x>.+?)\}(?P<b>.*)" will capture the variable into 'x' and the rest of the string into 'b'
with(.. | select(tag=="!!str" and match($reg)) ; will loop over all the strings that match that regex in the document
capture($reg) as $c will run the reg ex with the capture groups, and put the result into the variable c
Finally . = ($r | eval("." + $c.x) + $b will update the current string . by running the variable x from the capture group against the root $r | eval("." + $c.x) and the appending the remaining original string b.
I have a yaml as below, and I want to do a pre-handle, replace ${xxx} to actual value, which from yaml itself.
So, I replace by sub and eval:
yq -i '(.. | select(tag=="!!str")) |= (. | sub("\$\{(.+?)\}", eval(".$1")))' test.yaml
I expected:
But actually is:
You see, the eval can't get expected value, it just null. Because dot point at current node position after select, no longer point at document root position, and .deploy-info.es.server.es-host not exist, so eval return null.
Is there any way or a sign, can point at document root node absolutely?
Such as !, if I use eval("!.es-host"), it will find by document root absolutely, not relatively.
Thanks.
The text was updated successfully, but these errors were encountered: