Skip to content

Commit e0c28cc

Browse files
authored
Merge pull request #24 from devfeel/develop
feature: mapper ignore tag processor
2 parents 4a27752 + f159fa2 commit e0c28cc

9 files changed

Lines changed: 135 additions & 31 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ go get -u github.com/devfeel/mapper
1111
## 2. Getting Started
1212

1313
Traditional Usage
14-
```go
14+
``` go
1515
package main
1616

1717
import (
@@ -81,7 +81,7 @@ userMap: &{map 10 x1asd 100 2017-11-20 13:45:56.3972504 +0800 CST m=+0.006004001
8181

8282
Object Usage
8383

84-
```go
84+
``` go
8585
package main
8686

8787
import (
@@ -91,41 +91,50 @@ import (
9191

9292
type (
9393
User struct {
94-
Name string `json:"name" mapper:"name"`
95-
Age int `json:"age" mapper:"age"`
94+
Name string `json:"name" mapper:"name"`
95+
Class int `mapper:"class"`
96+
Age int `json:"age" mapper:"-"`
9697
}
9798

9899
Student struct {
99100
Name string `json:"name" mapper:"name"`
100-
Age int `json:"age" mapper:"-"`
101+
Class int `mapper:"class"`
102+
Age []int `json:"age" mapper:"-"`
101103
}
102104
)
103105

104106
func main() {
105-
user := &User{Name: "test", Age: 10}
107+
user := &User{Name: "shyandsy", Class: 1, Age: 10}
106108
student := &Student{}
107109

108110
// create mapper object
109111
m := mapper.NewMapper()
110112

111-
// enable the type checking
112-
m.SetEnabledTypeChecking(true)
113+
// in the version < v0.7.8, we will use field name as key when mapping structs
114+
// we keep it as default behavior in this version
115+
m.SetEnableIgnoreFieldTag(true)
113116

114-
student.Age = 1
117+
student.Age = []int{1}
115118

116119
// disable the json tag
117120
m.SetEnabledJsonTag(false)
118121

119122
// student::age should be 1
120123
m.Mapper(user, student)
121124

125+
fmt.Println("user:")
126+
fmt.Println(user)
127+
fmt.Println("student:")
122128
fmt.Println(student)
123129
}
124130
```
125131

126132
执行main,输出:
127133
```
128-
&{test 1}
134+
user:
135+
&{shyandsy 1 10}
136+
student:
137+
&{shyandsy 1 [1]}
129138
```
130139

131140

constant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package mapper
22

33
const (
4-
packageVersion = "0.7.8"
4+
packageVersion = "0.7.9"
55
mapperTagKey = "mapper"
66
jsonTagKey = "json"
77
IgnoreTagValue = "-"

example/ignoretag/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/devfeel/mapper"
6+
)
7+
8+
type (
9+
User struct {
10+
Name string `json:"name" mapper:"name"`
11+
Class int `mapper:"class"`
12+
Age int `json:"age" mapper:"-"`
13+
}
14+
15+
Student struct {
16+
Name string `json:"name" mapper:"name"`
17+
Class int `mapper:"class"`
18+
Age int `json:"age" mapper:"-"`
19+
}
20+
)
21+
22+
func main() {
23+
user := &User{Name: "shyandsy", Class: 1, Age: 10}
24+
student := &Student{}
25+
26+
// create mapper object
27+
m := mapper.NewMapper()
28+
29+
// in the version < v0.7.8, we will use field name as key when mapping structs
30+
// we keep it as default behavior in this version
31+
m.SetEnableFieldIgnoreTag(true)
32+
33+
// disable the json tag
34+
m.SetEnabledJsonTag(false)
35+
36+
// student::age should be 1
37+
m.Mapper(user, student)
38+
39+
fmt.Println("user:")
40+
fmt.Println(user)
41+
fmt.Println("student:")
42+
fmt.Println(student)
43+
}

example/object/main.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,39 @@ import (
77

88
type (
99
User struct {
10-
Name string `json:"name" mapper:"name"`
11-
Age int `json:"age" mapper:"age"`
10+
Name string `json:"name" mapper:"name"`
11+
Class int `mapper:"class"`
12+
Age int `json:"age" mapper:"-"`
1213
}
1314

1415
Student struct {
1516
Name string `json:"name" mapper:"name"`
16-
Age int `json:"age" mapper:"-"`
17+
Class int `mapper:"class"`
18+
Age []int `json:"age" mapper:"-"`
1719
}
1820
)
1921

2022
func main() {
21-
user := &User{Name: "test", Age: 10}
23+
user := &User{Name: "shyandsy", Class: 1, Age: 10}
2224
student := &Student{}
2325

2426
// create mapper object
2527
m := mapper.NewMapper()
2628

27-
// enable the type checking
28-
m.SetEnabledTypeChecking(true)
29+
// in the version < v0.7.8, we will use field name as key when mapping structs
30+
// we keep it as default behavior in this version
31+
m.SetEnableFieldIgnoreTag(true)
2932

30-
student.Age = 1
33+
student.Age = []int{1}
3134

3235
// disable the json tag
3336
m.SetEnabledJsonTag(false)
3437

3538
// student::age should be 1
3639
m.Mapper(user, student)
3740

41+
fmt.Println("user:")
42+
fmt.Println(user)
43+
fmt.Println("student:")
3844
fmt.Println(student)
3945
}

mapper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type IMapper interface {
4242

4343
SetEnabledMapperStructField(isEnabled bool)
4444
IsEnabledMapperStructField() bool
45+
46+
SetEnableFieldIgnoreTag(isEnabled bool)
47+
IsEnableFieldIgnoreTag() bool
4548
}
4649

4750
func init() {
@@ -100,6 +103,14 @@ func SetEnabledMapperStructField(isEnabled bool) {
100103
standardMapper.SetEnabledMapperStructField(isEnabled)
101104
}
102105

106+
// SetEnableFieldIgnoreTag set the enabled flag for the ignored tag
107+
// in the version < 0.7.8, we use field name as the key when mapping structs if field tag is "-"
108+
// from 0.7.8, we add switch enableFieldIgnoreTag which is false in default
109+
// if caller enable this flag, the field will be ignored in the mapping process
110+
func SetEnableFieldIgnoreTag(isEnabled bool) {
111+
standardMapper.SetEnableFieldIgnoreTag(isEnabled)
112+
}
113+
103114
// Register register struct to init Map
104115
func Register(obj interface{}) error {
105116
return standardMapper.Register(obj)

mapper_object.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type mapperObject struct {
2121
enabledAutoTypeConvert bool
2222
enabledMapperTag bool
2323
enabledJsonTag bool
24+
25+
// in the version < 0.7.8, we use field name as the key when mapping structs if field tag is "-"
26+
// from 0.7.8, we add switch enableIgnoreFieldTag which is false in default
27+
// if caller enable this flag, the field will be ignored in the mapping process
28+
enableFieldIgnoreTag bool
2429
}
2530

2631
func NewMapper() IMapper {
@@ -35,6 +40,7 @@ func NewMapper() IMapper {
3540
enabledAutoTypeConvert: true,
3641
enabledMapperTag: true,
3742
enabledJsonTag: true,
43+
enableFieldIgnoreTag: false, // 保留老版本默认行为:对于tag = “-”的字段使用FieldName
3844
}
3945
dm.useWrapper(dm.DefaultTimeWrapper)
4046
return &dm
@@ -166,6 +172,18 @@ func (dm *mapperObject) IsEnabledMapperStructField() bool {
166172
return dm.enabledMapperStructField
167173
}
168174

175+
// SetEnableFieldIgnoreTag set the enabled flag for the ignored tag
176+
// in the version < 0.7.8, we use field name as the key when mapping structs if field tag is "-"
177+
// from 0.7.8, we add switch enableFieldIgnoreTag which is false in default
178+
// if caller enable this flag, the field will be ignored in the mapping process
179+
func (dm *mapperObject) SetEnableFieldIgnoreTag(isEnabled bool) {
180+
dm.enableFieldIgnoreTag = isEnabled
181+
}
182+
183+
func (dm *mapperObject) IsEnableFieldIgnoreTag() bool {
184+
return dm.enableFieldIgnoreTag
185+
}
186+
169187
// GetTypeName get type name
170188
func (dm *mapperObject) GetTypeName(obj interface{}) string {
171189
object := reflect.ValueOf(obj)

mapper_object_internal.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ func (dm *mapperObject) registerValue(objValue reflect.Value) error {
2222
typeName := regValue.Type().String()
2323
if regValue.Type().Kind() == reflect.Struct {
2424
for i := 0; i < regValue.NumField(); i++ {
25-
mapFieldName := typeName + nameConnector + dm.getFieldName(regValue, i)
25+
fieldName := dm.getFieldName(regValue, i)
26+
if fieldName == IgnoreTagValue {
27+
continue
28+
}
29+
mapFieldName := typeName + nameConnector + fieldName
2630
realFieldName := regValue.Type().Field(i).Name
2731
dm.fieldNameMap.Store(mapFieldName, realFieldName)
2832
}
@@ -39,6 +43,12 @@ func (dm *mapperObject) getFieldName(objElem reflect.Value, index int) string {
3943
fieldName := ""
4044
field := objElem.Type().Field(index)
4145
tag := dm.getStructTag(field)
46+
47+
// keeps the behavior in old version
48+
if tag == IgnoreTagValue && !dm.IsEnableFieldIgnoreTag() {
49+
tag = ""
50+
}
51+
4252
if tag != "" {
4353
fieldName = tag
4454
} else {
@@ -81,6 +91,10 @@ func (dm *mapperObject) elemToStruct(fromElem, toElem reflect.Value) {
8191
for i := 0; i < fromElem.NumField(); i++ {
8292
fromFieldInfo := fromElem.Field(i)
8393
fieldName := dm.getFieldName(fromElem, i)
94+
if fieldName == IgnoreTagValue {
95+
continue
96+
}
97+
8498
// check field is exists
8599
realFieldName, exists := dm.CheckExistsField(toElem, fieldName)
86100
if !exists {
@@ -131,6 +145,9 @@ func (dm *mapperObject) elemToMap(fromElem, toElem reflect.Value) {
131145
for i := 0; i < fromElem.NumField(); i++ {
132146
fromFieldInfo := fromElem.Field(i)
133147
fieldName := dm.getFieldName(fromElem, i)
148+
if fieldName == IgnoreTagValue {
149+
continue
150+
}
134151
toElem.SetMapIndex(reflect.ValueOf(fieldName), fromFieldInfo)
135152
}
136153
}
@@ -257,28 +274,21 @@ func (dm *mapperObject) getStructTag(field reflect.StructField) string {
257274
// 1.check mapperTagKey
258275
if dm.enabledMapperTag {
259276
tagValue = field.Tag.Get(mapperTagKey)
260-
if dm.checkTagValidity(tagValue) {
277+
if tagValue != "" {
261278
return tagValue
262279
}
263280
}
264281

265282
// 2.check jsonTagKey
266283
if dm.enabledJsonTag {
267284
tagValue = field.Tag.Get(jsonTagKey)
268-
if dm.checkTagValidity(tagValue) {
285+
if tagValue != "" {
269286
// support more tag property, as json tag omitempty 2018-07-13
270287
return strings.Split(tagValue, ",")[0]
271288
}
272289
}
273290

274-
return ""
275-
}
276-
277-
func (dm *mapperObject) checkTagValidity(tagValue string) bool {
278-
if tagValue != "" && tagValue != IgnoreTagValue {
279-
return true
280-
}
281-
return false
291+
return tagValue
282292
}
283293

284294
func (dm *mapperObject) checkIsRegister(objElem reflect.Value) bool {
@@ -287,7 +297,7 @@ func (dm *mapperObject) checkIsRegister(objElem reflect.Value) bool {
287297
return isOk
288298
}
289299

290-
// convert slice interface{} to []interface{}
300+
// convertToSlice convert slice interface{} to []interface{}
291301
func (dm *mapperObject) convertToSlice(arr interface{}) []interface{} {
292302
v := reflect.ValueOf(arr)
293303
if v.Kind() == reflect.Ptr {
@@ -308,7 +318,7 @@ func (dm *mapperObject) convertToSlice(arr interface{}) []interface{} {
308318
return ret
309319
}
310320

311-
// CheckIsTypeWrapper check value is in type wrappers
321+
// checkIsTypeWrapper check value is in type wrappers
312322
func (dm *mapperObject) checkIsTypeWrapper(value reflect.Value) bool {
313323
for _, w := range dm.typeWrappers {
314324
if w.IsType(value) {

version.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## devfeel/mapper
22

3+
#### Version 0.7.9
4+
* Feature: add feature flag for ignore tag.
5+
* Tips: about "-" we keep default behavior as previous version by default, which is use field name as key when mapping structure.
6+
* Tips: now you can use SetEnableFieldIgnoreTag function to enable this flag right now
7+
* 2022-04-17 21:00 in ShangHai
8+
39
#### Version 0.7.8
410
* Refactor: use mapperObject refactored the static version implementation.
511
* 2022-04-16 10:00 in ShangHai

0 commit comments

Comments
 (0)