Documentation
¶
Overview ¶
Package gottp provides a Go implementation of the Template Text Parser (TTP) library.
GoTTP is a semi-structured text parsing library that uses templates to extract structured data from text. It is compatible with Python TTP templates and provides enhanced features including stateless compiled templates and multi-language macro support.
Quick Start ¶
package main
import (
"fmt"
"github.com/roc-ops/gottp"
)
func main() {
// Compile a template
template := `
<group name="interfaces">
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
</group>
`
compiled, err := gottp.CompileTemplate(template)
if err != nil {
panic(err)
}
// Parse data (can be called repeatedly without reset)
data := `
interface Loopback0
ip address 192.168.0.1/24
!`
result, err := compiled.Parse(
gottp.Inputs{"Default_Input": data},
nil, // vars
nil, // options
)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", result)
// With source maps enabled:
parseResult, err := compiled.ParseWithValidation(
gottp.Inputs{"Default_Input": data},
nil, // vars
&gottp.ParseOptions{EnableSourceMap: true},
)
if err != nil {
panic(err)
}
// Access parsed data
fmt.Printf("Data: %+v\n", parseResult.Data)
// Access source map to see which input lines matched
if parseResult.SourceMap != nil {
inputMap := parseResult.SourceMap.Inputs["Default_Input"]
for _, line := range inputMap.Lines {
if line.Matched {
fmt.Printf("Line %d matched\n", line.LineNumber+1)
}
}
}
}
Key Features ¶
- Stateless Compiled Templates: Compile once, use many times without state resets
- Full TTP Compatibility: Works with existing Python TTP templates
- Multi-Language Macros: Support for Starlark, JavaScript, and Python macros
- Thread-Safe: Compiled templates are immutable and safe for concurrent use
- High Performance: Leverages Go's compiled nature for better performance
- Source Maps: Track which parts of input text matched which template patterns (optional)
Stateless Design ¶
Unlike Python TTP, GoTTP's compiled templates are stateless. The Parse() method takes all necessary parameters and has no internal state, allowing for:
- Repeated calls without reset
- Concurrent execution from multiple goroutines
- Better performance and memory efficiency
Template Syntax ¶
GoTTP uses the same template syntax as Python TTP. Templates are XML-like structures with special tags:
- <template>: Root template container
- <group>: Pattern matching group
- <input>: Input data source configuration
- <output>: Output formatting configuration
- <vars>: Template variables
- <macro>: Macro function definitions
- <extend>: Template extension
Pattern Matching ¶
Patterns use double curly braces to define variables:
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
Variables can have functions applied:
{{ ip | IP }}
{{ mac | MAC }}
{{ value | upper | split(',') }}
Macros ¶
Macros allow custom processing logic. Supported languages:
- Starlark (default): Python-like, safe for embedding, no dependencies
- JavaScript: Using goja runtime, no external dependencies
- Python: Using CGO (optional, requires -tags python build flag and Python runtime)
Example:
<macro name="process_data" language="starlark">
def process_data(data):
return data.upper()
</macro>
Template Extension ¶
Templates can extend other templates using the <extend> tag:
<extend template="base_template.txt" groups="common,advanced" />
Code Generation ¶
Use the gottp-gen tool to embed templates at compile time:
//go:generate gottp-gen -template=template.txt -var=MyTemplate
Then run: go generate
Examples ¶
See the examples/ directory for complete working examples.
Index ¶
- func LoadLookupFromCSV(name string, data []byte, keyColumn string) (map[string]map[string]interface{}, error)
- func LoadLookupFromJSON(name string, data []byte) (map[string]map[string]interface{}, error)
- func LoadLookupFromYAML(name string, data []byte) (map[string]map[string]interface{}, error)
- func LoadLookupsFromJSON(data []byte) (map[string]map[string]interface{}, error)
- func MergeLookups(lookups ...map[string]map[string]interface{}) map[string]map[string]interface{}
- func SaveCompiledTemplate(ct *CompiledTemplate, format string) ([]byte, error)
- func SaveCompiledTemplateToBytes(ct *CompiledTemplate, format SerializationFormat) ([]byte, error)
- func ValidateTemplate(templateStr string) error
- type CompileOptions
- type CompiledTemplate
- func CompileTemplate(templateText string) (*CompiledTemplate, error)
- func CompileTemplateWithOptions(templateText string, options *CompileOptions) (*CompiledTemplate, error)
- func LoadCompiledTemplate(data []byte, format string) (*CompiledTemplate, error)
- func LoadCompiledTemplateFromBytes(data []byte, format SerializationFormat) (*CompiledTemplate, error)
- func (ct *CompiledTemplate) GetWarnings() []string
- func (ct *CompiledTemplate) NewRuntime() *Runtimedeprecated
- func (ct *CompiledTemplate) Parse(inputs Inputs, vars Vars, options *ParseOptions) (interface{}, error)
- func (ct *CompiledTemplate) ParseWithValidation(inputs Inputs, vars Vars, options *ParseOptions) (*ParseResult, error)
- type Functions
- type GroupFunc
- type InputFunc
- type InputSourceMap
- type Inputs
- type LineMapping
- type MacroFunc
- type MacroRegistry
- type MatchFunc
- type MatchMapping
- type OutputFunc
- type ParseOptions
- type ParseResult
- type PythonParser
- func (p *PythonParser) AddInput(data, inputName string)
- func (p *PythonParser) AddTemplate(template string) error
- func (p *PythonParser) AddVars(vars Vars)
- func (p *PythonParser) ClearInput()
- func (p *PythonParser) ClearResult()
- func (p *PythonParser) Parse() error
- func (p *PythonParser) Result() []interface{}
- type Runtime
- type SerializationFormat
- type SourceMap
- type VarRange
- type Vars
- type YANGModules
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadLookupFromCSV ¶
func LoadLookupFromCSV(name string, data []byte, keyColumn string) (map[string]map[string]interface{}, error)
LoadLookupFromCSV parses CSV data into a named lookup table suitable for ParseOptions.Lookups. The first row is treated as headers. The keyColumn parameter specifies which column to use as the lookup key. If keyColumn is empty, the first column is used. Each row becomes an entry in the lookup table keyed by the keyColumn value, with all columns (including the key column) as fields.
All values are strings since CSV does not carry type information.
Returns a single-entry map (name -> table) that can be directly assigned or merged into ParseOptions.Lookups.
func LoadLookupFromJSON ¶
LoadLookupFromJSON parses JSON data into a named lookup table suitable for ParseOptions.Lookups. The JSON should be an object where keys map to objects (map of maps). Example JSON: {"65100": {"as_name": "Subs", "prefix_num": "734"}}
Returns a single-entry map (name -> table) that can be directly assigned or merged into ParseOptions.Lookups.
func LoadLookupFromYAML ¶
LoadLookupFromYAML parses YAML data into a named lookup table suitable for ParseOptions.Lookups. Example YAML:
"65100": as_name: Subs prefix_num: "734"
Returns a single-entry map (name -> table) that can be directly assigned or merged into ParseOptions.Lookups.
func LoadLookupsFromJSON ¶
LoadLookupsFromJSON parses JSON data containing multiple named lookup tables. The JSON should be a nested object: {"table_name": {"key": {"field": "value"}}}. This is useful when a single JSON file contains all lookup tables.
Returns a map that can be directly assigned to ParseOptions.Lookups.
func MergeLookups ¶
MergeLookups merges multiple lookup maps into a single map suitable for ParseOptions.Lookups. Later entries override earlier entries with the same name.
func SaveCompiledTemplate ¶
func SaveCompiledTemplate(ct *CompiledTemplate, format string) ([]byte, error)
SaveCompiledTemplate saves a compiled template to bytes in the specified format Supported formats: "gob", "json", "yaml"
func SaveCompiledTemplateToBytes ¶
func SaveCompiledTemplateToBytes(ct *CompiledTemplate, format SerializationFormat) ([]byte, error)
SaveCompiledTemplateToBytes saves a compiled template to bytes
func ValidateTemplate ¶
ValidateTemplate validates a template string before compilation. Returns an error if the template has validation issues.
Types ¶
type CompileOptions ¶
type CompileOptions struct {
// Functions provides custom functions baked into the compiled template,
// organized by scope. These functions are available for all Parse() calls
// and can be overridden per-parse via ParseOptions.Functions.
Functions *Functions
}
CompileOptions contains options for template compilation. Functions registered here are baked into the compiled template and available for all subsequent Parse() calls without needing to pass them in ParseOptions.
Precedence: built-in functions < CompileOptions functions < ParseOptions functions
type CompiledTemplate ¶
type CompiledTemplate struct {
// contains filtered or unexported fields
}
CompiledTemplate is a stateless, immutable compiled template.
Once compiled, a template can be used to parse multiple inputs without any state resets. The Parse() method is safe for concurrent use from multiple goroutines, though each goroutine should create its own Runtime instance (which happens automatically in Parse()).
Compiled templates can be serialized and saved for later use, or embedded in Go code using the gottp-gen code generation tool.
func CompileTemplate ¶
func CompileTemplate(templateText string) (*CompiledTemplate, error)
CompileTemplate compiles a TTP template text into a CompiledTemplate.
The compiled template is immutable and stateless, allowing it to be used repeatedly without reset. It can be safely shared across goroutines.
Example:
template := `<group name="test">{{ value }}</group>`
compiled, err := gottp.CompileTemplate(template)
if err != nil {
// handle error
}
func CompileTemplateWithOptions ¶
func CompileTemplateWithOptions(templateText string, options *CompileOptions) (*CompiledTemplate, error)
CompileTemplateWithOptions compiles a TTP template text with compile-time options.
Functions provided in CompileOptions are baked into the compiled template and available for all subsequent Parse() calls. They can be overridden per-parse via ParseOptions.Functions.
Precedence for function resolution:
- built-in functions (lowest)
- CompileOptions.Functions (middle)
- ParseOptions.Functions (highest, per-parse)
Example:
compiled, err := gottp.CompileTemplateWithOptions(template, &gottp.CompileOptions{
Functions: &gottp.Functions{
Match: map[string]gottp.MatchFunc{
"custom_transform": myTransformFunc,
},
},
})
func LoadCompiledTemplate ¶
func LoadCompiledTemplate(data []byte, format string) (*CompiledTemplate, error)
LoadCompiledTemplate loads a compiled template from bytes in the specified format
func LoadCompiledTemplateFromBytes ¶
func LoadCompiledTemplateFromBytes(data []byte, format SerializationFormat) (*CompiledTemplate, error)
LoadCompiledTemplateFromBytes loads a compiled template from bytes
func (*CompiledTemplate) GetWarnings ¶
func (ct *CompiledTemplate) GetWarnings() []string
GetWarnings returns compilation warnings (non-fatal issues like Python-specific syntax in Starlark macros)
func (*CompiledTemplate) NewRuntime
deprecated
func (ct *CompiledTemplate) NewRuntime() *Runtime
NewRuntime creates a reusable Runtime instance for a compiled template. This allows you to register Go macros and reuse the runtime for multiple parses.
Deprecated: For registering custom macros, use CompileTemplateWithOptions with CompileOptions.Functions.Macro instead, which provides a cleaner API and proper precedence handling. The RegisterGoMacro API continues to work for backward compatibility.
func (*CompiledTemplate) Parse ¶
func (ct *CompiledTemplate) Parse(inputs Inputs, vars Vars, options *ParseOptions) (interface{}, error)
Parse executes the compiled template with given inputs and variables.
This method is stateless and can be called repeatedly without reset. It does not modify the CompiledTemplate instance, making it safe for concurrent use. However, for best performance with concurrent parsing, create a new CompiledTemplate instance for each goroutine or use the parallel parsing utilities.
Parameters:
- inputs: Map of input names to input data strings
- vars: Map of variable names to variable values (can be nil)
- options: Parse options (can be nil for defaults)
Returns:
- Parsed results as interface{} (typically map[string]interface{} or []interface{})
- error if parsing fails
Example:
result, err := compiled.Parse(
gottp.Inputs{"Default_Input": "interface Loopback0\n ip address 1.1.1.1/24"},
gottp.Vars{"site": "datacenter1"},
nil,
)
func (*CompiledTemplate) ParseWithValidation ¶
func (ct *CompiledTemplate) ParseWithValidation(inputs Inputs, vars Vars, options *ParseOptions) (*ParseResult, error)
ParseWithValidation parses and returns both data and validation results
type Functions ¶
type Functions struct {
Match map[string]MatchFunc
Group map[string]GroupFunc
Output map[string]OutputFunc
Input map[string]InputFunc
Macro map[string]MacroFunc
}
Functions contains custom functions organized by scope.
type GroupFunc ¶
type GroupFunc func(data map[string]interface{}, args []string, kwargs map[string]interface{}) (map[string]interface{}, bool, error)
GroupFunc is the signature for custom group functions. Matches internal/functions/group.Function.
type InputFunc ¶
type InputFunc func(data string, args []string, kwargs map[string]interface{}) (string, bool, error)
InputFunc is the signature for custom input functions. Matches internal/functions/input.Function.
type InputSourceMap ¶
type InputSourceMap struct {
Lines []*LineMapping // one entry per input line
}
InputSourceMap contains source mapping for a single input
type LineMapping ¶
type LineMapping struct {
LineNumber int // 0-indexed line number
Matched bool // whether this line matched
Matches []*MatchMapping // matches on this line
}
LineMapping represents the mapping for a single line of input
type MacroFunc ¶
type MacroFunc func(data map[string]interface{}, args []string, kwargs map[string]interface{}) (map[string]interface{}, bool, error)
MacroFunc is the signature for custom Go macro functions. Matches internal/macro.GoMacroFunc.
type MacroRegistry ¶
type MacroRegistry struct {
// contains filtered or unexported fields
}
MacroRegistry provides access to register native Go macros
func (*MacroRegistry) RegisterGoMacro
deprecated
func (mr *MacroRegistry) RegisterGoMacro(name string, fn func(data map[string]interface{}, args []string, kwargs map[string]interface{}) (map[string]interface{}, bool, error))
RegisterGoMacro registers a native Go macro function. This allows high-performance macro execution without conversion overhead. The function signature matches group functions:
func(data map[string]interface{}, args []string, kwargs map[string]interface{}) (map[string]interface{}, bool, error)
Deprecated: Use CompileTemplateWithOptions with CompileOptions.Functions.Macro instead, which provides a cleaner API and proper precedence handling. RegisterGoMacro continues to work for backward compatibility.
type MatchFunc ¶
type MatchFunc func(value interface{}, args []string, kwargs map[string]interface{}) (interface{}, error)
MatchFunc is the signature for custom match functions. Matches internal/functions/match.Function.
type MatchMapping ¶
type MatchMapping struct {
StartCol int // start column (0-indexed)
EndCol int // end column (exclusive)
GroupName string // group name that matched
PatternIndex int // pattern index within group
Variables map[string]*VarRange // variable name -> character range
ResultPath string // path in result structure (e.g., "interfaces[0]")
}
MatchMapping represents a single match on a line
type OutputFunc ¶
type OutputFunc func(data interface{}, args []string, kwargs map[string]interface{}) (interface{}, error)
OutputFunc is the signature for custom output functions. Matches internal/functions/output.Function.
type ParseOptions ¶
type ParseOptions struct {
YANGModules *YANGModules // YANG modules for validation
EnableSourceMap bool // Enable source map collection (zero overhead when false)
// Lookups provides runtime lookup tables merged with compiled lookups at parse time.
// Runtime lookups override compiled lookups with the same name.
Lookups map[string]map[string]interface{}
// Vars provides runtime variables merged with compiled template vars and Parse() vars.
// Precedence: compiled vars < Parse() vars < ParseOptions.Vars.
Vars map[string]interface{}
// Functions provides custom functions injected at parse time, organized by scope.
// Custom functions override built-in functions with the same name.
Functions *Functions
}
ParseOptions represents options for parsing
type ParseResult ¶
type ParseResult struct {
Data interface{} // Parsed data
ValidationResults map[string]*yang.ValidationResult // YANG validation results by group name
SourceMap *SourceMap // Source map tracking input positions to results (optional)
}
ParseResult contains the parsed results and validation results
type PythonParser ¶
type PythonParser struct {
// contains filtered or unexported fields
}
PythonParser provides Python-compatible API
func NewParser ¶
func NewParser() *PythonParser
NewParser creates a new Python-compatible parser (stateful API)
func (*PythonParser) AddInput ¶
func (p *PythonParser) AddInput(data, inputName string)
AddInput adds input data
func (*PythonParser) AddTemplate ¶
func (p *PythonParser) AddTemplate(template string) error
AddTemplate adds a template
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime provides access to the underlying runtime for advanced operations such as registering native Go macros before parsing
func (*Runtime) GetMacroRegistry ¶
func (r *Runtime) GetMacroRegistry() *MacroRegistry
GetMacroRegistry returns the macro registry for registering Go macros This allows you to register native Go macros for high-performance execution
func (*Runtime) Parse ¶
func (r *Runtime) Parse(inputs Inputs, vars Vars, options *ParseOptions) (interface{}, error)
Parse executes the runtime with given inputs and variables
func (*Runtime) ParseWithValidation ¶
func (r *Runtime) ParseWithValidation(inputs Inputs, vars Vars, options *ParseOptions) (*ParseResult, error)
ParseWithValidation parses and returns both data and validation results
type SerializationFormat ¶
type SerializationFormat = compiler.SerializationFormat
SerializationFormat represents the format for serialization
const ( FormatGob SerializationFormat = compiler.FormatGob FormatJSON SerializationFormat = compiler.FormatJSON FormatYAML SerializationFormat = compiler.FormatYAML )
type SourceMap ¶
type SourceMap struct {
Inputs map[string]*InputSourceMap // input name -> source map
}
SourceMap tracks which parts of input text matched which template patterns