Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add std.manifestJson manifestJsonEx and manifestYamlStream #160

Merged
merged 1 commit into from
Apr 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,13 @@ limitations under the License.
"\\r"
else if ch == "\t" then
"\\t"
else if ch == "\u0000" then
"\\u0000"
else
local cp = std.codepoint(ch);
if cp < 32 || cp > 126 then
if cp < 32 || (cp >= 126 && cp <= 159) then
"\\u%04x" % [cp]
else
ch;
"\"%s\"" % std.foldl(function(a, b) a + trans(b), std.stringChars(str), ""),
"\"%s\"" % std.join("", [trans(ch) for ch in std.stringChars(str)]),

escapeStringPython(str)::
std.escapeStringJson(str),
Expand All @@ -697,7 +695,7 @@ limitations under the License.
"'\"'\"'"
else
ch;
"'%s'" % std.foldl(function(a, b) a + trans(b), std.stringChars(str), ""),
"'%s'" % std.join("", [trans(ch) for ch in std.stringChars(str)]),

escapeStringDollars(str_)::
local str = std.toString(str_);
Expand All @@ -708,6 +706,47 @@ limitations under the License.
ch;
std.foldl(function(a, b) a + trans(b), std.stringChars(str), ""),

manifestJson(value):: std.manifestJsonEx(value, " "),

manifestJsonEx(value, indent)::
local aux(v, path, cindent) =
if v == true then
"true"
else if v == false then
"false"
else if v == null then
"null"
else if std.type(v) == "number" then
"" + v
else if std.type(v) == "string" then
std.escapeStringJson(v)
else if std.type(v) == "function" then
error "Tried to manifest function at " + path
else if std.type(v) == "array" then
local range = std.range(0, std.length(v) - 1);
local lines = ["[\n" + cindent]
+ std.join([",\n" + cindent],
[[indent + aux(v[i], path + [i], cindent + indent)] for i
in range])
+ ["\n" + cindent + "]"];
std.join("", lines)
else if std.type(v) == "object" then
local lines = ["{\n" + cindent]
+ std.join([",\n" + cindent],
[[indent + "\"" + k + "\": "
+ aux(v[k], path + [k], cindent + indent)]
for k in std.objectFields(v)])
+ ["\n" + cindent + "}"];
std.join("", lines);
aux(value, [], ""),

manifestYamlStream(value)::
if std.type(value) != "array" then
error "manifestYamlStream only takes arrays, got " + std.type(value)
else
"---\n" + std.join("\n---\n", [std.manifestJson(e) for e in value]) + '\n...\n',


manifestPython(o)::
if std.type(o) == "object" then
local fields = ["%s: %s" % [std.escapeStringPython(k), std.manifestPython(o[k])]
Expand Down
2 changes: 1 addition & 1 deletion test_suite/error.equality_function.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RUNTIME ERROR: Cannot test equality of functions
std.jsonnet:941:17-41 function <anonymous>
std.jsonnet:980:17-41 function <anonymous>
error.equality_function.jsonnet:17:1-32
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_array.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_array.jsonnet:18:18-31 thunk <array_element>
std.jsonnet:921:41-44 thunk <b>
std.jsonnet:921:33-44 function <anonymous>
std.jsonnet:921:33-44 function <aux>
std.jsonnet:924:29-44 function <anonymous>
std.jsonnet:925:21-32
std.jsonnet:960:41-44 thunk <b>
std.jsonnet:960:33-44 function <anonymous>
std.jsonnet:960:33-44 function <aux>
std.jsonnet:963:29-44 function <anonymous>
std.jsonnet:964:21-32
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_object.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_object.jsonnet:18:22-35 object <b>
std.jsonnet:935:62-65 thunk <b>
std.jsonnet:935:54-65 function <anonymous>
std.jsonnet:935:54-65 function <aux>
std.jsonnet:938:29-44 function <anonymous>
std.jsonnet:939:21-32
std.jsonnet:974:62-65 thunk <b>
std.jsonnet:974:54-65 function <anonymous>
std.jsonnet:974:54-65 function <aux>
std.jsonnet:977:29-44 function <anonymous>
std.jsonnet:978:21-32
8 changes: 4 additions & 4 deletions test_suite/error.invariant.equality.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Object assertion failed.
error.invariant.equality.jsonnet:17:10-14 thunk <object_assert>
std.jsonnet:935:54-57 thunk <a>
std.jsonnet:935:54-65 function <anonymous>
std.jsonnet:935:54-65 function <anonymous>
std.jsonnet:939:21-32
std.jsonnet:974:54-57 thunk <a>
std.jsonnet:974:54-65 function <anonymous>
std.jsonnet:974:54-65 function <anonymous>
std.jsonnet:978:21-32
26 changes: 26 additions & 0 deletions test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,30 @@ std.assertEqual(std.split("/foo/", "/"), ["", "foo", ""]) &&
std.assertEqual(std.splitLimit("foo/bar", "/", 1), ["foo", "bar"]) &&
std.assertEqual(std.splitLimit("/foo/", "/", 1), ["", "foo/"]) &&

std.assertEqual(std.manifestJsonEx({
x: [1, 2, 3, true, false, null, "string\nstring"],
y: { a: 1, b: 2, c: [1, 2] },
}, " ") + "\n", |||
{
"x": [
1,
2,
3,
true,
false,
null,
"string\nstring"
],
"y": {
"a": 1,
"b": 2,
"c": [
1,
2
]
}
}
|||
) &&

true