template

package
v0.0.0-...-7c1ae57 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegistryTestFunc

func RegistryTestFunc(name string) func(*ResolvedValue, Options, language.Tag) (*ResolvedValue, error)

RegistryTestFunc is the implementation of the ":test:function", ":test:format" and "test:select". ":test:function" is both functions - ":test:format" and ":test:select".

Types

type Func

type Func func(input *ResolvedValue, options Options, locale language.Tag) (output *ResolvedValue, err error)

type Option

type Option func(t *Template)

Option is a template option.

func WithFunc

func WithFunc(name string, f Func) Option

WithFunc adds a single function to function registry.

Example:

WithFunc("bar", f)     // function ":bar"
WithFunc("foo:bar", f) // function with namespace ":foo:bar"

func WithFuncs

func WithFuncs(reg Registry) Option

WithFuncs adds functions to function registry.

Example:

WithFuncs(Registry{
	"bar": f,     // function ":bar"
	"foo:bar": f, // function with namespace ":foo:bar"
})

func WithLocale

func WithLocale(locale language.Tag) Option

WithLocale adds locale information to the template.

type Options

type Options map[string]*ResolvedValue

Options are a possible options for the function.

func (Options) GetInt

func (o Options) GetInt(name string, fallback int, validate ...Validate[int]) (int, error)

GetInt returns the value by name. If the value is not found, returns the fallback value. If the value is not in allowed list, return error.

func (Options) GetString

func (o Options) GetString(name, fallback string, validate ...Validate[string]) (string, error)

GetString returns the value by name. If the value is not found, returns the fallback value. If the value is not in allowed list, return error.

type Registry

type Registry map[string]Func

func NewRegistry

func NewRegistry() Registry

NewRegistry returns a new registry with default functions.

type ResolvedValue

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

ResolvedValue keeps the result of the Expression resolution with optionally defined format() and selectKey() functions for Format and Select contexts.

func NewResolvedValue

func NewResolvedValue(value any, options ...ResolvedValueOpt) *ResolvedValue

NewResolvedValue creates a new variable of type *ResolvedValue. If value is already *ResolvedValue, the optional format() and selectKey() are applied to it.

func (*ResolvedValue) String

func (r *ResolvedValue) String() string

String returns formatted string value.

type ResolvedValueOpt

type ResolvedValueOpt func(*ResolvedValue)

ResolvedValueOpt is a function to apply to the ResolvedValue.

func WithFormat

func WithFormat(format func() string) ResolvedValueOpt

WithFormat applies a formatting function to the ResolvedValue returned by Func. The formatting function is called in the formatting context.

func WithSelectKey

func WithSelectKey(selectKey func(keys []string) string) ResolvedValueOpt

WithSelectKey applies a selection function to the ResolvedValue returned by Func. The selection function is called in the selection context.

Keys exclude catch all key "*". If keys contain "*", it is string literal and is NOT catch all key.

type Template

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

Template represents a MessageFormat2 template.

Example (ComplexMessage)
package main

import (
	"fmt"
	"os"

	"go.expect.digital/mf2"
	"go.expect.digital/mf2/template"
	"golang.org/x/text/language"
)

func main() {
	// Define a MF2 string.
	const input = `.local $age = { 42 }
.input { $color :color style=RGB}
{{John is { $age } years old and his favorite color is { $color }.}}`

	color := func(value *template.ResolvedValue, options template.Options, _ language.Tag) (*template.ResolvedValue, error) { //nolint:lll
		errorf := func(format string, args ...any) (*template.ResolvedValue, error) {
			return nil, fmt.Errorf("exec color function: "+format, args...)
		}

		if value == nil {
			return errorf("input is required: %w", mf2.ErrBadOperand)
		}

		color := value.String()

		format := func() string {
			if len(options) == 0 {
				return color
			}

			style, err := options.GetString("style", "RGB")
			if err != nil {
				return color
			}

			var result string

			switch style {
			case "RGB":
				switch color {
				case "red":
					result = "255,0,0"
				case "green":
					result = "0,255,0"
				case "blue":
					result = "0,0,255"
				}
			case "HEX": // Other Implementations
			case "HSL": // Other Implementations
			}

			return result
		}

		return template.NewResolvedValue(color, template.WithFormat(format)), nil
	}

	// Parse template.
	t, err := template.New(template.WithFunc("color", color)).Parse(input)
	if err != nil {
		panic(err)
	}

	// Execute the template.
	if err = t.Execute(os.Stdout, map[string]any{"color": "red"}); err != nil {
		panic(err)
	}

}
Output:

John is 42 years old and his favorite color is 255,0,0.
Example (PlainText)
package main

import (
	"os"

	"go.expect.digital/mf2/template"
)

func main() {
	// Define a MF2 string.
	const input = "Hello World!"

	// Parse template.
	t, err := template.New().Parse(input)
	if err != nil {
		panic(err)
	}

	if err := t.Execute(os.Stdout, nil); err != nil {
		panic(err)
	}

}
Output:

Hello World!
Example (SimpleMessage)
package main

import (
	"os"

	"go.expect.digital/mf2/template"
)

func main() {
	// Define a MF2 string.
	const input = "Today is { $degrees :number signDisplay=always } degrees outside."

	// Parse template.
	t, err := template.New().Parse(input)
	if err != nil {
		panic(err)
	}

	// Execute the template.
	if err = t.Execute(os.Stdout, map[string]any{"degrees": 15}); err != nil {
		panic(err)
	}

}
Output:

Today is +15 degrees outside.

func New

func New(options ...Option) *Template

New returns a new Template.

func (*Template) Execute

func (t *Template) Execute(w io.Writer, input map[string]any) error

Execute writes the result of the template to the given writer.

func (*Template) Parse

func (t *Template) Parse(input string) (*Template, error)

Parse parses the MessageFormat2 string and returns the template.

func (*Template) Sprint

func (t *Template) Sprint(input map[string]any) (string, error)

Sprint wraps Execute and returns the result as a string.

type TestFunctionOptions

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

type Validate

type Validate[T any] func(T) error

Jump to

Keyboard shortcuts

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