Documentation
¶
Overview ¶
Package jsonplate renders structured JSON payload templates ("jsonplates") by resolving embedded path references against a data context.
A jsonplate is ordinary JSON in which any object of the form
{"$ref": "path.into.data"}
is a reference: at render time it is replaced by the value found at that path in the supplied data context. Every other value is a literal and is copied through unchanged. Path resolution is delegated to queryfy's query language (field access, nested access, array indexing: "a.b", "a.b[0].c"), so this package owns only the structural substitution walk — queryfy does the traversal.
Validation happens at definition time (Validate), resolution at render time (Render). A reference whose path is absent from the data resolves to JSON null rather than erroring, so a partially-populated context degrades gracefully instead of failing a whole render.
Example:
plate := []byte(`{"asset":{"$ref":"current"},"out":{"$ref":"affected[0].ref.id"}}`)
data := map[string]interface{}{
"current": "InService",
"affected": []interface{}{map[string]interface{}{"ref": map[string]interface{}{"id": 123}}},
}
out, _ := jsonplate.Render(plate, data)
// out == {"asset":"InService","out":123}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
Render resolves a jsonplate against the given data context and returns the rendered JSON. plate must be a valid JSON document. data is the context that reference paths are resolved against (typically an event's data map).
A reference to a path that does not exist in data resolves to JSON null.
func Validate ¶
Validate checks that plate is well-formed: valid JSON, and every reference object ({"$ref": ...}) carries a string path. It does not check that the referenced paths exist (that is only knowable at render time against real data); it checks the template's own structure. Intended to be called when an event def is created, so a malformed jsonplate is rejected up front rather than at firing time.
Types ¶
This section is empty.