Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrDuplicateDeclaration = errors.New("duplicate declaration") ErrDuplicateOptionName = errors.New("duplicate option name") ErrFormatting = errors.New("formatting error") ErrMissingSelectorAnnotation = errors.New("missing selector annotation") // ErrOperandMismatch is an Invalid Expression error that occurs when an operand provided // to a function during function resolution does not match one of the expected // implementation-defined types for that function; or in which a literal operand value does not // have the required format and thus cannot be processed into one of the expected // implementation-defined types for that specific function. ErrOperandMismatch = errors.New("operand mismatch") ErrSelection = errors.New("selection error") ErrSyntax = errors.New("syntax error") ErrUnknownFunction = errors.New("unknown function reference") ErrUnresolvedVariable = errors.New("unresolved variable") ErrUnsupportedExpression = errors.New("unsupported expression") ErrUnsupportedStatement = errors.New("unsupported statement") )
MessageFormat2 Errors as defined in the specification.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(t *Template)
Option is a template option.
func WithFunc ¶
func WithFunc(name string, f RegistryFunc) Option
WithFunc adds a single function to function registry.
func WithLocale ¶
type Options ¶
Options are a possible options for the function.
type Registry ¶
type Registry map[string]RegistryFunc
func NewRegistry ¶
func NewRegistry() Registry
NewRegistry returns a new registry with default functions.
type RegistryFunc ¶
type SortableVariant ¶
type SortableVariants ¶
type SortableVariants []SortableVariant
func (SortableVariants) Len ¶
func (s SortableVariants) Len() int
func (SortableVariants) Less ¶
func (s SortableVariants) Less(i, j int) bool
func (SortableVariants) Swap ¶
func (s SortableVariants) Swap(i, j int)
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/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 any, options template.Options, locale language.Tag) (any, error) { if value == nil { return "", fmt.Errorf("input is required: %w", template.ErrOperandMismatch) } color, ok := value.(string) if !ok { return nil, fmt.Errorf("input is not a string: %w", template.ErrOperandMismatch) } if options == nil { return color, nil } style, err := options.GetString("style", "RGB") if err != nil { return nil, fmt.Errorf("get style: %w", err) } 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, nil } // } // Parse template. t, err := template.New(template.WithFunc("color", template.RegistryFunc{Format: 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.
Click to show internal directories.
Click to hide internal directories.