transform

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

blocks/transform/transform.go

Package transform provides declarative data mapping utilities for use inside flow steps. It eliminates the boilerplate of manual Bind + map[string]any construction when combining data from multiple enrichment sources, request fields, and computed values into a single payload.

All functions are pure — no I/O, no lifecycle, no core.Block registration. They are consumed directly inside flow.Transform and flow.Respond steps.

// Inside a flow.Transform step:
flow.NewStep("build-payload",
    flow.Transform(func(ctx context.Context, req *server.Request, s *flow.State) error {
        payload, err := transform.New(s, req).
            AllFrom("load-customer").               // spread all fields from state["load-customer"]
            Pick("load-credit", "available").       // pick one field from state["load-credit"]
            PathParam("order_id", "id").             // from URL :id
            Compute("status", func(s *flow.State, _ *server.Request) any {
                return "pending"
            }).
            Build()
        if err != nil { return err }
        s.Set("payload", payload)
        return nil
    }))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Merge

func Merge(state *flow.State, keys ...string) (map[string]any, error)

Merge combines the values of multiple state keys into a single flat map. Keys from later sources overwrite keys from earlier ones on collision.

merged, err := transform.Merge(state, "load-customer", "load-credit")

func Omit

func Omit(state *flow.State, key string, fields ...string) (map[string]any, error)

Omit returns all fields of the value stored under key, excluding the specified ones.

public, err := transform.Omit(state, "load-customer", "password", "internal_id")

func Pick

func Pick(state *flow.State, key string, fields ...string) (map[string]any, error)

Pick extracts only the specified fields from the value stored in state under key.

picked, err := transform.Pick(state, "load-customer", "id", "name", "tax_id")

func Rename

func Rename(state *flow.State, key string, mapping map[string]string) (map[string]any, error)

Rename returns all fields of the value stored under key, renaming fields according to the mapping (oldName → newName). Unmapped fields keep their names.

renamed, err := transform.Rename(state, "load-customer",
    map[string]string{"customer_type": "type", "tax_id": "cnpj"})

func Step

func Step(destKey string, fn func(*Builder) *Builder) flow.StepFn

Step returns a flow.StepFn that runs the builder function, stores the result in state under destKey, and passes any error back to the flow engine.

flow.NewStep("build-payload",
    transform.Step("payload", func(b *transform.Builder) *transform.Builder {
        return b.
            AllFrom("load-customer").
            Pick("load-credit", "available").
            PathParam("order_id", "id").
            Set("status", "pending")
    }))

Types

type Builder

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

Builder provides a fluent API for composing a map from multiple sources: state values, request parameters and computed values.

Instantiate with New, chain methods, and call Build or MustBuild at the end.

func New

func New(state *flow.State, req *server.Request) *Builder

New creates a Builder bound to the given state and request. Both are optional — pass nil for either if not needed.

func (*Builder) AllFrom

func (b *Builder) AllFrom(stateKey string) *Builder

AllFrom spreads all fields of state[key] into the output map. Existing keys are overwritten on collision.

.AllFrom("load-customer")   // adds id, name, email, type, ...

func (*Builder) Build

func (b *Builder) Build() (map[string]any, error)

Build returns the assembled map, or an aggregated error if any step failed.

func (*Builder) Compute

func (b *Builder) Compute(destKey string, fn func(*flow.State, *server.Request) any) *Builder

Compute derives a value from state and request via a function.

.Compute("total_with_tax", func(s *flow.State, _ *server.Request) any {
    var body OrderRequest
    s.Bind("__body__", &body)
    return body.Amount * 1.12
})

func (*Builder) Field

func (b *Builder) Field(destKey, stateKey, srcField string) *Builder

Field copies one field from state[stateKey] into the output under destKey. Useful for renaming or promoting nested values.

.Field("customer_name", "load-customer", "name")
.Field("credit",        "load-credit",   "available")

func (*Builder) Header

func (b *Builder) Header(destKey, headerName string) *Builder

Header copies a request header value into the output.

.Header("trace_id", "X-Request-Id")

func (*Builder) MustBuild

func (b *Builder) MustBuild() map[string]any

MustBuild returns the assembled map and panics on error. Use only in tests or when errors are structurally impossible.

func (*Builder) Omit

func (b *Builder) Omit(stateKey string, excluded ...string) *Builder

Omit spreads all fields of state[stateKey] except the excluded ones.

.Omit("load-customer", "internal_code", "raw_password")

func (*Builder) PathParam

func (b *Builder) PathParam(destKey, paramName string) *Builder

PathParam copies a URL path parameter (from :param) into the output.

.PathParam("order_id", "id")   // copies req.PathParam("id") → output["order_id"]

func (*Builder) Pick

func (b *Builder) Pick(stateKey string, fields ...string) *Builder

Pick copies only the specified fields from state[stateKey] into the output.

.Pick("load-customer", "id", "name")

func (*Builder) QueryParam

func (b *Builder) QueryParam(destKey, paramName string) *Builder

QueryParam copies a URL query string parameter into the output.

.QueryParam("page", "page")    // copies ?page=2 → output["page"]

func (*Builder) Set

func (b *Builder) Set(key string, value any) *Builder

Set adds a static key-value pair to the output.

.Set("status", "pending")
.Set("version", 2)

Jump to

Keyboard shortcuts

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