-
Notifications
You must be signed in to change notification settings - Fork 492
On JSON notification issues #2197
Description
Hi. I've been asked to look into an issue of making JSON notifications (for instance to POST JSON to PD on an incident ack, etc.). My understanding is that composing JSON using a text template engine is inherently incorrect: it is clumsy and error-prone.
From this pull request #2135 and @kylebrandt's comment I see that there is an effort to improve the situation. There are three helpers makeSlice, makeMap and | json that could be used to assemble and serialise a data structure and avoid dealing with JSON encoding issues. Yet they don't seem to quite cut it.
One issue in particular is related to a complex data structure provided to, say, actionBodyAck and limited capabilites of Go's text/template package: how to traverse multi-dimensional data? .States is an array, so I presume multiple accidents may be acknowledged at once. Here is where I fail to come up with an appropriate way to get IDs across all .States items.
I can think of two potential enhancements.
- Keep composing JSON by hand as text, but provide with a JSON escaping helper that would be a middle ground between built-in
jsand Bosun'sjson. It would encode given values (both scalar and structures) in such a way that it can be always safely used in place of an object field value:
jsonBody = `{
"key": {{ V "$key" | json_val }},
"accident_nr": {{ len .States | json_val }},
"accident_ids": [ {{ range .States }}{{ .Id }},{{ end }} ],
"summary": {{ .Subject | json_val }}
}
`
- Provide templates with another helper that could help querying data, return it in a structured way which then can be serialised with
json. Something alike jq. For example:
jsonBody = `
{{- $accident_nr := len .States -}}
{{- makeMap "key" (V "$key") "accident_nr" $accident_nr "summary" .Subject "accident_ids" (jq "[.States[].Id]") | json -}}
`
I would be glad to hear what's your vision on improving support for JSON notifications? Thanks.