Native Functions
Tanka extends Jsonnet using native functions, offering additional functionality not yet available in the standard library.
To use them in your code, you need to access them using std.native
from the standard library:
{ someField: std.native('<name>')(<arguments>),}
std.native
takes the native function’s name as a string
argument and returns a function
, which is called using the second set of parentheses.
sha256
Section titled “sha256”Signature
Section titled “Signature”sha256(string str) string
sha256
computes the SHA256 sum of the given string.
Examples
Section titled “Examples”{ sum: std.native('sha256')('Hello, World!'),}
Evaluating with Tanka results in the JSON:
{ "sum": "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f"}
parseJson
Section titled “parseJson”Signature
Section titled “Signature”parseJson(string json) Object
parseJson
parses a json string and returns the respective Jsonnet type (Object
, Array
, etc).
Examples
Section titled “Examples”{ array: std.native('parseJson')('[0, 1, 2]'), object: std.native('parseJson')('{ "foo": "bar" }'),}
Evaluating with Tanka results in the JSON:
{ "array": [0, 1, 2], "object": { "foo": "bar" }}
parseYaml
Section titled “parseYaml”Signature
Section titled “Signature”parseYaml(string yaml) []Object
parseYaml
wraps yaml.Unmarshal
to convert a string of yaml document(s) into
a set of dicts. If yaml
only contains a single document, a single value array
will be returned.
Examples
Section titled “Examples”{ yaml: std.native('parseYaml')(||| --- foo: bar --- bar: baz |||),}
Evaluating with Tanka results in the JSON:
{ "yaml": [ { "foo": "bar" }, { "bar": "baz" } ]}
manifestJsonFromJson
Section titled “manifestJsonFromJson”Signature
Section titled “Signature”manifestJsonFromJson(string json, int indent) string
manifestJsonFromJson
reserializes JSON and allows to change the indentation.
Examples
Section titled “Examples”{ indentWithEightSpaces: std.native('manifestJsonFromJson')('{ "foo": { "bar": "baz" } }', 8),}
Evaluating with Tanka results in the JSON:
{ "indentWithEightSpaces": "{\n \"foo\": {\n \"bar\": \"baz\"\n }\n}\n"}
manifestYamlFromJson
Section titled “manifestYamlFromJson”Signature
Section titled “Signature”manifestYamlFromJson(string json) string
manifestYamlFromJson
serializes a JSON string as a YAML document.
Examples
Section titled “Examples”{ yaml: std.native('manifestYamlFromJson')('{ "foo": { "bar": "baz" } }'),}
Evaluating with Tanka results in the JSON:
{ "yaml": "foo:\n bar: baz\n"}
escapeStringRegex
Section titled “escapeStringRegex”Signature
Section titled “Signature”escapeStringRegex(string s) string
escapeStringRegex
escapes all regular expression metacharacters and returns a
regular expression that matches the literal text.
Examples
Section titled “Examples”{ escaped: std.native('escapeStringRegex')('"([0-9]+"'),}
Evaluating with Tanka results in the JSON:
{ "escaped": "\"\\(\\[0-9\\]\\+\""}
regexMatch
Section titled “regexMatch”Signature
Section titled “Signature”regexMatch(string regex, string s) boolean
regexMatch
returns whether the given string is matched by the given
RE2 regular expression.
Examples
Section titled “Examples”{ matched: std.native('regexMatch')('.', 'a'),}
Evaluating with Tanka results in the JSON:
{ "matched": true}
regexSubst
Section titled “regexSubst”Signature
Section titled “Signature”regexSubst(string regex, string src, string repl) string
regexSubst
replaces all matches of the re2 regular expression with the
replacement string.
Examples
Section titled “Examples”{ substituted: std.native('regexSubst')('p[^m]*', 'pm', 'poe'),}
Evaluating with Tanka results in the JSON:
{ "substituted": "poem"}