Skip to content

Commit 91c6df1

Browse files
committed
Return error from Rename function
Rename should return also an error, which will be wrapped and returned from Mapper.MapAny method. This is the only way to stop the conversion when there is something wrong with renaming.
1 parent 281810e commit 91c6df1

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

defaults.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ func acceptAllFields(string, Element) (bool, error) {
77
return true, nil
88
}
99

10-
func noRename(_ string, e Element) string {
11-
return e.Name()
10+
func noRename(_ string, e Element) (string, error) {
11+
return e.Name(), nil
1212
}
1313

1414
func interfaceValue(_ string, e Element) (interface{}, error) {

mapify.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ type Mapper struct {
2121
// and wrapped error is returned from Mapper.MapAny method.
2222
type Filter func(path string, e Element) (bool, error)
2323

24-
// Rename renames element name.
25-
type Rename func(path string, e Element) string
24+
// Rename renames element name. If error is returned then the whole conversion is aborted
25+
// and wrapped error is returned from Mapper.MapAny method.
26+
type Rename func(path string, e Element) (string, error)
2627

2728
// MapValue maps (transforms) element value. If error is returned then the whole conversion is aborted
2829
// and wrapped error is returned from Mapper.MapAny method.
@@ -96,7 +97,11 @@ func (i Mapper) mapStruct(path string, reflectValue reflect.Value) (map[string]i
9697
}
9798

9899
if accepted {
99-
renamed := i.Rename(fieldPath, element)
100+
renamed, err := i.Rename(fieldPath, element)
101+
if err != nil {
102+
return nil, fmt.Errorf("Rename failed: %w", err)
103+
}
104+
100105
mappedValue, err := i.MapValue(fieldPath, element)
101106
if err != nil {
102107
return nil, fmt.Errorf("MapValue failed: %w", err)

mapify_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,13 @@ func TestFilter(t *testing.T) {
417417
assert.Nil(t, result)
418418
assert.ErrorIs(t, actualErr, givenError)
419419
})
420-
421420
}
422421

423422
func TestRename(t *testing.T) {
424423
t.Run("should rename struct field", func(t *testing.T) {
425424
mapper := mapify.Mapper{
426-
Rename: func(path string, e mapify.Element) string {
427-
return "newName"
425+
Rename: func(path string, e mapify.Element) (string, error) {
426+
return "newName", nil
428427
},
429428
}
430429
// when
@@ -440,6 +439,20 @@ func TestRename(t *testing.T) {
440439
}
441440
assert.Equal(t, expected, v)
442441
})
442+
443+
t.Run("should return error when Rename returned error", func(t *testing.T) {
444+
givenError := stringError("err")
445+
mapper := mapify.Mapper{
446+
Rename: func(path string, e mapify.Element) (string, error) {
447+
return e.Name(), givenError
448+
},
449+
}
450+
// when
451+
result, actualErr := mapper.MapAny(struct{ Field string }{})
452+
// then
453+
assert.Nil(t, result)
454+
assert.ErrorIs(t, actualErr, givenError)
455+
})
443456
}
444457

445458
func TestMapValue(t *testing.T) {

0 commit comments

Comments
 (0)