Help us shape the future of Go! Take the 2026 Go Developer Survey

transforms

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package transforms extends the pipeline with schema validation, data transformation, and enrichment middlewares.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Schema

type Schema struct {
	Name   string        `json:"name" yaml:"name"`
	Fields []SchemaField `json:"fields" yaml:"fields"`
	Strict bool          `json:"strict" yaml:"strict"` // Reject unknown fields.
}

Schema defines the expected shape and validation rules for items.

func (*Schema) Validate

func (s *Schema) Validate(item *types.Item) ValidationResult

Validate checks an item against the schema.

type SchemaField

type SchemaField struct {
	Name     string `json:"name" yaml:"name"`
	Type     string `json:"type" yaml:"type"` // string, number, boolean, array, url, email, date
	Required bool   `json:"required" yaml:"required"`
	Pattern  string `json:"pattern" yaml:"pattern"` // regex
	MinLen   int    `json:"min_len" yaml:"min_len"`
	MaxLen   int    `json:"max_len" yaml:"max_len"`
}

SchemaField defines validation rules for a single field.

type SchemaValidationMiddleware

type SchemaValidationMiddleware struct {
	Schema *Schema
	Logger *slog.Logger
	OnFail string // "drop", "log", "annotate"
}

SchemaValidationMiddleware validates items against a schema and drops invalid ones.

func (*SchemaValidationMiddleware) Name

Name returns the middleware name.

func (*SchemaValidationMiddleware) Process

func (m *SchemaValidationMiddleware) Process(item *types.Item) (*types.Item, error)

Process validates the item.

type Transform

type Transform func(item *types.Item) error

Transform is a function that modifies an item in place.

func AddTimestamp

func AddTimestamp(field string) Transform

AddTimestamp adds a timestamp field to items.

func ComputeField

func ComputeField(name string, compute func(fields map[string]any) any) Transform

ComputeField creates a new field by applying a function to existing fields.

func DropFields

func DropFields(fields ...string) Transform

DropFields removes specified fields from items.

func RegexReplace

func RegexReplace(field, pattern, replacement string) Transform

RegexReplace applies a regex replacement on a field.

func RenameField

func RenameField(from, to string) Transform

RenameField renames a field.

func SetDefault

func SetDefault(field string, defaultVal any) Transform

SetDefault sets a default value if the field is missing or empty.

func TrimStrings

func TrimStrings() Transform

TrimStrings trims whitespace from all string fields.

type TransformMiddleware

type TransformMiddleware struct {
	Transforms []Transform
	Logger     *slog.Logger
}

TransformMiddleware applies a sequence of transforms to items.

func (*TransformMiddleware) Name

func (m *TransformMiddleware) Name() string

Name returns the middleware name.

func (*TransformMiddleware) Process

func (m *TransformMiddleware) Process(item *types.Item) (*types.Item, error)

Process applies all transforms in order.

type ValidationResult

type ValidationResult struct {
	Valid  bool     `json:"valid"`
	Errors []string `json:"errors,omitempty"`
}

ValidationResult contains the outcome of schema validation.

Jump to

Keyboard shortcuts

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