mold

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidDive describes an invalid dive tag configuration.
	ErrInvalidDive = errors.New("invalid dive tag configuration")

	// ErrUndefinedKeysTag describes an undefined keys tag when endkeys tag is defined.
	ErrUndefinedKeysTag = errors.New("'" + endKeysTag + "' tag encountered without a corresponding '" + keysTag + "' tag")

	// ErrInvalidKeysTag describes a misuse of the keys tag.
	ErrInvalidKeysTag = errors.New("'" + keysTag + "' tag must be immediately preceded by the '" + diveTag + "' tag")
)
View Source
var (
	// ErrTranslatedFieldNotFound is returned when the target translated field (e.g., StatusName) is not found.
	ErrTranslatedFieldNotFound = errors.New("target translated field not found")
	// ErrTranslationKindEmpty is returned when the translation kind parameter is missing.
	ErrTranslationKindEmpty = errors.New("translation kind parameter is empty")
	// ErrTranslatedFieldNotSettable is returned when the target translated field cannot be set.
	ErrTranslatedFieldNotSettable = errors.New("target translated field is not settable")
	// ErrNoTranslatorSupportsKind is returned when no translator supports the given kind.
	ErrNoTranslatorSupportsKind = errors.New("no translator supports the given kind")
	// ErrUnsupportedFieldType is returned when the field type is not supported for translation.
	ErrUnsupportedFieldType = errors.New("unsupported field type for translation")
)
View Source
var ErrDictionaryResolverNotConfigured = errors.New("dictionary resolver is not configured, please provide one in the container")

ErrDictionaryResolverNotConfigured is returned when DictionaryResolver is not provided.

View Source
var Module = fx.Module(
	"vef:mold",
	fx.Decorate(
		fx.Annotate(
			func(loader mold.DictionaryLoader, bus event.Bus) mold.DictionaryResolver {
				if loader == nil {
					return nil
				}

				return mold.NewCachedDictionaryResolver(loader, bus)
			},
			fx.ParamTags(`optional:"true"`),
		),
	),
	fx.Provide(

		fx.Annotate(
			NewTransformer,
			fx.ParamTags(`group:"vef:mold:field_transformers"`, `group:"vef:mold:struct_transformers"`, `group:"vef:mold:interceptors"`),
		),

		fx.Annotate(
			NewTranslateTransformer,
			fx.ParamTags(`group:"vef:mold:translators"`),
			fx.ResultTags(`group:"vef:mold:field_transformers"`),
		),

		fx.Annotate(
			NewDictionaryTranslator,
			fx.ParamTags(`optional:"true"`),
			fx.ResultTags(`group:"vef:mold:translators"`),
		),
	),
)

Module defines the fx module for the transformer package.

Functions

func NewDictionaryTranslator added in v0.24.0

func NewDictionaryTranslator(resolver mold.DictionaryResolver) mold.Translator

NewDictionaryTranslator creates a data dictionary translator instance.

func NewTransformer

func NewTransformer(fieldTransformers []mold.FieldTransformer, structTransformers []mold.StructTransformer, interceptors []mold.Interceptor) mold.Transformer

NewTransformer creates a new transformer instance, integrating all registered transformers and interceptors.

func NewTranslateTransformer

func NewTranslateTransformer(translators []mold.Translator) mold.FieldTransformer

NewTranslateTransformer creates a translate transformer instance.

Types

type DictionaryTranslator added in v0.24.0

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

DictionaryTranslator is a data dictionary translator that converts code values to readable names.

func (*DictionaryTranslator) Supports added in v0.24.0

func (*DictionaryTranslator) Supports(kind string) bool

func (*DictionaryTranslator) Translate added in v0.24.0

func (t *DictionaryTranslator) Translate(ctx context.Context, kind, value string) (string, error)

type ErrInvalidTag

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

ErrInvalidTag defines a bad value for a tag being used.

func (*ErrInvalidTag) Error

func (e *ErrInvalidTag) Error() string

type ErrInvalidTransformValue

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

ErrInvalidTransformValue describes an invalid argument passed to Struct or Field.

func (*ErrInvalidTransformValue) Error

func (e *ErrInvalidTransformValue) Error() string

type ErrInvalidTransformation

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

ErrInvalidTransformation describes an invalid argument passed to Struct or Field.

func (*ErrInvalidTransformation) Error

func (e *ErrInvalidTransformation) Error() string

type ErrUndefinedTag

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

ErrUndefinedTag defines a tag that does not exist.

func (*ErrUndefinedTag) Error

func (e *ErrUndefinedTag) Error() string

type MoldFieldLevel

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

func (*MoldFieldLevel) Field

func (f *MoldFieldLevel) Field() reflect.Value

func (*MoldFieldLevel) Name

func (f *MoldFieldLevel) Name() string

func (*MoldFieldLevel) Param

func (f *MoldFieldLevel) Param() string

func (*MoldFieldLevel) Parent

func (f *MoldFieldLevel) Parent() reflect.Value

func (*MoldFieldLevel) SiblingField

func (f *MoldFieldLevel) SiblingField(name string) (reflect.Value, bool)

func (*MoldFieldLevel) Struct added in v0.28.0

func (f *MoldFieldLevel) Struct() reflect.Value

func (*MoldFieldLevel) Transformer

func (f *MoldFieldLevel) Transformer() mold.Transformer

type MoldStructLevel

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

func (MoldStructLevel) Parent

func (s MoldStructLevel) Parent() reflect.Value

func (MoldStructLevel) Struct

func (s MoldStructLevel) Struct() reflect.Value

func (MoldStructLevel) Transformer

func (s MoldStructLevel) Transformer() mold.Transformer

type MoldTransformer

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

MoldTransformer is the base controlling object which contains all necessary information.

func New

func New() *MoldTransformer

New creates a new Transform object with default tag name of 'mold'.

func (*MoldTransformer) Field

func (t *MoldTransformer) Field(ctx context.Context, v any, tags string) error

Field applies the provided transformations against the variable.

func (*MoldTransformer) Register

func (t *MoldTransformer) Register(tag string, fn mold.Func)

Register adds a transformation with the given tag.

NOTE: This method is not thread-safe; register all transformations before use.

func (*MoldTransformer) RegisterAlias

func (t *MoldTransformer) RegisterAlias(alias, tags string)

RegisterAlias registers a mapping of a single transform tag that defines a common or complex set of transformations.

NOTE: This method is not thread-safe; register all aliases before use.

func (*MoldTransformer) RegisterInterceptor

func (t *MoldTransformer) RegisterInterceptor(fn mold.InterceptorFunc, types ...any)

RegisterInterceptor registers interceptor functions against one or more types. InterceptorFunc allows intercepting incoming values to redirect modifications to an inner type/value.

NOTE: This method is not thread-safe; register all interceptors before use.

func (*MoldTransformer) RegisterStructLevel

func (t *MoldTransformer) RegisterStructLevel(fn mold.StructLevelFunc, types ...any)

RegisterStructLevel registers a StructLevelFunc against a number of types.

NOTE: This method is not thread-safe; register all struct-level functions before use.

func (*MoldTransformer) Struct

func (t *MoldTransformer) Struct(ctx context.Context, v any) error

Struct applies transformations against the provided struct.

type TranslateTransformer

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

TranslateTransformer is a translator-based transformer that converts values to readable names Supports multiple translators and delegates to the appropriate one based on translation kind (from tag parameters).

func (*TranslateTransformer) Tag

Tag returns the transformer tag name "translate".

func (*TranslateTransformer) Transform

func (t *TranslateTransformer) Transform(ctx context.Context, fl mold.FieldLevel) error

Transform executes translation transformation logic. The engine dereferences pointer sources via extractType before this runs, so the dispatch sees an already-unwrapped value and matches []string for the slice path (a *[]string source arrives here as []string); everything else takes the scalar path. Both paths share the same `<Field>Name` sibling convention and translator selection rules.

Jump to

Keyboard shortcuts

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