Documentation
¶
Overview ¶
Package renderer implements data-driven templates for generating textual output
The renderer extends the standard golang text/template and sprig functions.
Templates are executed by applying them to a data structure (configuration). Values in the template refer to elements of the data structure (typically a field of a struct or a key in a map).
Actions can be combined using UNIX-like pipelines.
The input text for a template is UTF-8-encoded text in any format.
See renderer.ExtraFunctions for our custom functions.
Detailed documentation on the syntax and available functions can be found here:
Index ¶
- func CidrHost(hostnum int, prefix interface{}) (*net.IP, error)
- func CidrNetmask(prefix interface{}) (*net.IP, error)
- func CidrSubnetSizes(args ...interface{}) ([]*net.IPNet, error)
- func CidrSubnets(newbits int, prefix interface{}) ([]*net.IPNet, error)
- func ExtraFunctions() template.FuncMap
- func FromJSON(unmarshallable string) (interface{}, error)
- func FromYAML(unmarshallable string) (interface{}, error)
- func Gzip(input interface{}) (string, error)
- func JSONPath(expression string, marshallable interface{}) (interface{}, error)
- func MergeFunctions(dst *template.FuncMap, src template.FuncMap) error
- func N(start, end int) []int
- func NetFunctions() template.FuncMap
- func ToYAML(marshallable interface{}) (string, error)
- func Ungzip(input interface{}) (string, error)
- func WithCryptFunctions() func(*config.Config)
- func WithDelim(left, right string) func(*config.Config)
- func WithExtraFunctions() func(*config.Config)
- func WithFunctions(extraFunctions template.FuncMap) func(*config.Config)
- func WithMoreFunctions(moreFunctions template.FuncMap) func(*config.Config)
- func WithMoreParameters(extraParams ...map[string]interface{}) func(*config.Config)
- func WithNetFunctions() func(*config.Config)
- func WithOptions(options ...string) func(*config.Config)
- func WithParameters(parameters map[string]interface{}) func(*config.Config)
- func WithSprigFunctions() func(*config.Config)
- type Renderer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CidrHost ¶ added in v0.3.0
CidrHost calculates a full host IP address within a given IP network address prefix.
Example (Simple) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
)
func main() {
tmpl := `
{{ cidrHost 16 "10.12.127.0/20" }}
{{ cidrHost 268 "10.12.127.0/20" }}
{{ cidrHost 34 "fd00:fd12:3456:7890:00a2::/72" }}
{{ "10.12.127.0/20" | cidrHost 16 }}
`
result, err := renderer.New(
renderer.WithNetFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: 10.12.112.16 10.12.113.12 fd00:fd12:3456:7890::22 10.12.112.16
func CidrNetmask ¶ added in v0.3.0
CidrNetmask converts an IPv4 address prefix given in CIDR notation into a subnet mask address.
Example (Simple) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
)
func main() {
tmpl := `
{{ cidrNetmask "10.0.0.0/12" }}
`
result, err := renderer.New(
renderer.WithNetFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: 255.240.0.0
func CidrSubnetSizes ¶ added in v0.3.0
CidrSubnetSizes calculates a sequence of consecutive subnet prefixes that may be of different prefix lengths under a common base prefix.
Example (Simple) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
)
func main() {
tmpl := `
{{ range $k, $v := cidrSubnetSizes 4 4 8 4 "10.1.0.0/16" }}
{{ $k }} {{ $v }}{{ end }}
{{ range $k, $v := cidrSubnetSizes 16 16 16 32 "fd00:fd12:3456:7890::/56" }}
{{ $k }} {{ $v }}{{ end }}
`
result, err := renderer.New(
renderer.WithNetFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: 0 10.1.0.0/20 1 10.1.16.0/20 2 10.1.32.0/24 3 10.1.48.0/20 0 fd00:fd12:3456:7800::/72 1 fd00:fd12:3456:7800:100::/72 2 fd00:fd12:3456:7800:200::/72 3 fd00:fd12:3456:7800:300::/88
func CidrSubnets ¶ added in v0.3.0
CidrSubnets calculates a subnet address within a given IP network address prefix.
Example (Simple) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
)
func main() {
tmpl := `
{{ index (cidrSubnets 2 "10.0.0.0/16") 0 }}
{{ index ("10.0.0.0/16" | cidrSubnets 2) 1 }}
{{ range cidrSubnets 3 "10.0.0.0/16" }}
{{ . }}{{ end }}
`
result, err := renderer.New(
renderer.WithNetFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: 10.0.0.0/18 10.0.64.0/18 10.0.0.0/19 10.0.32.0/19 10.0.64.0/19 10.0.96.0/19 10.0.128.0/19 10.0.160.0/19 10.0.192.0/19 10.0.224.0/19
func ExtraFunctions ¶ added in v0.0.6
ExtraFunctions provides additional template functions to the standard (text/template) ones
func FromJSON ¶ added in v0.1.4
FromJSON is a template function, that unmarshalls JSON string to a map
func FromYAML ¶ added in v0.1.4
FromYAML is a template function, that unmarshalls YAML string to a map
func JSONPath ¶ added in v0.1.4
JSONPath is a template function, that evaluates JSONPath expression against a data structure and returns a list of results
Example (Array) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
"github.com/VirtusLab/render/renderer/parameters"
)
func main() {
json := `["Good Morning", "Hello World!"]`
expression := "{$[1]}"
params := parameters.Parameters{
"json": json,
"expression": expression,
}
tmpl := `
{{ .json | fromJson | jsonPath .expression }}
`
result, err := renderer.New(
renderer.WithParameters(params),
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: Hello World!
Example (Multi) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
"github.com/VirtusLab/render/renderer/parameters"
)
func main() {
yaml := `---
data:
en: "Hello World!"
---
data:
pl: "Witaj Świecie!"
`
expression := "{$[*].data}"
params := parameters.Parameters{
"yaml": yaml,
"expression": expression,
}
tmpl := `
{{- range $m := .yaml | fromYaml | jsonPath .expression }}
{{ range $k, $v := $m }}{{ $k }} {{ $v }}{{ end }}
{{- end }}
`
result, err := renderer.New(
renderer.WithParameters(params),
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: en Hello World! pl Witaj Świecie!
Example (Multi_nested) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
"github.com/VirtusLab/render/renderer/parameters"
)
func main() {
yamlWithJson := `---
data:
en.json: |2
{
"welcome":{
"message":["Good Morning", "Hello World!"]
}
}
---
data:
pl.json: |2
{
"welcome":{
"message":["Dzień dobry", "Witaj Świecie!"]
}
}
`
expression := "{$[*].data.*}"
expression2 := "{$.welcome.message[1]}"
params := parameters.Parameters{
"yamlWithJson": yamlWithJson,
"expression": expression,
"expression2": expression2,
}
tmpl := `
{{- range $r := .yamlWithJson | fromYaml | jsonPath .expression }}
{{ $r | fromJson | jsonPath $.expression2 }}
{{- end }}
`
result, err := renderer.New(
renderer.WithParameters(params),
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: Hello World! Witaj Świecie!
Example (Simple) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
"github.com/VirtusLab/render/renderer/parameters"
)
func main() {
json := `{
"welcome":{
"message":["Good Morning", "Hello World!"]
}
}`
expression := "{$.welcome.message[1]}"
params := parameters.Parameters{
"json": json,
"expression": expression,
}
tmpl := `{{ .json | fromJson | jsonPath .expression }}`
result, err := renderer.New(
renderer.WithParameters(params),
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: Hello World!
Example (Wildcard) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
"github.com/VirtusLab/render/renderer/parameters"
)
func main() {
json := `{
"welcome":{
"message":["Good Morning", "Hello World!"]
}
}`
expression := "{$.welcome.message[*]}"
params := parameters.Parameters{
"json": json,
"expression": expression,
}
tmpl := `
{{- range $m := .json | fromJson | jsonPath .expression }}
{{ $m }}
{{- end }}
`
result, err := renderer.New(
renderer.WithParameters(params),
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: Good Morning Hello World!
Example (Yaml) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
"github.com/VirtusLab/render/renderer/parameters"
)
func main() {
yaml := `---
welcome:
message:
- "Good Morning"
- "Hello World!"
`
expression := "{$.welcome.message[1]}"
params := parameters.Parameters{
"yaml": yaml,
"expression": expression,
}
tmpl := `{{ .yaml | fromYaml | jsonPath .expression }}`
result, err := renderer.New(
renderer.WithParameters(params),
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: Hello World!
func MergeFunctions ¶ added in v0.0.6
MergeFunctions merges two template.FuncMap instances, overrides if necessary
func N ¶ added in v0.1.5
N returns a slice of integers form the given start to end (inclusive)
Example (Empty) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
)
func main() {
tmpl := `
{{ range $i := n 0 0 }}{{ $i }} {{ end }}
`
result, err := renderer.New(
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: 0
Example (Simple) ¶
package main
import (
"fmt"
"github.com/VirtusLab/render/renderer"
)
func main() {
tmpl := `
{{ range $i := n 0 10 }}{{ $i }} {{ end }}
`
result, err := renderer.New(
renderer.WithExtraFunctions(),
).Render(tmpl)
if err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
Output: 0 1 2 3 4 5 6 7 8 9 10
func NetFunctions ¶ added in v0.3.0
NetFunctions provides additional template functions to the standard (text/template) ones
func ToYAML ¶ added in v0.1.4
ToYAML is a template function, it turns a marshallable structure into a YAML fragment
func WithCryptFunctions ¶ added in v0.0.6
WithCryptFunctions mutates Renderer configuration by merging the Crypt template functions
func WithDelim ¶ added in v0.0.6
WithDelim mutates Renderer configuration by replacing the left and right delimiters
func WithExtraFunctions ¶ added in v0.0.6
WithExtraFunctions mutates Renderer configuration by merging the custom template functions
func WithFunctions ¶ added in v0.0.6
WithFunctions mutates Renderer configuration by replacing the template functions
func WithMoreFunctions ¶ added in v0.0.6
WithMoreFunctions mutates Renderer configuration by merging the given template functions,
func WithMoreParameters ¶ added in v0.0.8
WithMoreParameters mutates Renderer configuration by merging the given template parameters
func WithNetFunctions ¶ added in v0.3.0
WithNetFunctions mutates Renderer configuration by merging the custom template functions
func WithOptions ¶ added in v0.0.6
WithOptions mutates Renderer configuration by replacing the template functions
func WithParameters ¶ added in v0.0.6
WithParameters mutates Renderer configuration by replacing all template parameters
func WithSprigFunctions ¶ added in v0.0.6
WithSprigFunctions mutates Renderer configuration by merging the Sprig template functions
Types ¶
type Renderer ¶
type Renderer interface {
base.Renderer
Clone(configurators ...func(*config.Config)) Renderer
FileRender(inputPath, outputPath string) error
DirRender(inputDir, outputDir string) error
NestedRender(args ...interface{}) (string, error)
ReadFile(file string) (string, error)
}
Renderer allows for parameterised text template rendering
Directories
¶
| Path | Synopsis |
|---|---|
|
Package parameters defines data structure for the data-driven renderer Parameters is a tree structure and can be created from a YAML files or 'key=value' pairs.
|
Package parameters defines data structure for the data-driven renderer Parameters is a tree structure and can be created from a YAML files or 'key=value' pairs. |