-1

I want that by env variable (string) the varb_letter will get different data from my metadata.

here is what I tried to do.

Can u assist?

thanks!!!

varb_letter: (( env("LANDSCAPE") == 'a' ? asjson(.metadata.a) : env("LANDSCAPE") == 'b' ? asjson(.metadata.b) : env("LANDSCAPE") == 'c' ? asjson(.metadata.c) ))

metadata:
  a:
  b:
  c:

env variable control on the variable data, I'm getting errors of wrong syntax

Ran Yanay
  • 3
  • 2

1 Answers1

0

For this answer I assume you use Spiff++ as a templating engine.

There are a few issues:

  • String constants like a need to be enclosed in double quotes, not single quotes, so the correct syntax is env("LANDSCAPE") == "a"
  • The else value must be entered directly after : without a space in between (because that conflicts with default yaml syntax. So the correct syntax for, e.g., the second condition is :env("LANDSCAPE") == "b"
  • You always need an else clause, also for the final condition
  • The metadata needs to be filled

MCVE:

varb_letter: (( env("LANDSCAPE") == "a" ? asjson(.metadata.a) :env("LANDSCAPE") == "b" ? asjson(.metadata.b) :env("LANDSCAPE") == "c" ? asjson(.metadata.c) :false ))

metadata:
  a: 1
  b: 2
  c: 3

Output of spiff++ merge with LANDSCAPE=b for example:

metadata:
  a: 1
  b: 2
  c: 3
varb_letter: "2"
Marijn
  • 1,640
  • 14
  • 24
  • thank you so much for the quick response. however, this variable. always getting false even when the env is 'a' for example – Ran Yanay May 15 '23 at 19:30
  • @RanYanay that may be related to how you set the variable, possibly it is not available in the context where you run Spiff. Could you try just assigning the value of the variable to a key, for example `landscape: (( env("LANDSCAPE") ))`? Or try a variable that should always be defined, like `env("PWD")` (the Present Working Directory). It would also be helpful if you add some more information to your question on which operating system you use exactly, how you set the environment variable and how you run the template (from the terminal, or a system call from other code for example). – Marijn May 15 '23 at 20:57
  • thank you. maybe thats was just syntax error, working fine! – Ran Yanay May 16 '23 at 06:05