transformer

package
v0.0.0-...-84db50f Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package transformer provides functionality for transforming and parsing data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TextParser

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

TextParser provides functionality to parse various text formats (JSON, XML, plain text) and extract data into a structured map. It uses a configuration map to define the extraction rules for each format, such as JSONPath for JSON, XPath for XML, and regex for plain text.

Summary: Generic parser for extracting data from JSON, XML, Text, or using JQ.

func NewTextParser

func NewTextParser() *TextParser

NewTextParser returns a shared instance of TextParser.

Summary: Returns a singleton instance of TextParser.

Returns:

  • *TextParser: The singleton instance.

Side Effects:

  • Initializes the singleton on first call.

func (*TextParser) Parse

func (p *TextParser) Parse(inputType string, input []byte, config map[string]string, jqQuery string) (any, error)

Parse extracts data from an input byte slice based on the specified input type and configuration.

Summary: Parses input data according to rules defined in config or query.

Parameters:

  • inputType: string. One of "json", "xml", "text", "jq".
  • input: []byte. The raw input data.
  • config: map[string]string. Extraction rules (key -> path/regex). Used for json, xml, text.
  • jqQuery: string. The JQ query string. Used for jq type.

Returns:

  • any: The extracted data (usually map[string]any or any for jq).
  • error: An error if parsing fails or input type is unsupported.

Errors:

  • Returns error if input format is invalid.
  • Returns error if extraction rules fail.
  • Returns "unsupported input type" for unknown types.

func (*TextParser) Transform

func (p *TextParser) Transform(templateStr string, data any) ([]byte, error)

Transform takes a map of data and a Go template string and returns a byte slice containing the transformed output.

Summary: Delegates to the internal Transformer to render templates.

Parameters:

  • templateStr: string. The Go template.
  • data: any. The context data.

Returns:

  • []byte: The rendered output.
  • error: An error if transformation fails.

type TextTemplate

type TextTemplate struct {
	IsJSON bool
	// contains filtered or unexported fields
}

TextTemplate provides a simple wrapper around Go's standard text/template for rendering strings with dynamic data.

Summary: High-performance template engine using fasttemplate.

func NewTemplate

func NewTemplate(templateString, startTag, endTag string) (*TextTemplate, error)

NewTemplate parses a template string and creates a new TextTemplate.

Summary: Initializes a new TextTemplate.

Parameters:

  • templateString: string. The template source.
  • startTag: string. The start delimiter (e.g. "{{").
  • endTag: string. The end delimiter (e.g. "}}").

Returns:

  • *TextTemplate: The parsed template.
  • error: An error if parsing fails.

Side Effects:

  • Auto-detects if the template output is likely JSON to enable automatic escaping.

func (*TextTemplate) Render

func (t *TextTemplate) Render(params map[string]any) (string, error)

Render executes the template with the provided parameters and returns the resulting string.

Summary: Renders the template with data.

Parameters:

  • params: map[string]any. The data map for variable substitution.

Returns:

  • string: The rendered output.
  • error: An error if a key is missing or rendering fails.

Errors:

  • Returns error if a required tag is missing in params.

Side Effects:

  • Automatically escapes strings if the template is detected as JSON.

type Transformer

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

Transformer provides functionality to transform a map of data into a structured string using a Go template. It supports multiple output formats specified by the template, such as JSON, XML, or plain text.

Summary: Data transformation engine using Go templates with caching and pooling optimization.

func NewTransformer

func NewTransformer() *Transformer

NewTransformer creates and returns a new instance of Transformer.

Summary: Initializes a new Transformer.

Returns:

  • *Transformer: The initialized transformer.

Side Effects:

  • Initializes a sync.Pool for bytes.Buffer.

func (*Transformer) Transform

func (t *Transformer) Transform(templateStr string, data any) ([]byte, error)

Transform takes a map of data and a Go template string and returns a byte slice containing the transformed output.

Summary: Executes a Go template against provided data.

Parameters:

  • templateStr: string. The Go template to execute.
  • data: any. The input data context for the template.

Returns:

  • []byte: The transformed output.
  • error: An error if template parsing or execution fails.

Errors:

  • Returns error if template syntax is invalid.
  • Returns error if template execution fails.

Side Effects:

  • Caches parsed templates.
  • Uses a buffer pool to reduce allocations.

Jump to

Keyboard shortcuts

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