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 ¶
- func Merge(state *flow.State, keys ...string) (map[string]any, error)
- func Omit(state *flow.State, key string, fields ...string) (map[string]any, error)
- func Pick(state *flow.State, key string, fields ...string) (map[string]any, error)
- func Rename(state *flow.State, key string, mapping map[string]string) (map[string]any, error)
- func Step(destKey string, fn func(*Builder) *Builder) flow.StepFn
- type Builder
- func (b *Builder) AllFrom(stateKey string) *Builder
- func (b *Builder) Build() (map[string]any, error)
- func (b *Builder) Compute(destKey string, fn func(*flow.State, *server.Request) any) *Builder
- func (b *Builder) Field(destKey, stateKey, srcField string) *Builder
- func (b *Builder) Header(destKey, headerName string) *Builder
- func (b *Builder) MustBuild() map[string]any
- func (b *Builder) Omit(stateKey string, excluded ...string) *Builder
- func (b *Builder) PathParam(destKey, paramName string) *Builder
- func (b *Builder) Pick(stateKey string, fields ...string) *Builder
- func (b *Builder) QueryParam(destKey, paramName string) *Builder
- func (b *Builder) Set(key string, value any) *Builder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Merge ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
New creates a Builder bound to the given state and request. Both are optional — pass nil for either if not needed.
func (*Builder) AllFrom ¶
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) Compute ¶
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 ¶
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 ¶
Header copies a request header value into the output.
.Header("trace_id", "X-Request-Id")
func (*Builder) MustBuild ¶
MustBuild returns the assembled map and panics on error. Use only in tests or when errors are structurally impossible.
func (*Builder) Omit ¶
Omit spreads all fields of state[stateKey] except the excluded ones.
.Omit("load-customer", "internal_code", "raw_password")
func (*Builder) PathParam ¶
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 ¶
Pick copies only the specified fields from state[stateKey] into the output.
.Pick("load-customer", "id", "name")
func (*Builder) QueryParam ¶
QueryParam copies a URL query string parameter into the output.
.QueryParam("page", "page") // copies ?page=2 → output["page"]