Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to printf in jsonnet?

Tags:

jsonnet

Is there a way to print the objects in jsonnet? This is for debugging purposes mainly.

I am using error to print the objects but that terminates the program execution.

local obj = [
{
  myKey: 2,
}];
error 'Printing' + std.toString(obj)

Outputs:

RUNTIME ERROR: Printing[{"myKey": 2}]
    snippet:6:1-37  

A better way to do this ?

like image 793
Hakan Baba Avatar asked Mar 07 '23 11:03

Hakan Baba


1 Answers

To followup on Dave Cunningham's answer, std.trace() is available since 0.11.0, it behaves like a "hook in the middle", where it's 1st argument is the string you want to show, 2nd is what you want to return.

Using it for the provided example:

$ cat foo.jsonnet
local obj = [
{
    myKey: 2,
}];
std.trace("obj content: %s" % [obj], obj)

$ jsonnet foo.jsonnet 
TRACE: foo.jsonnet:5 obj content: [{"myKey": 2}]
[
   {
      "myKey": 2
   }
]
like image 174
jjo Avatar answered Jun 22 '23 23:06

jjo