blob: 419a511c8638e19e89491d2642197a4b1ff3cdad [file] [log] [blame]
Serge Bazanski5d67d0c2021-02-06 16:43:33 +00001// An untyped prototext marshaller. Like std.manifestJson, but when you want to
2// emit proto-but-not-really.
3//
4// This supports recursively nested objects/arrays with string, number and bool
5// leaves. All hidden object fields are ignored. The given value must be an
6// object.
7//
8// One unintuitive aspect of proto(text) is that you cannot have
9// multidimensional arrays, ie. the following is forbidden:
10//
11// manifestProtoText({foo: [[1, 2]]})
12//
13// RUNTIME ERROR: manifestProtoText: at .foo[0]: double nested arrays are
14// unsupported by prototext
15
16{
17 local top = self,
18
19 // manifestProtoText takes an object and returns a serialized prototext
20 // representation of it. If indentation is "", then a single-line prototext
21 // will be emitted, otherwise a multi-line prototext will be emitted with
22 // each object depth being indented by the indentation value.
23 manifestProtoText:: function(value, indentation=" ") top.recurse(value, indentation, 0, [""]),
24
25 // Available as std.repeat in jsonnet 0.15
26 repeat:: function(str, count) if count <= 0 then "" else (str + top.repeat(str, count-1)),
27
28 emit:: function(str, indentation, nindent) (
29 top.repeat(indentation, nindent) + str + (if indentation == "" then " " else "\n")
30 ),
31
32 fatal:: function(str, path) error "manifestProtoText: at %s: %s" % [std.join("", path), str],
33
34 // objectField emits a rendered `k: v` object field, potentially recursing
35 // back into recurse for printing the value of object. The value must not
36 // be an array, these are handled/flattened by recurse.
37 objectField:: function(name, value, indentation, nindent, path) (
38 if std.isObject(value) then (
39 top.emit("%s <" % [name], indentation, nindent) +
40 top.recurse(value, indentation, nindent+1, path) +
41 top.emit(">", indentation, nindent)
42 ) else if std.isArray(value) then (
43 top.fatal("double nested arrays are unsupported by prototext", path)
44 ) else if std.isFunction(value) then (
45 top.fatal("cannot manifest functions", path)
46 ) else if std.isBoolean(value) then (
47 top.emit("%s: %s" % [name, if value then "true" else "false"], indentation, nindent)
48 ) else if std.isNumber(value) then (
49 top.emit("%s: %s" % [name, std.toString(value)], indentation, nindent)
50 ) else if std.isString(value) then (
51 // Atempt to properly escape strings via JSON manifestation. Not
52 // entirely sure this always works.
53 top.emit("%s: %s" % [name, std.manifestJson(value)], indentation, nindent)
54 ) else (
55 top.fatal("unknown type %s" % [std.type(value)], path)
56 )
57 ),
58
59 // recurse returns the string representation of an object as prototext.
60 recurse:: function(value, indentation, nindent, path) (
61 if std.isObject(value) then std.join("", [
62 (
63 local field = value[fieldName];
64 if std.isArray(field) then std.join("", std.mapWithIndex(function(i, el) (
65 local newPath = path + ["."+fieldName, "[%d]" % [i]];
66 top.objectField(fieldName, el, indentation, nindent, newPath)
67 ), field)) else (
68 local newPath = path + ["."+fieldName];
69 top.objectField(fieldName, field, indentation, nindent, newPath)
70 )
71 )
72 for fieldName in std.objectFields(value)
73 ]) else
74 top.fatal("manifestProtoText can only manifest objects", path)
75 ),
76}