transform

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package transform defines immutable, strongly typed T-to-T transformations. It can be used independently of the root shape package and never validates.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapKey

type MapKey interface {
	~string |
		~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

MapKey is the set of map key types supported by Map.

type MapTransformer

type MapTransformer[K MapKey, V any] struct {
	// contains filtered or unexported fields
}

MapTransformer transforms a map and each key and value in stable key order.

func Map

func Map[K MapKey, V any](key Transformer[K], value Transformer[V]) MapTransformer[K, V]

Map creates a transformer that applies key and value in stable key order.

func (MapTransformer[K, V]) Apply

func (t MapTransformer[K, V]) Apply(fns ...func(map[K]V) (map[K]V, error)) MapTransformer[K, V]

Apply appends custom whole-map transformations.

func (MapTransformer[K, V]) ApplyContext

func (t MapTransformer[K, V]) ApplyContext(fns ...func(context.Context, map[K]V) (map[K]V, error)) MapTransformer[K, V]

ApplyContext appends context-aware whole-map transformations.

func (MapTransformer[K, V]) IfNull

func (t MapTransformer[K, V]) IfNull(v map[K]V) MapTransformer[K, V]

IfNull replaces a nil map with an isolated snapshot of v.

func (MapTransformer[K, V]) Then

func (t MapTransformer[K, V]) Then(vs ...Transformer[map[K]V]) Transformer[map[K]V]

Then composes this transformer with subsequent map transformers.

func (MapTransformer[K, V]) Transform

func (t MapTransformer[K, V]) Transform(v map[K]V) (map[K]V, error)

Transform runs the map pipeline with a background context.

func (MapTransformer[K, V]) TransformContext

func (t MapTransformer[K, V]) TransformContext(ctx context.Context, v map[K]V) (map[K]V, error)

TransformContext runs the map pipeline and observes cancellation.

type NumberTransformer

type NumberTransformer[N Numeric] struct{ ValueTransformer[N] }

NumberTransformer transforms a numeric value of type N.

func Duration

func Duration() NumberTransformer[types.Duration]

Duration creates a transformer for types.Duration.

func Float64

func Float64() NumberTransformer[float64]

Float64 creates a float64 transformer.

func Int

func Int() NumberTransformer[int]

Int creates an int transformer.

func Int64

func Int64() NumberTransformer[int64]

Int64 creates an int64 transformer.

func Number

func Number[N Numeric]() NumberTransformer[N]

Number creates a transformer for a supported numeric type.

func (NumberTransformer[N]) Apply

func (t NumberTransformer[N]) Apply(fns ...func(N) (N, error)) NumberTransformer[N]

Apply appends custom numeric transformations.

func (NumberTransformer[N]) ApplyContext

func (t NumberTransformer[N]) ApplyContext(fns ...func(context.Context, N) (N, error)) NumberTransformer[N]

ApplyContext appends context-aware custom numeric transformations.

func (NumberTransformer[N]) IfZero

func (t NumberTransformer[N]) IfZero(v N) NumberTransformer[N]

IfZero replaces numeric zero with v.

type Numeric

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}

Numeric is the set of supported signed, unsigned, and floating-point types.

type PointerTransformer

type PointerTransformer[T any] struct {
	// contains filtered or unexported fields
}

PointerTransformer transforms an optional *T and its non-nil value.

func Pointer

func Pointer[T any](inner Transformer[T]) PointerTransformer[T]

Pointer creates a transformer for *T that applies inner to non-nil values.

func (PointerTransformer[T]) Apply

func (t PointerTransformer[T]) Apply(fns ...func(*T) (*T, error)) PointerTransformer[T]

Apply appends custom pointer transformations.

func (PointerTransformer[T]) ApplyContext

func (t PointerTransformer[T]) ApplyContext(fns ...func(context.Context, *T) (*T, error)) PointerTransformer[T]

ApplyContext appends context-aware custom pointer transformations.

func (PointerTransformer[T]) IfNull

func (t PointerTransformer[T]) IfNull(v *T) PointerTransformer[T]

IfNull replaces nil with an isolated snapshot of v.

func (PointerTransformer[T]) Then

func (t PointerTransformer[T]) Then(vs ...Transformer[*T]) Transformer[*T]

Then composes this transformer with subsequent pointer transformers.

func (PointerTransformer[T]) Transform

func (t PointerTransformer[T]) Transform(v *T) (*T, error)

Transform runs the pointer pipeline with a background context.

func (PointerTransformer[T]) TransformContext

func (t PointerTransformer[T]) TransformContext(ctx context.Context, v *T) (*T, error)

TransformContext runs the pointer pipeline and observes cancellation.

type SliceTransformer

type SliceTransformer[T any] struct {
	// contains filtered or unexported fields
}

