Skip to content

Commit 594e936

Browse files
committed
fix: Ignore custom functions for directly assignable types
1 parent 0b37c2e commit 594e936

File tree

3 files changed

+11
-39
lines changed

3 files changed

+11
-39
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mapper.Map(&to, from)
107107

108108
##### Conversion functions
109109

110-
They are used to convert one type into another. Conversion functions have the highest priority. The second argument is the current mapper instance and is optional.
110+
They are used to convert one type into another and have the highest priority, however they are not applied to fields of directly assignable structs. The second argument is the current mapper instance and is optional.
111111

112112
```go
113113
mapper.AddConvFunc(func(p RawPassword, mapper *Mapper) PasswordHash {
@@ -117,7 +117,7 @@ mapper.AddConvFunc(func(p RawPassword, mapper *Mapper) PasswordHash {
117117

118118
##### Inspection functions
119119

120-
Those are triggered _after_ a value has been successfully mapped. The value is **always taken by pointer**.
120+
Those are triggered _after_ a value has been successfully mapped. The value is **always taken by pointer**. Likewise to conversion functions, they are not called for fields of directly assignable structs.
121121

122122
```go
123123
mapper.AddInspectFunc(func(dto *UserDto) {

dto.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,6 @@ func errorFromReflectValue(rv reflect.Value) error {
9393
return err
9494
}
9595

96-
// isCompositeKind returns true if this type contains other types (is composite)
97-
func isCompositeKind(k reflect.Kind) bool {
98-
switch k {
99-
case reflect.Struct, reflect.Slice, reflect.Map:
100-
return true
101-
default:
102-
return false
103-
}
104-
}
105-
10696
// ==================================== Conversion and inspection functions ===
10797

10898
// Run inspect functions for (dst-src) pair
@@ -366,19 +356,16 @@ func (m *Mapper) mapValue(dstRv, srcRv reflect.Value) (returnError error) {
366356
return err
367357
}
368358

369-
// don't skip calling functions for assignable types
370-
if !m.HasCustomFuncs() || !isCompositeKind(fk) {
371-
// 2. Check direct assignment
372-
if srcRv.Type().AssignableTo(dstRv.Type()) {
373-
dstRv.Set(srcRv)
374-
return
375-
}
359+
// 2. Check direct assignment
360+
if srcRv.Type().AssignableTo(dstRv.Type()) {
361+
dstRv.Set(srcRv)
362+
return
363+
}
376364

377-
// 3. Check conversion
378-
if srcRv.Type().ConvertibleTo(dstRv.Type()) {
379-
dstRv.Set(srcRv.Convert(dstRv.Type()))
380-
return
381-
}
365+
// 3. Check conversion
366+
if srcRv.Type().ConvertibleTo(dstRv.Type()) {
367+
dstRv.Set(srcRv.Convert(dstRv.Type()))
368+
return
382369
}
383370

384371
// 4. Handle pointers by dereferencing from

dto_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,6 @@ func TestErrorPropagation(t *testing.T) {
306306
assert.Equal(t, testError, err)
307307
}
308308

309-
// Test custom functions are not skipped for assignable types
310-
func TestDontSkipFunctions(t *testing.T) {
311-
var outProduct Product
312-
testProduct := commonProducts[0]
313-
314-
testError := errors.New("Called!")
315-
316-
m := Mapper{}
317-
m.AddConvFunc(func(s string) (string, error) {
318-
return "", testError
319-
})
320-
err := m.Map(&outProduct, testProduct)
321-
assert.Equal(t, testError, err)
322-
}
323-
324309
func TestPointerCases(t *testing.T) {
325310
{
326311
var fromProduct = struct {

0 commit comments

Comments
 (0)