garden icon indicating copy to clipboard operation
garden copied to clipboard

can't grab element from array after using split() in string templating

Open Orzelius opened this issue 2 years ago • 1 comments

This doesn't work with err: Invalid template string (${split("something.else", ".")[0]}): Unable to parse as valid template string.

kind: Module
name: test
description: test env string templating
type: exec
variables:
  element: ${split("something.else", ".")[0]}

tasks:
  - name: test
    env:
      TEST: ${var.element}
    command: ["echo $TEST"]

This does work as expected (prints "else" to the console)

kind: Module
name: test
description: test env string templating
type: exec
variables:
  arr: ${split("something.else", ".")}

tasks:
  - name: test
    env:
      TEST: ${var.arr[1]}
    command: ["echo $TEST"]

Orzelius avatar Jun 10 '22 09:06 Orzelius

This is still a valid issue, wasn't fixed by #3057.

vvagaytsev avatar Jul 25 '22 08:07 vvagaytsev

@hnicke encountered this last week on the Discord working around another issue

split(string((var.memory-mb * 0.9) / 2), '.')[0]  -> Unable to parse as valid template string.

worldofgeese avatar Mar 20 '23 13:03 worldofgeese

looking for a workaround? here's one that i use:

${slice(split("foo,bar,baz", ","), 0, 1)} 

this resolves to "foo"

UPDATE

2023 June 22 16:48 GMT-4

After upgrading to 0.13.3 I found the above workaround inconsistent (validation errors in some fields but not others, ie io.k8s.api.core.v1.Container.command: got "array", expected "string" ). Here's the new workaround

${string(slice(split("foo,bar,baz", ","), 0, 1))} 

This resolves to "foo"

chrispsplash avatar May 09 '23 20:05 chrispsplash