template

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: Apache-2.0 Imports: 7 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateHeadersSourceFactory

func CreateHeadersSourceFactory() map[string]SourceFactory

CreateHeadersSourceFactory creates a source factory map with the "headers" source. This is a convenience function for creating templates that can reference HTTP headers.

Types

type HttpHeaderResolver

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

HttpHeaderResolver resolves field values from HTTP headers.

func NewHttpHeaderResolver

func NewHttpHeaderResolver(headers nethttp.Header) *HttpHeaderResolver

NewHttpHeaderResolver creates a resolver that looks up values in HTTP headers.

func (*HttpHeaderResolver) Resolve

func (h *HttpHeaderResolver) Resolve(fieldName string) (string, error)

type MapResolver

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

MapResolver resolves field values from a string map.

func NewMapResolver

func NewMapResolver(data map[string]string) *MapResolver

NewMapResolver creates a resolver that looks up values in the provided map.

func (*MapResolver) Resolve

func (m *MapResolver) Resolve(fieldName string) (string, error)

type ParsedTemplate

type ParsedTemplate struct {
	Template        string           // Final template with format specifiers
	Variables       []Variable       // Ordered list of variables - order comes from parse order
	VariableIndices map[string][]int // Map from variable name to all indices where it appears
}

func ParseTemplate

func ParseTemplate(template string, opts TemplateParserOptions) (*ParsedTemplate, error)

type SourceFactory

type SourceFactory func(fieldName string) VariableFormatter

SourceFactory creates a VariableFormatter for a specific field within a source. Called during template parsing when encountering syntax like {source.fieldName}.

func NewSourceFactory

func NewSourceFactory(sourceName string) SourceFactory

NewSourceFactory creates a SourceFactory for a given source name. This allows users to easily create custom sources for their templates.

Example usage:

sources := map[string]template.SourceFactory{
    "secrets": template.NewSourceFactory("secrets"),
    "config":  template.NewSourceFactory("config"),
}

type SourceFormatter

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

SourceFormatter is a formatter that resolves values from a runtime data source. It's used internally by the template system when parsing source references like {headers.Token}.

func (*SourceFormatter) FormatString

func (sf *SourceFormatter) FormatString() string

func (*SourceFormatter) GetResult

func (sf *SourceFormatter) GetResult() (any, error)

func (*SourceFormatter) SetField

func (sf *SourceFormatter) SetField(_ string, _ any)

func (*SourceFormatter) VariableNames

func (sf *SourceFormatter) VariableNames() []string

type SourceResolver

type SourceResolver interface {
	Resolve(fieldName string) (string, error)
}

SourceResolver resolves field values from a runtime data source. Implementations adapt different data structures (http.Header, maps, etc.) to provide string values for named fields.

type TemplateBuilder

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

TemplateBuilder builds the final template string by collecting values for all variables and rendering them into the template.

func NewTemplateBuilder

func NewTemplateBuilder(pt *ParsedTemplate, omitIfFalse bool) (*TemplateBuilder, error)

NewTemplateBuilder creates a new builder from a parsed template.

func (*TemplateBuilder) FormatString

func (tb *TemplateBuilder) FormatString() string

func (*TemplateBuilder) GetResult

func (tb *TemplateBuilder) GetResult() (any, error)

func (*TemplateBuilder) SetField

func (tb *TemplateBuilder) SetField(path string, value any)

func (*TemplateBuilder) SetSourceResolver

func (tb *TemplateBuilder) SetSourceResolver(sourceName string, resolver SourceResolver)

SetSourceResolver sets the resolver for all formatters using the specified source. The resolver will be used to resolve field values when GetResult is called.

func (*TemplateBuilder) VariableNames

func (tb *TemplateBuilder) VariableNames() []string

type TemplateParserOptions

type TemplateParserOptions struct {
	InputSchema *jsonschema.Schema           // used to validate parameters and determine their type
	Formatters  map[string]VariableFormatter // used to specify specific formatting options for specific variables
	Sources     map[string]SourceFactory     // factories for creating formatters for custom sources (e.g., headers, secrets)
}

type Variable

type Variable struct {
	VariableFormatter
	Name  string // the variable name (e.g. "userId", or "env.API_KEY")
	Type  VariableType
	Index int // Position in the parameter list
}

type VariableFormatter

type VariableFormatter interface {
	SetField(path string, value any)
	GetResult() (any, error)
	FormatString() string
	VariableNames() []string // Returns the list of variable names this formatter needs
}

VariableFormatter formats template variables into their final values. It implements a builder pattern where values are set via SetField and the final result is retrieved via GetResult.

func NewTemplateFormatter

func NewTemplateFormatter(templateStr string, inputSchema *jsonschema.Schema, omitIfFalse bool, sources map[string]SourceFactory) (VariableFormatter, error)

NewTemplateFormatter creates a formatter from a template string.

type VariableType

type VariableType int
const (
	VariableTypeParam VariableType = iota
	VariableTypeEnv
	VariableTypeSource
)

Jump to

Keyboard shortcuts

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