Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Constant ¶
type Constant struct {
// contains filtered or unexported fields
}
Constant is an Eval-confirming implementation that always returns the provided constant string value. This is useful for cases where a fixed string value is needed in the template without evaluating any context map.
type Eval ¶
type Eval interface {
// String evaluates the field's value in the context map and returns its
// string representation. A non-nil error is returned if the implementation
// cannot evaluate the value (e.g. the field is required and not present in
// the context map). An empty string indicates the field was not found or
// its value is nil.
String(c map[string]any) (string, error)
}
Eval is a generic interface for evaluating the value of a field in a given context map as a string value.
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field represents a single named value in the template conforming to Eval.
func F ¶
F creates a new Field instance with the provided id and sets the default format to Value. The field instance defaults to being optional (not required) and has no default value.
func (*Field) Default ¶
Default sets a default value for the field. If the field is not present in the context map or its value is nil, the default value will be used instead.
func (*Field) Formatter ¶
Formatter sets a custom Stringer function to format the field's value. This allows for custom formatting logic to be applied to the field's value when generating the string representation. If not set, the default Value function is used to convert the value to a string.
func (*Field) Required ¶
Required marks the field as required. If the field is not present in the context map and no default value is set, an error will be returned when the template is evaluated.
func (*Field) String ¶
String evaluates the field's value in the context map and returns its string representation using its provided Stringer. A non-nil error is returned if the field is required and not present in the context map (and no acceptable default value is provided). An empty string indicates the field was not found or its value is nil.
type FirstMatch ¶
type FirstMatch struct {
// contains filtered or unexported fields
}
FirstMatch is an Eval-confirming implementation that evaluates the given fields in order and returns the first successfully evaluated value as a string, if any.
func First ¶
func First(fields ...Eval) *FirstMatch
First creates an evaluator that returns the first non-nil/non-zero value from a list of field values, otherwise returning an empty string. This is useful for cases where fields have some form of mutually exclusive relationships.
func (*FirstMatch) String ¶
func (f *FirstMatch) String(c map[string]any) (string, error)
String returns the first non-zero/non-nil value from the list of fields provided to First. If no fields are found, an empty string is returned. If an error occurs while evaluating a field, it returns the error directly.
type Stringer ¶
Stringer mimics fmt.Stringer but returns a string for a given value rather than being a method on a type. This is used to provide formatting callbacks when stringifying a field's value in the template.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template represents a list of ordered Eval values that can be used to generate a string representation from an input (a "context map") with conditional inclusion behavior and formatting delegates.
func NewTemplate ¶
NewTemplate creates a new Template instance with the provided list of Eval values. The fields are stored in the order they are provided and will be evaluated in that order when generating the string representation.
func (Template) String ¶
String generates a string representation of the template using the provided context map inputs. Each template Eval is evaluated in order, returning any non-nil errors. Otherwise, the corresponding string, if non-empty, is concatenated into a single string with a space separator. If no fields are found or all are nil, an empty string is returned.