kube/prototext.libsonnet: implement manifestProtoText
This hairy little ball of recursive object manifestation allows emitting
prototext directly from jsonnet.
Change-Id: I8237c629862cfcdf32ca250fba9eda9926c4f9b4
diff --git a/kube/prototext.libsonnet b/kube/prototext.libsonnet
new file mode 100644
index 0000000..419a511
--- /dev/null
+++ b/kube/prototext.libsonnet
@@ -0,0 +1,76 @@
+// An untyped prototext marshaller. Like std.manifestJson, but when you want to
+// emit proto-but-not-really.
+//
+// This supports recursively nested objects/arrays with string, number and bool
+// leaves. All hidden object fields are ignored. The given value must be an
+// object.
+//
+// One unintuitive aspect of proto(text) is that you cannot have
+// multidimensional arrays, ie. the following is forbidden:
+//
+// manifestProtoText({foo: [[1, 2]]})
+//
+// RUNTIME ERROR: manifestProtoText: at .foo[0]: double nested arrays are
+// unsupported by prototext
+
+{
+ local top = self,
+
+ // manifestProtoText takes an object and returns a serialized prototext
+ // representation of it. If indentation is "", then a single-line prototext
+ // will be emitted, otherwise a multi-line prototext will be emitted with
+ // each object depth being indented by the indentation value.
+ manifestProtoText:: function(value, indentation=" ") top.recurse(value, indentation, 0, [""]),
+
+ // Available as std.repeat in jsonnet 0.15
+ repeat:: function(str, count) if count <= 0 then "" else (str + top.repeat(str, count-1)),
+
+ emit:: function(str, indentation, nindent) (
+ top.repeat(indentation, nindent) + str + (if indentation == "" then " " else "\n")
+ ),
+
+ fatal:: function(str, path) error "manifestProtoText: at %s: %s" % [std.join("", path), str],
+
+ // objectField emits a rendered `k: v` object field, potentially recursing
+ // back into recurse for printing the value of object. The value must not
+ // be an array, these are handled/flattened by recurse.
+ objectField:: function(name, value, indentation, nindent, path) (
+ if std.isObject(value) then (
+ top.emit("%s <" % [name], indentation, nindent) +
+ top.recurse(value, indentation, nindent+1, path) +
+ top.emit(">", indentation, nindent)
+ ) else if std.isArray(value) then (
+ top.fatal("double nested arrays are unsupported by prototext", path)
+ ) else if std.isFunction(value) then (
+ top.fatal("cannot manifest functions", path)
+ ) else if std.isBoolean(value) then (
+ top.emit("%s: %s" % [name, if value then "true" else "false"], indentation, nindent)
+ ) else if std.isNumber(value) then (
+ top.emit("%s: %s" % [name, std.toString(value)], indentation, nindent)
+ ) else if std.isString(value) then (
+ // Atempt to properly escape strings via JSON manifestation. Not
+ // entirely sure this always works.
+ top.emit("%s: %s" % [name, std.manifestJson(value)], indentation, nindent)
+ ) else (
+ top.fatal("unknown type %s" % [std.type(value)], path)
+ )
+ ),
+
+ // recurse returns the string representation of an object as prototext.
+ recurse:: function(value, indentation, nindent, path) (
+ if std.isObject(value) then std.join("", [
+ (
+ local field = value[fieldName];
+ if std.isArray(field) then std.join("", std.mapWithIndex(function(i, el) (
+ local newPath = path + ["."+fieldName, "[%d]" % [i]];
+ top.objectField(fieldName, el, indentation, nindent, newPath)
+ ), field)) else (
+ local newPath = path + ["."+fieldName];
+ top.objectField(fieldName, field, indentation, nindent, newPath)
+ )
+ )
+ for fieldName in std.objectFields(value)
+ ]) else
+ top.fatal("manifestProtoText can only manifest objects", path)
+ ),
+}