transformer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package transformer converts structured data between supported encodings and applies caller-provided transformations to the decoded value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(value any, transforms ...Transformation) (any, error)

Apply runs transforms in order.

func Convert

func Convert(data []byte, input Format, output Format) ([]byte, error)

Convert decodes data from the input format and encodes it to the output format.

func Decode

func Decode(data []byte, format Format) (any, error)

Decode decodes data into generic Go values.

func Encode

func Encode(value any, format Format) ([]byte, error)

Encode encodes generic Go values into the requested format.

func Validate

func Validate(value any, conditions ...Condition) error

Validate returns the first error from non-nil conditions.

Types

type Condition

type Condition func(any) error

Condition validates a transformation value.

func IsEmail

func IsEmail() Condition

IsEmail validates that a value is an email address.

func IsEthAddress

func IsEthAddress() Condition

IsEthAddress validates that a value is an Ethereum address.

func IsIPv4

func IsIPv4() Condition

IsIPv4 validates that a value is an IPv4 address.

func IsIPv6

func IsIPv6() Condition

IsIPv6 validates that a value is an IPv6 address.

func IsNumber

func IsNumber() Condition

IsNumber validates that a value is numeric.

func IsPhoneNumber

func IsPhoneNumber() Condition

IsPhoneNumber validates that a value is a phone number.

func IsSolanaAddress

func IsSolanaAddress() Condition

IsSolanaAddress validates that a value is a Solana public key address.

func IsString

func IsString() Condition

IsString validates that a value is a string.

func MustInclude

func MustInclude(keys ...string) Condition

MustInclude validates that a payload includes all required keys.

func MustNotInclude

func MustNotInclude(keys ...string) Condition

MustNotInclude validates that a payload excludes all forbidden keys.

func Regex

func Regex(pattern string, name ...string) Condition

Regex validates that the JSON representation of a value matches a pattern.

type Format

type Format string

Format identifies a supported wire encoding.

const (
	FormatEnv  Format = "env"
	FormatJSON Format = "json"
	FormatTOML Format = "toml"
	FormatYAML Format = "yaml"
)

type Payload

type Payload map[string]any

Payload is a decoded top-level object.

func (Payload) Delete

func (payload Payload) Delete(keys ...string) Payload

Delete removes keys from payload and returns payload for chaining.

func (Payload) Get

func (payload Payload) Get(key string) (any, bool)

Get returns the value stored for key.

func (Payload) Has

func (payload Payload) Has(key string) bool

Has reports whether key exists in payload.

func (Payload) Rename

func (payload Payload) Rename(from string, to string) Payload

Rename renames a key when it exists and returns payload for chaining.

func (Payload) Set

func (payload Payload) Set(key string, value any) Payload

Set stores value for key and returns payload for chaining.

type Transformation

type Transformation func(any) (any, error)

Transformation mutates or replaces decoded data.

func Delete

func Delete(keys []string, conditions ...Condition) Transformation

Delete deletes top-level map keys.

func Map

func Map(mapper func(Payload) error, conditions ...Condition) Transformation

Map applies mapper to the decoded top-level map.

func Rename

func Rename(from string, to string, conditions ...Condition) Transformation

Rename renames a top-level map key when it exists.

func Set

func Set(key string, value any, conditions ...Condition) Transformation

Set sets a top-level map key to value.

type Transformer

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

Transformer decodes input data, applies transformations in order, and encodes the result into the requested output format.

func New

func New(transforms ...Transformation) *Transformer

New creates a Transformer with optional transforms.

func (*Transformer) Add

func (transformer *Transformer) Add(transforms ...Transformation) *Transformer

Add appends transforms to the Transformer.

func (*Transformer) Transform

func (transformer *Transformer) Transform(data []byte, input Format, output Format) ([]byte, error)

Transform decodes data from the input format, applies transforms, and encodes it to the output format.

Jump to

Keyboard shortcuts

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