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 ¶
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 ¶
WithFuncs adds functions to function registry.
Example:
WithFuncs(Registry{ "bar": f, // function ":bar" "foo:bar": f, // function with namespace ":foo:bar" })
func WithLocale ¶
WithLocale adds locale information to the template.
type Options ¶
type Options map[string]*ResolvedValue
Options are a possible options for the function.
type Registry ¶
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.
type TestFunctionOptions ¶
type TestFunctionOptions struct {
// contains filtered or unexported fields
}