mapper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 1 Imported by: 0

README

类型映射器

类型映射器的作用是将一个数据结构的字段映射到另一个数据结构的字段,通常用于对象之间的数据转换。它可以简化不同类型或结构之间的数据传递,减少手动赋值的代码量,提高开发效率。例如,在处理数据库实体与业务模型或 API 请求/响应模型之间的转换时,类型映射器非常有用。

基于Copier的类型映射器

package main

import (
	"github.com/52dev/go-utils/mapper"
)

func main() {
	type DtoType struct {
		Name string
		Age  int
	}

	type EntityType struct {
		Name string
		Age  int
	}

	mapper := mapper.NewCopierMapper[DtoType, EntityType]()

	// 测试 ToEntity 方法
	dto := &DtoType{Name: "Alice", Age: 25}
	entity := mapper.ToEntity(dto)

	// 测试 ToEntity 方法,传入 nil
	entityNil := mapper.ToEntity(nil)

	// 测试 ToDTO 方法
	entity = &EntityType{Name: "Bob", Age: 30}
	dtoResult := mapper.ToDTO(entity)

	// 测试 ToDTO 方法,传入 nil
	dtoNil := mapper.ToDTO(nil)
}

ent 与 protobuf 的枚举类型映射器

package main

import "github.com/52dev/go-utils/mapper"

func main() {
	type DtoType int32
	type EntityType string

	const (
		DtoTypeOne DtoType = 1
		DtoTypeTwo DtoType = 2
	)

	const (
		EntityTypeOne EntityType = "One"
		EntityTypeTwo EntityType = "Two"
	)

	nameMap := map[int32]string{
		1: "One",
		2: "Two",
	}
	valueMap := map[string]int32{
		"One": 1,
		"Two": 2,
	}

	converter := mapper.NewEnumTypeConverter[DtoType, EntityType](nameMap, valueMap)

	// 测试 ToEntity 方法
	dto := DtoTypeOne
	entity := converter.ToEntity(&dto)

	// 测试 ToEntity 方法,传入不存在的值
	dtoInvalid := DtoType(3)
	entityInvalid := converter.ToEntity(&dtoInvalid)

	// 测试 ToDTO 方法
	tmpEntityTwo := EntityTypeTwo
	entity = &tmpEntityTwo
	dtoResult := converter.ToDTO(entity)

	// 测试 ToDTO 方法,传入不存在的值
	tmpEntityThree := EntityType("Three")
	entityInvalid = &tmpEntityThree
	dtoInvalidResult := converter.ToDTO(entityInvalid)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGenericTypeConverterPair

func NewGenericTypeConverterPair[A interface{}, B interface{}](
	srcType A,
	dstType B,
	fromFn func(src A) B,
	toFn func(src B) A,
) []copier.TypeConverter

Types

type CopierMapper

type CopierMapper[DTO any, ENTITY any] struct {
	// contains filtered or unexported fields
}

func NewCopierMapper

func NewCopierMapper[DTO any, ENTITY any]() *CopierMapper[DTO, ENTITY]

func (*CopierMapper[DTO, ENTITY]) AppendConverter

func (m *CopierMapper[DTO, ENTITY]) AppendConverter(converter copier.TypeConverter)

func (*CopierMapper[DTO, ENTITY]) AppendConverters

func (m *CopierMapper[DTO, ENTITY]) AppendConverters(converters []copier.TypeConverter)

func (*CopierMapper[DTO, ENTITY]) ToDTO

func (m *CopierMapper[DTO, ENTITY]) ToDTO(entity *ENTITY) *DTO

func (*CopierMapper[DTO, ENTITY]) ToEntity

func (m *CopierMapper[DTO, ENTITY]) ToEntity(dto *DTO) *ENTITY

type EnumTypeConverter

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

func NewEnumTypeConverter

func NewEnumTypeConverter[DTO ~int32, ENTITY ~string](
	nameMap map[int32]string,
	valueMap map[string]int32,
) *EnumTypeConverter[DTO, ENTITY]

func (*EnumTypeConverter[DTO, ENTITY]) NewConverterPair

func (m *EnumTypeConverter[DTO, ENTITY]) NewConverterPair() []copier.TypeConverter

func (*EnumTypeConverter[DTO, ENTITY]) ToDTO

func (m *EnumTypeConverter[DTO, ENTITY]) ToDTO(entity *ENTITY) *DTO

func (*EnumTypeConverter[DTO, ENTITY]) ToEntity

func (m *EnumTypeConverter[DTO, ENTITY]) ToEntity(dto *DTO) *ENTITY

type Mapper

type Mapper[DTO any, ENTITY any] interface {
	// ToEntity converts a DTO to a Database Entity.
	ToEntity(*DTO) *ENTITY

	// ToDTO converts a Database Entity to a DTO.
	ToDTO(*ENTITY) *DTO
}

Mapper defines the interface for converting between Data Transfer Objects (DTOs) and Database Entities.

Jump to

Keyboard shortcuts

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