Skip to content

Commit

Permalink
Implement @tcl format
Browse files Browse the repository at this point in the history
  • Loading branch information
dbohdan committed Sep 4, 2016
1 parent 6005caa commit 19f2e61
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,28 @@ def walk(f):
else f
end;

def format(fmt): _format(fmt);
# Convert the input to a string representation of Tcl dictionaries.
def totcl:
if type == "array" then
# Convert array to object with keys 0, 1, 2... and process
# it as object.
[range(0;length) as $i
| {key: $i | tostring, value: .[$i]}]
| from_entries
| totcl
elif type == "object" then
to_entries
| [.[] | "{" + .key + "} {" + (.value | totcl) + "}"]
| join(" ")
else
tostring
| gsub("{";"\\{")
| gsub("}";"\\}")
end;

def format(fmt):
if fmt == "tcl" then
totcl
else
_format(fmt)
end;
4 changes: 4 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ null
["foo", 1, ["a", 1, "b", 2, {"foo":"bar"}]]
["foo",1,["a",1,"b",2,{"foo":"bar"}]]

@tcl
[0,"foo",{"bar":{"baz":"quux"}}]
"{0} {0} {1} {foo} {2} {{bar} {{baz} {quux}}}"

#
# Dictionary construction syntax
#
Expand Down

0 comments on commit 19f2e61

Please sign in to comment.