transform

package
v0.4.0 Latest Latest
Warning

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

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

Documentation

Overview

Package transform provides a small, composable layer to apply structural rewrites to syntetic/model graphs without polluting the core model or the builders. It defines a Transformer interface and helpers to compose and apply transformations to packages, types, and nodes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyNode

func ApplyNode(n mdl.Node, ts ...Transformer) mdl.Node

ApplyNode applies transformers to a node.

func ApplyPackage

func ApplyPackage(p *mdl.Package, ts ...Transformer) *mdl.Package

ApplyPackage applies transformers to a package.

func ApplyType

func ApplyType(t *mdl.Type, ts ...Transformer) *mdl.Type

ApplyType applies transformers to a type.

func LookupDeclName

func LookupDeclName(tr Transformer, t *mdl.Type) (string, bool)

LookupDeclName attempts to retrieve a decl-name override from a transformer, returning (name, true) when available.

Types

type DeclNameProvider

type DeclNameProvider interface {
	DeclNameFor(t *mdl.Type) (string, bool)
}

DeclNameProvider exposes a declaration-name override for model.Type values. Builders can use this to override the printed name without mutating the type.

type Nop

type Nop struct{}

Nop is a no-op transformer that returns inputs unchanged.

func (Nop) ApplyNode

func (Nop) ApplyNode(n mdl.Node) mdl.Node

func (Nop) ApplyPackage

func (Nop) ApplyPackage(p *mdl.Package) *mdl.Package

func (Nop) ApplyType

func (Nop) ApplyType(t *mdl.Type) *mdl.Type

type TagOption

type TagOption func(*TagTransformer)

TagOption configures the TagTransformer.

func WithFlatKey

func WithFlatKey(key string) TagOption

WithFlatKey sets the directive name for embedding/flattening (default: "flat").

func WithInlineKey

func WithInlineKey(key string) TagOption

WithInlineKey sets an additional directive alias for embedding/flattening (default: "inline").

func WithOmitKey

func WithOmitKey(key string) TagOption

WithOmitKey sets the directive name for omitting a field (default: "omit").

func WithRenameKey

func WithRenameKey(key string) TagOption

WithRenameKey sets the directive name for field rename (default: "rename").

func WithTagKey

func WithTagKey(key string) TagOption

WithTagKey sets the struct tag key to read directives from (default: "x").

func WithTagOverrideKey

func WithTagOverrideKey(key string) TagOption

WithTagOverrideKey sets the directive name used to override the field tag (default: "tag").

func WithTypeKey

func WithTypeKey(key string) TagOption

WithTypeKey sets the directive name for type override (default: "type").

type TagTransformer

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

TagTransformer applies struct-field level rewrites based on a configurable tag namespace (default key: x). Supported directives:

  • type: fully-qualified type path "pkg/import/path.Name" to override field type
  • rename: new field name for emission
  • omit: drop this field from emission
  • flat / inline: mark field as embedded (flatten)
  • tag: override field tag for emission (raw literal without backticks)

The transformer operates on model.Nodes. For *model.Struct it evaluates and applies directives to each field; it recurses into nested nodes.

func (*TagTransformer) ApplyNode

func (t *TagTransformer) ApplyNode(n mdl.Node) mdl.Node

func (*TagTransformer) ApplyPackage

func (t *TagTransformer) ApplyPackage(p *mdl.Package) *mdl.Package

func (*TagTransformer) ApplyType

func (t *TagTransformer) ApplyType(tp *mdl.Type) *mdl.Type

type Transformer

type Transformer interface {
	// ApplyPackage applies this transform to a package. Implementations may
	// walk files, types, and fields as needed.
	ApplyPackage(p *mdl.Package) *mdl.Package
	// ApplyType applies this transform to a single type.
	ApplyType(t *mdl.Type) *mdl.Type
	// ApplyNode applies this transform to a node.
	ApplyNode(n mdl.Node) mdl.Node
}

Transformer applies structural rewrites to model graphs. Implementations may operate in-place or return new values. Callers should not rely on mutation semantics; always use the returned value.

func Compose

func Compose(ts ...Transformer) Transformer

Compose builds a transformer that applies the provided transformers in order. Nil transformers are ignored. When no transformers are supplied, a no-op transformer is returned.

func DeclNameOverride

func DeclNameOverride(fn func(*mdl.Type) (string, bool)) Transformer

DeclNameOverride returns a Transformer that provides a declaration-name override for types using the supplied function.

func FieldRewriter

func FieldRewriter(fn func(mdl.Field) (mdl.Field, bool)) Transformer

FieldRewriter returns a Transformer that applies fn to all fields within struct nodes, allowing field modification or removal (return ok=false to drop).

func FromTags

func FromTags(opts ...TagOption) Transformer

FromTags returns a transformer that applies tag-driven rewrites.

func NodeRewriter

func NodeRewriter(fn func(mdl.Node) mdl.Node) Transformer

NodeRewriter returns a Transformer that applies fn to every node, recursing into child nodes where applicable.

func TypeRewriter

func TypeRewriter(fn func(*mdl.Type) *mdl.Type) Transformer

TypeRewriter returns a Transformer that applies fn to types.

Jump to

Keyboard shortcuts

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