From 38f3cb8a3b15546578d24aa9249cbe03d4876ca8 Mon Sep 17 00:00:00 2001 From: Bobo Date: Mon, 16 Jun 2025 11:59:09 +0800 Subject: [PATCH] feat: fix mapper bug. --- mapper/enum_converter.go | 12 ++++++------ mapper/mapper_test.go | 20 ++++++++++---------- tag.bat | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/mapper/enum_converter.go b/mapper/enum_converter.go index 4d3a268..5710f06 100644 --- a/mapper/enum_converter.go +++ b/mapper/enum_converter.go @@ -4,12 +4,12 @@ import ( "github.com/jinzhu/copier" ) -type EnumTypeConverter[DTO ~string, MODEL ~int32] struct { +type EnumTypeConverter[DTO ~int32, MODEL ~string] struct { nameMap map[int32]string valueMap map[string]int32 } -func NewEnumTypeConverter[DTO ~string, MODEL ~int32](nameMap map[int32]string, valueMap map[string]int32) *EnumTypeConverter[DTO, MODEL] { +func NewEnumTypeConverter[DTO ~int32, MODEL ~string](nameMap map[int32]string, valueMap map[string]int32) *EnumTypeConverter[DTO, MODEL] { return &EnumTypeConverter[DTO, MODEL]{ valueMap: valueMap, nameMap: nameMap, @@ -21,7 +21,7 @@ func (m *EnumTypeConverter[DTO, MODEL]) ToModel(dto *DTO) *MODEL { return nil } - find, ok := m.valueMap[string(*dto)] + find, ok := m.nameMap[int32(*dto)] if !ok { return nil } @@ -35,7 +35,7 @@ func (m *EnumTypeConverter[DTO, MODEL]) ToDto(model *MODEL) *DTO { return nil } - find, ok := m.nameMap[int32(*model)] + find, ok := m.valueMap[string(*model)] if !ok { return nil } @@ -45,8 +45,8 @@ func (m *EnumTypeConverter[DTO, MODEL]) ToDto(model *MODEL) *DTO { } func (m *EnumTypeConverter[DTO, MODEL]) NewConverterPair() []copier.TypeConverter { - srcType := MODEL(0) - dstType := DTO("") + srcType := MODEL("") + dstType := DTO(0) fromFn := m.ToDto toFn := m.ToModel diff --git a/mapper/mapper_test.go b/mapper/mapper_test.go index 30b88ff..a038069 100644 --- a/mapper/mapper_test.go +++ b/mapper/mapper_test.go @@ -43,17 +43,17 @@ func TestCopierMapper(t *testing.T) { } func TestEnumTypeConverter(t *testing.T) { - type DtoType string - type ModelType int32 + type DtoType int32 + type ModelType string const ( - DtoTypeOne DtoType = "One" - DtoTypeTwo DtoType = "Two" + DtoTypeOne DtoType = 1 + DtoTypeTwo DtoType = 2 ) const ( - ModelTypeOne ModelType = 1 - ModelTypeTwo ModelType = 2 + ModelTypeOne ModelType = "One" + ModelTypeTwo ModelType = "Two" ) nameMap := map[int32]string{ @@ -71,10 +71,10 @@ func TestEnumTypeConverter(t *testing.T) { dto := DtoTypeOne model := converter.ToModel(&dto) assert.NotNil(t, model) - assert.Equal(t, int32(1), int32(*model)) + assert.Equal(t, "One", string(*model)) // 测试 ToModel 方法,传入不存在的值 - dtoInvalid := DtoType("Three") + dtoInvalid := DtoType(3) modelInvalid := converter.ToModel(&dtoInvalid) assert.Nil(t, modelInvalid) @@ -83,10 +83,10 @@ func TestEnumTypeConverter(t *testing.T) { model = &tmpModelTwo dtoResult := converter.ToDto(model) assert.NotNil(t, dtoResult) - assert.Equal(t, "Two", string(*dtoResult)) + assert.Equal(t, DtoType(2), *dtoResult) // 测试 ToDto 方法,传入不存在的值 - tmpModelThree := ModelType(3) + tmpModelThree := ModelType("Three") modelInvalid = &tmpModelThree dtoInvalidResult := converter.ToDto(modelInvalid) assert.Nil(t, dtoInvalidResult) diff --git a/tag.bat b/tag.bat index a4b5546..2a554cf 100644 --- a/tag.bat +++ b/tag.bat @@ -8,7 +8,7 @@ git tag jwtutil/v0.0.2 git tag id/v0.0.2 git tag slug/v0.0.1 git tag name_generator/v0.0.1 -git tag mapper/v0.0.1 +git tag mapper/v0.0.2 git tag password/v0.0.1 git tag entgo/v1.1.31