SliceTransformer transforms a slice and each of its elements.

func Slice

func Slice[T any](inner Transformer[T]) SliceTransformer[T]

Slice creates a transformer that applies inner to each element.

func (SliceTransformer[T]) Apply

func (t SliceTransformer[T]) Apply(fns ...func([]T) ([]T, error)) SliceTransformer[T]

Apply appends custom whole-slice transformations.

func (SliceTransformer[T]) ApplyContext

func (t SliceTransformer[T]) ApplyContext(fns ...func(context.Context, []T) ([]T, error)) SliceTransformer[T]

ApplyContext appends context-aware whole-slice transformations.

func (SliceTransformer[T]) IfNull

func (t SliceTransformer[T]) IfNull(v []T) SliceTransformer[T]

IfNull replaces a nil slice with an isolated snapshot of v.

func (SliceTransformer[T]) Then

func (t SliceTransformer[T]) Then(vs ...Transformer[[]T]) Transformer[[]T]

Then composes this transformer with subsequent slice transformers.

func (SliceTransformer[T]) Transform

func (t SliceTransformer[T]) Transform(v []T) ([]T, error)

Transform runs the slice pipeline with a background context.

func (SliceTransformer[T]) TransformContext

func (t SliceTransformer[T]) TransformContext(ctx context.Context, v []T) ([]T, error)

TransformContext runs the slice pipeline and observes cancellation.

type StringTransformer

type StringTransformer struct{ ValueTransformer[string] }

StringTransformer transforms strings with built-in normalization steps.

func String

func String() StringTransformer

String creates a string transformer.

Example
package main

import (
	"fmt"

	"github.com/rhevorn/shape/transform"
)

func main() {
	canonicalName := transform.String().Trim().ToLower()
	name, err := canonicalName.Transform(" Pong ")

	fmt.Println(name, err)
}
Output:
pong <nil>

func (StringTransformer) Apply

func (t StringTransformer) Apply(fns ...func(string) (string, error)) StringTransformer

Apply appends custom string transformations.

func (StringTransformer) ApplyContext

func (t StringTransformer) ApplyContext(fns ...func(context.Context, string) (string, error)) StringTransformer

ApplyContext appends context-aware custom string transformations.

func (StringTransformer) IfZero

IfZero replaces an empty string with v.

func (StringTransformer) LTrim

func (t StringTransformer) LTrim(chars ...string) StringTransformer

LTrim removes leading Unicode whitespace, or characters from one cutset.

func (StringTransformer) RTrim

func (t StringTransformer) RTrim(chars ...string) StringTransformer

RTrim removes trailing Unicode whitespace, or characters from one cutset.

func (StringTransformer) ToLower

ToLower converts letters to lower case.

func (StringTransformer) ToUpper

ToUpper converts letters to upper case.

func (StringTransformer) Trim

func (t StringTransformer) Trim(chars ...string) StringTransformer

Trim removes surrounding Unicode whitespace, or characters from one cutset.

type Transformer

type Transformer[T any] interface {
	Transform(T) (T, error)
	TransformContext(context.Context, T) (T, error)
}

Transformer transforms a T without mutating caller-owned input.

type ValueTransformer

type ValueTransformer[T any] struct {
	// contains filtered or unexported fields
}

ValueTransformer is an immutable transformation pipeline for any T.

func Bool

func Bool() ValueTransformer[bool]

Bool creates a bool transformer.

func Time

func Time() ValueTransformer[time.Time]

Time creates a time.Time transformer.

func Value

func Value[T any]() ValueTransformer[T]

Value creates a transformer for any T.

func (ValueTransformer[T]) Apply

func (t ValueTransformer[T]) Apply(fns ...func(T) (T, error)) ValueTransformer[T]

Apply appends custom transformation steps in declaration order.

func (ValueTransformer[T]) ApplyContext

func (t ValueTransformer[T]) ApplyContext(fns ...func(context.Context, T) (T, error)) ValueTransformer[T]

ApplyContext appends context-aware custom transformation steps.

func (ValueTransformer[T]) IfZero

func (t ValueTransformer[T]) IfZero(fallback T) ValueTransformer[T]

IfZero replaces the zero value with an isolated snapshot of fallback.

func (ValueTransformer[T]) Then

func (t ValueTransformer[T]) Then(values ...Transformer[T]) Transformer[T]

Then composes this transformer with subsequent transformers.

func (ValueTransformer[T]) Transform

func (t ValueTransformer[T]) Transform(v T) (T, error)

Transform runs the pipeline with a background context.

func (ValueTransformer[T]) TransformContext

func (t ValueTransformer[T]) TransformContext(ctx context.Context, v T) (T, error)

TransformContext runs the pipeline and observes context cancellation.

Jump to

Keyboard shortcuts

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