You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried using the printf "%q" function in go to encode double-quoted strings (like this: {{ printf "%q" .Annotations.description }}). This renders correct JSON strings in most cases, but it's not perfect.
I wasn't able to figure out how to use the jq template function. alertmanager-webhook-proxy crashes when I try to load the template.
I tried {{ jq .Labels.example }} and {{ .Labels.example | jq }}.
Similarities between Go string literals and JSON strings:
Double quotes delimiters.
Two character escape sequences \\, \", \n, \t, and others.
Differences:
Go literal strings have \nnn (octal), \xFF, \uFFFF and \UFFFFFFFF character sequences.
JSON strings only have \uFFFF and a few 2-character sequences.
For example, in Go printf "%q" a null character will be represented as \x00, but JSON would require \u0000.
In JSON, all control characters U+0000 to U+001F must be escaped. Therefore, any control character that would not be represented as a 2-character escape sequence would produce bad JSON and cause an error.
Resolution:
I ended up re-writing in Python (because I am more familiar with that) so that I can use a JSON serialiser instead of a text template engine. This way, at least the JSON syntax is always correct.
The text was updated successfully, but these errors were encountered:
I tried using the
printf "%q"
function in go to encode double-quoted strings (like this:{{ printf "%q" .Annotations.description }}
). This renders correct JSON strings in most cases, but it's not perfect.I wasn't able to figure out how to use the
jq
template function.alertmanager-webhook-proxy
crashes when I try to load the template.I tried
{{ jq .Labels.example }}
and{{ .Labels.example | jq }}
.Reference documentation:
https://pkg.go.dev/text/template#hdr-Functions
https://pkg.go.dev/fmt#hdr-Printing
https://datatracker.ietf.org/doc/html/rfc8259#section-7
https://go.dev/ref/spec#String_literals
Similarities between Go string literals and JSON strings:
\\
,\"
,\n
,\t
, and others.Differences:
\nnn
(octal),\xFF
,\uFFFF
and\UFFFFFFFF
character sequences.\uFFFF
and a few 2-character sequences.For example, in Go
printf "%q"
a null character will be represented as\x00
, but JSON would require\u0000
.In JSON, all control characters
U+0000
toU+001F
must be escaped. Therefore, any control character that would not be represented as a 2-character escape sequence would produce bad JSON and cause an error.Resolution:
I ended up re-writing in Python (because I am more familiar with that) so that I can use a JSON serialiser instead of a text template engine. This way, at least the JSON syntax is always correct.
The text was updated successfully, but these errors were encountered: