goplate

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

goplate - yet another go template library

Fields are enclosed in double curly braces like regular Go templates:

{{ fieldName }}

Nested data structures use dot separators.

{{ fieldName.subStructure.field }}

Template syntax is fairly obvious - the names match roughly the JSON equivalent when marshalled. Names are case insensitive so {{ fieldName }} and {{ fieldname }} resolves to the same field.

Map access is limited to map[string]string only and map access is also fairly obvious if you are familiar with Go:

{{ mapName["name"] }}

Transformation functions

There is a few transformations available in the templates. This marshals the entire field as a JSON structure and includes it in the output

{{ fieldName | json }}

This formats an int64 nanosecond field to a string representation of the data:

{{ timestampField | asTime }}

This formats a byte buffer to a hex string:

{{ byteField | asHex }}

Usage

This will build a template and execute it:

type testStruct struct {
    Field1 int
    Field2 int
}

// ...

tmpl, err := New(`{{ field1 }}/{{ field2 }}`).
    WithParameters(&testStruct{}).
    Build()
if err != nil {
    panic()
}

buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, &testStruct{Field1: 1, Field2: 2}) {
    panic()
}

// Print buffer
fmt.Println(buf.String())

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HexConversion

func HexConversion(v interface{}) []byte

HexConversion returns a hex-encoded string

func Int64ToDateString

func Int64ToDateString(v interface{}) []byte

Int64ToDateString returns the value as a RFC3339 date string. If the type isn't an int64 or *wrapperspb.Int64Value type it will return a blank

Types

type Builder

type Builder struct {
	TemplateString string
	Transforms     TransformFunctionMap
	Parameters     interface{}
}

Builder is an type to build and configure template instances.

func New

func New(templateString string) *Builder

New creates a new template builder

func (*Builder) Build

func (t *Builder) Build() (*Template, error)

Build builds and validates the template

func (*Builder) WithJSONMarshaler

func (t *Builder) WithJSONMarshaler(marshaler JSONMarshaler) *Builder

func (*Builder) WithParameters

func (t *Builder) WithParameters(params interface{}) *Builder

WithParameters sets the parameter type used for the template. When the structure is built it will use this parameter struct to validate the template

func (*Builder) WithTransforms

func (t *Builder) WithTransforms(transformMap TransformFunctionMap) *Builder

WithTransforms modifies the transform function map for the template. The transform function map is used to apply transformations to fields in the merged data structure. They are simple strings with no parameters and the key to the

type JSONMarshaler

type JSONMarshaler interface {
	Marshal(obj interface{}) ([]byte, error)
}

func DefaultMarshaler

func DefaultMarshaler() JSONMarshaler

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template is the main templating engine

func (*Template) Execute

func (t *Template) Execute(writer io.Writer, params interface{}) error

Execute writes the expanded template to the supplied io.Writer

func (*Template) Validate

func (t *Template) Validate() (bool, []string)

Validate validates the template tags

type TransformFunc

type TransformFunc func(interface{}) []byte

TransformFunc is the transformation function for template expressions

func DefaultJSONTransformFunc

func DefaultJSONTransformFunc(marshaler JSONMarshaler) TransformFunc

DefaultJSONTransformFunc is the default JSON transform function

type TransformFunctionMap

type TransformFunctionMap map[string]TransformFunc

TransformFunctionMap is the function transform map for the templates.

Jump to

Keyboard shortcuts

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