renderer

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2022 License: Apache-2.0 Imports: 27 Imported by: 4

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CidrHost added in v0.3.0

func CidrHost(hostnum int, prefix interface{}) (*net.IP, error)

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

func CidrNetmask(prefix interface{}) (*net.IP, error)

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

func CidrSubnetSizes(args ...interface{}) ([]*net.IPNet, error)

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

func CidrSubnets(newbits int, prefix interface{}) ([]*net.IPNet, error)

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

func ExtraFunctions() template.FuncMap

ExtraFunctions provides additional template functions to the standard (text/template) ones

func FromJSON added in v0.1.4

func FromJSON(unmarshallable string) (interface{}, error)

FromJSON is a template function, that unmarshalls JSON string to a map

func FromYAML added in v0.1.4

func FromYAML(unmarshallable string) (interface{}, error)

FromYAML is a template function, that unmarshalls YAML string to a map

func Gzip added in v0.0.4

func Gzip(input interface{}) (string, error)

Gzip compresses the input using gzip algorithm

func JSONPath added in v0.1.4

func JSONPath(expression string, marshallable interface{}) (interface{}, error)

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

func MergeFunctions(dst *template.FuncMap, src template.FuncMap) error

MergeFunctions merges two template.FuncMap instances, overrides if necessary

func N added in v0.1.5

func N(start, end int) []int

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

func NetFunctions() template.FuncMap

NetFunctions provides additional template functions to the standard (text/template) ones

func ToYAML added in v0.1.4

func ToYAML(marshallable interface{}) (string, error)

ToYAML is a template function, it turns a marshallable structure into a YAML fragment

func Ungzip added in v0.0.4

func Ungzip(input interface{}) (string, error)

Ungzip un-compresses the input using gzip algorithm

func WithCryptFunctions added in v0.0.6

func WithCryptFunctions() func(*config.Config)

WithCryptFunctions mutates Renderer configuration by merging the Crypt template functions

func WithDelim added in v0.0.6

func WithDelim(left, right string) func(*config.Config)

WithDelim mutates Renderer configuration by replacing the left and right delimiters

func WithExtraFunctions added in v0.0.6

func WithExtraFunctions() func(*config.Config)

WithExtraFunctions mutates Renderer configuration by merging the custom template functions

func WithFunctions added in v0.0.6

func WithFunctions(extraFunctions template.FuncMap) func(*config.Config)

WithFunctions mutates Renderer configuration by replacing the template functions

func WithMoreFunctions added in v0.0.6

func WithMoreFunctions(moreFunctions template.FuncMap) func(*config.Config)

WithMoreFunctions mutates Renderer configuration by merging the given template functions,

func WithMoreParameters added in v0.0.8

func WithMoreParameters(extraParams ...map[string]interface{}) func(*config.Config)

WithMoreParameters mutates Renderer configuration by merging the given template parameters

func WithNetFunctions added in v0.3.0

func WithNetFunctions() func(*config.Config)

WithNetFunctions mutates Renderer configuration by merging the custom template functions

func WithOptions added in v0.0.6

func WithOptions(options ...string) func(*config.Config)

WithOptions mutates Renderer configuration by replacing the template functions

func WithParameters added in v0.0.6

func WithParameters(parameters map[string]interface{}) func(*config.Config)

WithParameters mutates Renderer configuration by replacing all template parameters

func WithSprigFunctions added in v0.0.6

func WithSprigFunctions() func(*config.Config)

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

func New

func New(configurators ...func(*config.Config)) Renderer

New creates a new renderer with the specified parameters and zero or more options

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL