Documentation
¶
Index ¶
- func AllBuiltinConverters() []copier.TypeConverter
- func ApplyPlan[P any, E any](plan *MapperPlan, m *CopierMapper[P, E], presets *PresetRegistry, ...) error
- func IsNil(v any) bool
- func NewGenericConverterPair[A any, B any](aToB func(A) (B, error), bToA func(B) (A, error)) []copier.TypeConverter
- func NewInt64PointerConverterPair() []copier.TypeConverter
- func NewIntInt32ConverterPair() []copier.TypeConverter
- func NewStringPointerConverterPair() []copier.TypeConverter
- func NewTimeConverterPair() []copier.TypeConverter
- func NewTimestamppbConverterPair() []copier.TypeConverter
- func NewUUIDStringConverterPair() []copier.TypeConverter
- func Ptr[T any](v T) *T
- func Val[T any](p *T) T
- type ConverterKind
- type CopierMapper
- func (m *CopierMapper[P, E]) AppendConverter(c copier.TypeConverter) *CopierMapper[P, E]
- func (m *CopierMapper[P, E]) AppendConverters(cs []copier.TypeConverter) *CopierMapper[P, E]
- func (m *CopierMapper[P, E]) MustToEntity(proto *P) *E
- func (m *CopierMapper[P, E]) MustToProto(entity *E) *P
- func (m *CopierMapper[P, E]) ToEntity(proto *P) (*E, error)
- func (m *CopierMapper[P, E]) ToEntityList(protos []*P) ([]*E, error)
- func (m *CopierMapper[P, E]) ToProto(entity *E) (*P, error)
- func (m *CopierMapper[P, E]) ToProtoList(entities []*E) ([]*P, error)
- func (m *CopierMapper[P, E]) WithFieldMapping(mapping map[string]string) *CopierMapper[P, E]
- func (m *CopierMapper[P, E]) WithPostToEntityHook(fn func(proto *P, entity *E) error) *CopierMapper[P, E]
- func (m *CopierMapper[P, E]) WithPostToProtoHook(fn func(entity *E, proto *P) error) *CopierMapper[P, E]
- type EnumConverter
- type HookRegistry
- type Mapper
- type MapperPlan
- type PresetRegistry
- type TypeConverter
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 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.
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 ¶
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 ¶
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 ¶
NewForwardMapper creates a unidirectional Mapper (A->B only). Calling Reverse/ReverseSlice on a forward-only Mapper returns nil.
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