Documentation
¶
Index ¶
- Variables
- func DefaultFuncMap() template.FuncMap
- type CacheStats
- type CachedTemplate
- type Config
- type FileSystemLoader
- type FileWatcher
- type GenericParser
- type MemoryLoader
- func (m *MemoryLoader) AddTemplate(name, content string)
- func (m *MemoryLoader) LastModified(name string) (time.Time, error)
- func (m *MemoryLoader) List() ([]string, error)
- func (m *MemoryLoader) Load(name string) (string, error)
- func (m *MemoryLoader) Watch(ctx context.Context, callback func(name string)) error
- type Parser
- type RequestData
- type RereadableRequest
- type TemplateCache
- type TemplateLoader
- type XMLHelper
- func (h XMLHelper) GetXMLAttribute(xmlMap map[string]interface{}, elementName, attrName string) string
- func (h XMLHelper) GetXMLAttributeArray(xmlMap map[string]interface{}, elementName, attrName string) []string
- func (h XMLHelper) GetXMLText(xmlMap map[string]interface{}, elementName string) string
- func (h XMLHelper) GetXMLTextArray(xmlMap map[string]interface{}, elementName string) []string
- func (h XMLHelper) GetXMLValue(xmlMap map[string]interface{}, elementName string) interface{}
- func (h XMLHelper) GetXMLValueArray(xmlMap map[string]interface{}, elementName string) []interface{}
- func (h XMLHelper) HasXMLAttribute(xmlMap map[string]interface{}, elementName, attrName string) bool
- func (h XMLHelper) HasXMLElement(xmlMap map[string]interface{}, elementName string) bool
- func (h XMLHelper) IsXMLArray(xmlMap map[string]interface{}, elementName string) bool
- func (h XMLHelper) ListXMLAttributes(xmlMap map[string]interface{}, elementName string) []string
- func (h XMLHelper) ListXMLElements(xmlMap map[string]interface{}) []string
- func (h XMLHelper) XMLArrayLength(xmlMap map[string]interface{}, elementName string) int
Constants ¶
This section is empty.
Variables ¶
var ( ErrTemplateNotFound = errors.New("template not found") ErrWatcherClosed = errors.New("file watcher is closed") ErrInvalidConfig = errors.New("invalid configuration") ErrParserClosed = errors.New("parser is closed") )
Common errors
Functions ¶
func DefaultFuncMap ¶
Helper function to create default function map with useful template functions
Types ¶
type CacheStats ¶
type CacheStats struct {
Size int // Current number of cached templates
MaxSize int // Maximum cache size (0 = unlimited)
HitCount int64 // Total number of cache hits
}
CacheStats holds cache statistics
type CachedTemplate ¶
type CachedTemplate struct {
Template *template.Template
LastModified time.Time
AccessTime time.Time
AccessCount int64
}
CachedTemplate holds a compiled template with metadata
type Config ¶
type Config struct {
// TemplateLoader specifies how to load templates
TemplateLoader TemplateLoader
// WatchFiles enables automatic template reloading on file changes
WatchFiles bool
// MaxCacheSize limits the number of cached templates (0 = unlimited)
MaxCacheSize int
// FuncMap provides custom template functions
FuncMap template.FuncMap
}
Config holds configuration for the parser
type FileSystemLoader ¶
type FileSystemLoader struct {
// RootDir is the root directory for templates
RootDir string
// Extension is the file extension for templates (e.g., ".tmpl", ".tpl")
Extension string
// Recursive enables recursive directory scanning
Recursive bool
// contains filtered or unexported fields
}
FileSystemLoader loads templates from the file system
func NewFileSystemLoader ¶
func NewFileSystemLoader(rootDir, extension string, recursive bool) *FileSystemLoader
NewFileSystemLoader creates a new file system template loader
func (*FileSystemLoader) LastModified ¶
func (f *FileSystemLoader) LastModified(name string) (time.Time, error)
LastModified implements TemplateLoader
func (*FileSystemLoader) List ¶
func (f *FileSystemLoader) List() ([]string, error)
List implements TemplateLoader
type FileWatcher ¶
type FileWatcher interface {
// Watch starts watching the specified directory for changes
Watch(ctx context.Context, dir, extension string, recursive bool, callback func(name string)) error
// Close stops watching and cleans up resources
Close() error
}
FileWatcher watches for file system changes
func NewFileWatcher ¶
func NewFileWatcher() (FileWatcher, error)
NewFileWatcher creates a new file watcher
type GenericParser ¶
type GenericParser[T any] interface { // Parse executes the named template and returns the result as type T Parse(templateName string, request *http.Request) (T, error) // ParseWith executes the named template with custom data and returns the result as type T ParseWith(templateName string, request *http.Request, data interface{}) (T, error) // UpdateTemplate loads or updates a template with the given content and hash UpdateTemplate(name string, content string, hash string) error // GetCacheStats returns cache statistics GetCacheStats() CacheStats // Close cleanly shuts down the parser and releases resources Close() error }
GenericParser provides type-safe template parsing for HTTP requests T is the target type that the parsed template will be converted to
func NewGenericParser ¶
func NewGenericParser[T any](config Config) (GenericParser[T], error)
NewGenericParser creates a new generic template parser with the given configuration
type MemoryLoader ¶
type MemoryLoader struct {
// contains filtered or unexported fields
}
MemoryLoader loads templates from memory (useful for testing)
func NewMemoryLoader ¶
func NewMemoryLoader() *MemoryLoader
NewMemoryLoader creates a new memory-based template loader
func (*MemoryLoader) AddTemplate ¶
func (m *MemoryLoader) AddTemplate(name, content string)
AddTemplate adds a template to memory
func (*MemoryLoader) LastModified ¶
func (m *MemoryLoader) LastModified(name string) (time.Time, error)
LastModified implements TemplateLoader (returns current time for memory loader)
func (*MemoryLoader) List ¶
func (m *MemoryLoader) List() ([]string, error)
List implements TemplateLoader
type Parser ¶
type Parser interface {
// Parse executes the named template with the given HTTP request data
Parse(templateName string, request *http.Request, output io.Writer) error
// ParseWith executes the named template with custom data along with HTTP request
ParseWith(templateName string, request *http.Request, data interface{}, output io.Writer) error
// UpdateTemplate loads or updates a template with the given content and hash
UpdateTemplate(name string, content string, hash string) error
// GetCacheStats returns cache statistics
GetCacheStats() CacheStats
// Close cleanly shuts down the parser and releases resources
Close() error
}
Parser provides high-performance template parsing for HTTP requests
type RequestData ¶
type RequestData struct {
// Request is the original HTTP request
Request *http.Request
// Headers contains all HTTP headers
Headers map[string][]string
// Query contains query parameters
Query map[string][]string
// Form contains form data (for POST requests)
Form map[string][]string
// Body contains the request body as string
Body string
// BodyJSON contains parsed JSON data when Content-Type is application/json
BodyJSON map[string]interface{}
// BodyXML contains parsed XML data when Content-Type is text/xml or application/xml
BodyXML map[string]interface{}
// Custom contains any additional custom data
Custom interface{}
}
RequestData represents the data structure available to templates
func ExtractRequestData ¶
func ExtractRequestData(r *RereadableRequest, customData interface{}) (*RequestData, error)
ExtractRequestData extracts structured data from the HTTP request for template use
type RereadableRequest ¶
RereadableRequest wraps an HTTP request to make it re-readable
func NewRereadableRequest ¶
func NewRereadableRequest(r *http.Request) (*RereadableRequest, error)
NewRereadableRequest creates a new re-readable HTTP request
func (*RereadableRequest) Body ¶
func (r *RereadableRequest) Body() string
Body returns the request body as a string
func (*RereadableRequest) BodyBytes ¶
func (r *RereadableRequest) BodyBytes() []byte
BodyBytes returns the request body as bytes
func (*RereadableRequest) Reset ¶
func (r *RereadableRequest) Reset()
Reset resets the request body to the beginning for re-reading
type TemplateCache ¶
type TemplateCache struct {
// contains filtered or unexported fields
}
TemplateCache provides efficient caching of compiled templates
func NewTemplateCache ¶
func NewTemplateCache(maxSize int, funcMap template.FuncMap) *TemplateCache
NewTemplateCache creates a new template cache
func (*TemplateCache) Clear ¶
func (c *TemplateCache) Clear()
Clear clears all templates from the cache
func (*TemplateCache) Get ¶
func (c *TemplateCache) Get(name string, loader TemplateLoader) (*template.Template, error)
Get retrieves a template from the cache or compiles it if not found
func (*TemplateCache) Remove ¶
func (c *TemplateCache) Remove(name string)
Remove removes a template from the cache
func (*TemplateCache) Set ¶
func (c *TemplateCache) Set(name string, tmpl *template.Template, hash string)
Set directly sets a template in the cache with the given hash
func (*TemplateCache) Stats ¶
func (c *TemplateCache) Stats() CacheStats
Stats returns cache statistics
type TemplateLoader ¶
type TemplateLoader interface {
// Load returns the template content by name
Load(name string) (content string, err error)
// List returns all available template names
List() ([]string, error)
// Watch starts watching for template changes and calls the callback
// when a template is modified. Returns a context cancel function.
Watch(ctx context.Context, callback func(name string)) error
// LastModified returns the last modification time of a template
LastModified(name string) (time.Time, error)
}
TemplateLoader defines the interface for loading templates
type XMLHelper ¶
type XMLHelper struct{}
XMLHelper provides template functions for XML manipulation
func (XMLHelper) GetXMLAttribute ¶
func (h XMLHelper) GetXMLAttribute(xmlMap map[string]interface{}, elementName, attrName string) string
GetXMLAttribute extracts a specific attribute from an XML node map Usage: {{xmlAttr .BodyXML "key" "attr1"}} to get the 'attr1' attribute from 'key' element Works with format (key/attr)
func (XMLHelper) GetXMLAttributeArray ¶
func (h XMLHelper) GetXMLAttributeArray(xmlMap map[string]interface{}, elementName, attrName string) []string
GetXMLAttributeArray extracts all attribute values as an array Usage: {{xmlAttrArray .BodyXML "item" "id"}} to get all 'id' attributes from 'item' elements
func (XMLHelper) GetXMLText ¶
GetXMLText extracts text content from an XML element
func (XMLHelper) GetXMLTextArray ¶
GetXMLTextArray extracts all text content from XML elements as string array
func (XMLHelper) GetXMLValue ¶
GetXMLValue extracts the value of an XML element Usage: {{xmlValue .BodyXML "key"}} to get the value of 'key' element For arrays: returns the first element
func (XMLHelper) GetXMLValueArray ¶
func (h XMLHelper) GetXMLValueArray(xmlMap map[string]interface{}, elementName string) []interface{}
GetXMLValueArray extracts all values of an XML element as an array Usage: {{xmlValueArray .BodyXML "item"}} to get all 'item' element values
func (XMLHelper) HasXMLAttribute ¶
func (h XMLHelper) HasXMLAttribute(xmlMap map[string]interface{}, elementName, attrName string) bool
HasXMLAttribute checks if an XML element has a specific attribute Usage: {{hasXMLAttr .BodyXML "key" "attr1"}}
func (XMLHelper) HasXMLElement ¶
HasXMLElement checks if an XML element exists Usage: {{hasXMLElement .BodyXML "key"}}
func (XMLHelper) IsXMLArray ¶
IsXMLArray checks if an XML element is an array (has multiple values) Usage: {{isXMLArray .BodyXML "item"}}
func (XMLHelper) ListXMLAttributes ¶
ListXMLAttributes returns all attribute names for a specific element Usage: {{range xmlAttrs .BodyXML "key"}}{{.}}{{end}}
func (XMLHelper) ListXMLElements ¶
ListXMLElements returns all element names from the XML map Usage: {{range xmlElements .BodyXML}}{{.}}{{end}}