这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

On JSON notification issues #2197

@mvuets

Description

@mvuets

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.

  1. Keep composing JSON by hand as text, but provide with a JSON escaping helper that would be a middle ground between built-in js and Bosun's json. 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 }}
}
`
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions