mapper

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllBuiltinConverters

func AllBuiltinConverters() []copier.TypeConverter

AllBuiltinConverters 返回所有内置转换器

func ApplyPlan

func ApplyPlan[P any, E any](plan *MapperPlan, m *CopierMapper[P, E], presets *PresetRegistry, hooks *HookRegistry) error

ApplyPlan configures a CopierMapper according to the given plan. Go does not support type parameters on methods, so this is a package-level function.

func IsNil

func IsNil(v any) bool

IsNil 检查 any 是否为 nil(包括值为 nil 的指针)

func NewGenericConverterPair

func NewGenericConverterPair[A any, B any](
	aToB func(A) (B, error),
	bToA func(B) (A, error),
) []copier.TypeConverter

NewGenericConverterPair 创建任意类型之间的自定义转换器对

func NewInt64PointerConverterPair

func NewInt64PointerConverterPair() []copier.TypeConverter

NewInt64PointerConverterPair 创建 int64 与 *int64 之间的转换器对

func NewIntInt32ConverterPair

func NewIntInt32ConverterPair() []copier.TypeConverter

NewIntInt32ConverterPair creates converters between int and int32. Covers the common case where ent uses int and proto uses int32.

func NewStringPointerConverterPair

func NewStringPointerConverterPair() []copier.TypeConverter

NewStringPointerConverterPair 创建 string 与 *string 之间的转换器对

func NewTimeConverterPair

func NewTimeConverterPair() []copier.TypeConverter

NewTimeConverterPair 创建 time.Time 与 *time.Time 之间的转换器对

func NewTimestamppbConverterPair

func NewTimestamppbConverterPair() []copier.TypeConverter

NewTimestamppbConverterPair 创建 time.Time 与 *timestamppb.Timestamp 之间的转换器对 用于 protobuf 时间类型转换

func NewUUIDStringConverterPair

func NewUUIDStringConverterPair() []copier.TypeConverter

NewUUIDStringConverterPair creates converters between uuid.UUID and string. Covers the common case where ent uses uuid.UUID for primary keys and proto uses string.

func Ptr

func Ptr[T any](v T) *T

Ptr 返回值的指针(辅助函数)

func Val

func Val[T any](p *T) T

Val 返回指针的值,如果为 nil 则返回零值(辅助函数)

Types

type ConverterKind

type ConverterKind string

ConverterKind identifies a built-in converter that the runtime can resolve. Values mirror the proto enum mapper.v1.ConverterKind.

const (
	ConverterTimestampTime ConverterKind = "TIMESTAMP_TIME"
	ConverterTimePTR       ConverterKind = "TIME_PTR"
	ConverterStringPTR     ConverterKind = "STRING_PTR"
	ConverterInt64PTR      ConverterKind = "INT64_PTR"
	ConverterEnumString    ConverterKind = "ENUM_STRING"
	ConverterUUIDString    ConverterKind = "UUID_STRING"
	ConverterIntInt32      ConverterKind = "INT_INT32"
)

type CopierMapper

type CopierMapper[P any, E any] struct {
	// contains filtered or unexported fields
}

CopierMapper is a reflection-based bidirectional mapper between Proto/domain type P and Entity/storage type E. Type parameter order: P = proto/domain side, E = entity/storage side.

func NewCopierMapper

func NewCopierMapper[P any, E any]() *CopierMapper[P, E]

func (*CopierMapper[P, E]) AppendConverter

func (m *CopierMapper[P, E]) AppendConverter(c copier.TypeConverter) *CopierMapper[P, E]

func (*CopierMapper[P, E]) AppendConverters

func (m *CopierMapper[P, E]) AppendConverters(cs []copier.TypeConverter) *CopierMapper[P, E]

func (*CopierMapper[P, E]) MustToEntity

func (m *CopierMapper[P, E]) MustToEntity(proto *P) *E

MustToEntity converts proto to entity, panics on error.

func (*CopierMapper[P, E]) MustToProto

func (m *CopierMapper[P, E]) MustToProto(entity *E) *P

MustToProto converts entity to proto, panics on error.

func (*CopierMapper[P, E]) ToEntity

func (m *CopierMapper[P, E]) ToEntity(proto *P) (*E, error)

ToEntity converts proto P to entity E. Returns (nil, nil) when input is nil.

func (*CopierMapper[P, E]) ToEntityList

func (m *CopierMapper[P, E]) ToEntityList(protos []*P) ([]*E, error)

func (*CopierMapper[P, E]) ToProto

func (m *CopierMapper[P, E]) ToProto(entity *E) (*P, error)

ToProto converts entity E to proto P. Returns (nil, nil) when input is nil.

func (*CopierMapper[P, E]) ToProtoList

func (m *CopierMapper[P, E]) ToProtoList(entities []*E) ([]*P, error)

func (*CopierMapper[P, E]) WithFieldMapping

func (m *CopierMapper[P, E]) WithFieldMapping(mapping map[string]string) *CopierMapper[P, E]

func (*CopierMapper[P, E]) WithPostToEntityHook

func (m *CopierMapper[P, E]) WithPostToEntityHook(fn func(proto *P, entity *E) error) *CopierMapper[P, E]

WithPostToEntityHook registers a function that runs after copier completes in ToEntity.

func (*CopierMapper[P, E]) WithPostToProtoHook

func (m *CopierMapper[P, E]) WithPostToProtoHook(fn func(entity *E, proto *P) error) *CopierMapper[P, E]

WithPostToProtoHook registers a function that runs after copier completes in ToProto. Use for field-level transformations that copier cannot handle (e.g. map[string]any -> structured proto message).

type EnumConverter

type EnumConverter[DTO ~int32, Entity ~string] struct {
	// contains filtered or unexported fields
}

EnumConverter 枚举类型转换器 DTO 通常是 int32 (protobuf enum) Entity 通常是 string (数据库存储)

func NewEnumConverter

func NewEnumConverter[DTO ~int32, Entity ~string](
	nameMap map[int32]string,
	valueMap map[string]int32,
) *EnumConverter[DTO, Entity]

NewEnumConverter 创建枚举转换器 nameMap: protobuf 生成的 XXX_name map valueMap: protobuf 生成的 XXX_value map

func (*EnumConverter[DTO, Entity]) NewConverterPair

func (c *EnumConverter[DTO, Entity]) NewConverterPair() []copier.TypeConverter

NewConverterPair 生成 copier.TypeConverter 对

func (*EnumConverter[DTO, Entity]) ToDomain

func (c *EnumConverter[DTO, Entity]) ToDomain(entity *Entity) *DTO

ToDomain 将 Entity 字符串转换为 DTO 枚举值

func (*EnumConverter[DTO, Entity]) ToEntity

func (c *EnumConverter[DTO, Entity]) ToEntity(dto *DTO) *Entity

ToEntity 将 DTO 枚举值转换为 Entity 字符串

type HookRegistry

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

HookRegistry manages named custom converter hooks. Repos register concrete implementations; generated code references hooks by name.

func NewHookRegistry

func NewHookRegistry() *HookRegistry

func (*HookRegistry) CheckRequired

func (r *HookRegistry) CheckRequired(names ...string) error

CheckRequired verifies all required hook names are registered.

func (*HookRegistry) Get

func (r *HookRegistry) Get(name string) ([]copier.TypeConverter, bool)

func (*HookRegistry) MustGet

func (r *HookRegistry) MustGet(name string) []copier.TypeConverter

func (*HookRegistry) Register

func (r *HookRegistry) Register(name string, converters ...copier.TypeConverter)

type Mapper

type Mapper[A, B any] struct {
	// contains filtered or unexported fields
}

Mapper provides type-safe, nil-safe conversion between two types.

func NewForwardMapper

func NewForwardMapper[A, B any](aToB func(*A) *B) *Mapper[A, B]

NewForwardMapper creates a unidirectional Mapper (A->B only). Calling Reverse/ReverseSlice on a forward-only Mapper returns nil.

func NewMapper

func NewMapper[A, B any](aToB func(*A) *B, bToA func(*B) *A) *Mapper[A, B]

NewMapper creates a bidirectional Mapper (A->B and B->A).

func (*Mapper[A, B]) Map

func (m *Mapper[A, B]) Map(a *A) *B

Map converts A to B. Returns nil when input is nil.

func (*Mapper[A, B]) MapSlice

func (m *Mapper[A, B]) MapSlice(as []*A) []*B

MapSlice batch-converts []*A to []*B, skipping nil entries.

func (*Mapper[A, B]) Reverse

func (m *Mapper[A, B]) Reverse(b *B) *A

Reverse converts B to A. Returns nil when input is nil or Mapper is forward-only.

func (*Mapper[A, B]) ReverseSlice

func (m *Mapper[A, B]) ReverseSlice(bs []*B) []*A

ReverseSlice batch-converts []*B to []*A, skipping nil entries.

type MapperPlan

type MapperPlan struct {
	Presets         []string
	FieldMapping    map[string]string
	FieldConverters map[string]ConverterKind
	IgnoredFields   []string
	CustomHooks     []string
}

MapperPlan is a declarative specification for how to configure a CopierMapper. Generated code produces MapperPlan values; runtime applies them.

func (*MapperPlan) Validate

func (p *MapperPlan) Validate(presets *PresetRegistry, hooks *HookRegistry) error

Validate checks that all referenced presets, field converters, and hooks exist without modifying any mapper.

type PresetRegistry

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

PresetRegistry manages named groups of converters.

func DefaultPresets

func DefaultPresets() *PresetRegistry

DefaultPresets returns a PresetRegistry with all built-in presets registered.

func NewPresetRegistry

func NewPresetRegistry() *PresetRegistry

func (*PresetRegistry) Collect

func (r *PresetRegistry) Collect(names ...string) ([]copier.TypeConverter, error)

Collect gathers converters from multiple presets; returns error listing any missing.

func (*PresetRegistry) Get

func (r *PresetRegistry) Get(name string) ([]copier.TypeConverter, bool)

func (*PresetRegistry) Register

func (r *PresetRegistry) Register(name string, factory func() []copier.TypeConverter)

func (*PresetRegistry) RegisterDefaults

func (r *PresetRegistry) RegisterDefaults()

RegisterDefaults registers all built-in presets.

type TypeConverter

type TypeConverter = copier.TypeConverter

Jump to

Keyboard shortcuts

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