Documentation
¶
Overview ¶
Package jsontemplate provides a library for transforming JSON events using a minimal template.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidTemplate = errors.New("invalid JSON format template")
)
Functions ¶
func JSONEncoder ¶ added in v0.2.0
JSONEncoder encodes the given value v to the given writer wr using the JSON encoding format.
It returns any error encountered during encoding.
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document holds JSON content.
type Encoder ¶ added in v0.2.0
Encoder encodes the given value v to the given writer wr.
It returns any error encountered during encoding.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
NewTemplate parses the given template content into a fasttemplate Template and returns it.
func NewTemplate ¶
NewTemplate parses the given template content into a fasttemplate Template and returns it.
Parameters:
content: The template content/string to parse.
Returns:
*Template: The parsed Template. error: Any error encountered parsing the template.
Functionality: This function parses the given template string into a fasttemplate Template which can then be executed against JSON events. It validates the template string is valid JSON before parsing and returns an error if invalid.
ExecuteToString executes the template against the given JSON event and returns the result as a string.
func (*Template) Execute ¶
Execute executes the template against the given JSON event and writes the result to the given writer. It returns the number of bytes written and any error.
func (*Template) ExecuteToString ¶
ExecuteToString executes the template against the given JSON event and returns the result as a string.
Example ¶
package main import ( "fmt" "github.com/wolfeidau/jsontemplate" ) func main() { tpl, _ := jsontemplate.NewTemplate(`{"name": "${msg.name}","age": "${msg.age}","cyclist": "${msg.cyclist}"}`) res, _ := tpl.ExecuteToString([]byte(`{"msg":{"name":"markw","age":23,"cyclist":true}}`)) fmt.Println(res) }
Output: {"name": "markw","age": 23,"cyclist": true}
Example (Encoded) ¶
package main import ( "fmt" "github.com/wolfeidau/jsontemplate" ) func main() { tpl, _ := jsontemplate.NewTemplate(`{"msg": "${msg;escape}"}`) res, _ := tpl.ExecuteToString([]byte(`{"msg":{"name":"markw","age":23,"cyclist":true}}`)) fmt.Println(res) }
Output: {"msg": "{\"name\":\"markw\",\"age\":23,\"cyclist\":true}"}