Skip to content

Commit 3c8308d

Browse files
committed
added new option for ignore field
1 parent 8327fce commit 3c8308d

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

auto.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gomapper
22

33
import (
44
"reflect"
5+
"slices"
56

67
"github.com/insei/fmap/v3"
78
)
@@ -33,6 +34,9 @@ func AutoRoute[TSource, TDest any | []any](opts ...Option) error {
3334
if destFld.GetType() != srcFld.GetType() {
3435
continue
3536
}
37+
if slices.Contains(opt.Excluded, srcFld) {
38+
continue
39+
}
3640
if err := setFieldRecursive(srcFld, destFld, source, dest); err != nil {
3741
return err
3842
}

auto_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
type AutoMappingStructSource struct {
12+
Age int
1213
Name string
1314
Time time.Time
1415
UUID uuid.UUID
@@ -18,10 +19,11 @@ type AutoMappingStructSource struct {
1819
}
1920

2021
type AutoMappingStructDest struct {
22+
UUID uuid.UUID
23+
Age int
2124
Name string
2225
SecondName string
2326
Time time.Time
24-
UUID uuid.UUID
2527
PtrTime *time.Time
2628
PtrUUID *uuid.UUID
2729
NestedStruct NestedStructDest
@@ -70,15 +72,24 @@ func TestAutoRoute(t *testing.T) {
7072
dest.PtrTime = &timeNow
7173
}
7274
dest.Time = timeNow
73-
}))
75+
}),
76+
WithFieldSkip(func(dest *AutoMappingStructSource) any {
77+
return &dest.UUID
78+
}),
79+
WithFieldSkip(func(dest *AutoMappingStructSource) any {
80+
return &dest.Age
81+
}),
82+
)
7483
t.Run("Auto route with options", func(t *testing.T) {
75-
source := &AutoMappingStructSource{Name: "Test1"}
84+
source := &AutoMappingStructSource{UUID: uuid.New(), Name: "Test1", Age: 25}
7685
dest, err := MapTo[AutoMappingStructDest](source)
7786
assert.NoError(t, err)
7887
assert.Equal(t, source.Name, dest.Name)
7988
assert.Equal(t, "Test2", dest.SecondName)
8089
assert.Equal(t, timeNow, *dest.PtrTime)
8190
assert.Equal(t, timeNow, dest.Time)
91+
assert.Equal(t, 0, dest.Age)
92+
assert.Equal(t, uuid.Nil, dest.UUID)
8293
})
8394
t.Run("Auto mapping struct fields", func(t *testing.T) {
8495
source := &AutoMappingStructSource{

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/insei/gomapper
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/google/uuid v1.6.0
7-
github.com/insei/fmap/v3 v3.0.0
7+
github.com/insei/fmap/v3 v3.1.2
88
github.com/stretchr/testify v1.9.0
99
)
1010

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
44
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
55
github.com/insei/fmap/v3 v3.0.0 h1:BpRsFgQ2nt5tl/8tzp6y+MWIAJhqCTeKBHC56L/Um30=
66
github.com/insei/fmap/v3 v3.0.0/go.mod h1:Kk0gs7nKb4E/JycKJFnrsX5hlyBBe0yetGKFCJG0vzk=
7+
github.com/insei/fmap/v3 v3.1.2 h1:ZBr+WiZpIxFNeMo2X4QOST4AFl0sGAkG+EO08Ved3bY=
8+
github.com/insei/fmap/v3 v3.1.2/go.mod h1:Kk0gs7nKb4E/JycKJFnrsX5hlyBBe0yetGKFCJG0vzk=
79
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
810
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
911
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

options.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package gomapper
22

3+
import (
4+
"github.com/insei/fmap/v3"
5+
)
6+
37
type withFuncOption[TSource, TDest any] struct {
48
fn func(TSource, *TDest)
59
}
610

11+
type withFieldSkip[TDest any] struct {
12+
field fmap.Field
13+
}
14+
715
type options struct {
8-
Fns []any
16+
Fns []any
17+
Excluded []fmap.Field
918
}
1019

1120
type Option interface {
@@ -16,6 +25,31 @@ func (a withFuncOption[TSource, TDest]) apply(opts *options) {
1625
opts.Fns = append(opts.Fns, a.fn)
1726
}
1827

28+
func (a withFieldSkip[TDest]) apply(opts *options) {
29+
if opts.Excluded == nil {
30+
opts.Excluded = make([]fmap.Field, 0)
31+
}
32+
opts.Excluded = append(opts.Excluded, a.field)
33+
}
34+
1935
func WithFunc[TSource, TDest any](fn func(TSource, *TDest)) Option {
2036
return &withFuncOption[TSource, TDest]{fn: fn}
2137
}
38+
39+
func WithFieldSkip[TSource any](fn func(*TSource) any) Option {
40+
source := new(TSource)
41+
42+
storage, err := fmap.GetFrom(source)
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
fieldPtr := fn(source)
48+
49+
field, err := storage.GetFieldByPtr(source, fieldPtr)
50+
if err != nil {
51+
panic(err)
52+
}
53+
54+
return &withFieldSkip[TSource]{field: field}
55+
}

0 commit comments

Comments
 (0)