Skip to content

Commit a24623b

Browse files
committed
#### Version 0.7.6
* Feature: add SetEnabledMapperTag to set enabled flag for 'Mapper' tag check * Feature: add SetEnabledJsonTag to set enabled flag for 'Json' tag check * Ops: add some test cases * Tips: Thanks to @aeramu for issue #12 * 2021-10-19 12:00 in ShangHai
1 parent 2ce1ff2 commit a24623b

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

mapper.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ var (
1616
enabledTypeChecking bool
1717
enabledMapperStructField bool
1818
enabledAutoTypeConvert bool
19+
enabledMapperTag bool
20+
enabledJsonTag bool
1921
timeType = reflect.TypeOf(time.Now())
2022
jsonTimeType = reflect.TypeOf(JSONTime(time.Now()))
2123
typeWrappers []TypeWrapper
2224
)
2325

2426
const (
25-
packageVersion = "0.7.5"
27+
packageVersion = "0.7.6"
2628
mapperTagKey = "mapper"
2729
jsonTagKey = "json"
2830
IgnoreTagValue = "-"
@@ -41,6 +43,8 @@ func init() {
4143
enabledTypeChecking = false
4244
enabledMapperStructField = true
4345
enabledAutoTypeConvert = true
46+
enabledMapperTag = true
47+
enabledJsonTag = true
4448
}
4549

4650
func PackageVersion() string {
@@ -72,6 +76,20 @@ func SetEnabledTypeChecking(isEnabled bool) {
7276
enabledTypeChecking = isEnabled
7377
}
7478

79+
// SetEnabledTypeChecking set enabled flag for 'Mapper' tag check
80+
// if set true, 'Mapper' tag will be check during mapping's GetFieldName
81+
// default is true
82+
func SetEnabledMapperTag(isEnabled bool) {
83+
enabledMapperTag = isEnabled
84+
}
85+
86+
// SetEnabledJsonTag set enabled flag for 'Json' tag check
87+
// if set true, 'Json' tag will be check during mapping's GetFieldName
88+
// default is true
89+
func SetEnabledJsonTag(isEnabled bool) {
90+
enabledJsonTag = isEnabled
91+
}
92+
7593
// SetEnabledAutoTypeConvert set enabled flag for auto type convert
7694
// if set true, field will auto convert in Time and Unix
7795
// default is true

mapper_internal.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,20 @@ func setFieldValue(fieldValue reflect.Value, fieldKind reflect.Kind, value inter
203203
func getStructTag(field reflect.StructField) string {
204204
tagValue := ""
205205
//1.check mapperTagKey
206-
tagValue = field.Tag.Get(mapperTagKey)
207-
if checkTagValidity(tagValue) {
208-
return tagValue
206+
if enabledMapperTag {
207+
tagValue = field.Tag.Get(mapperTagKey)
208+
if checkTagValidity(tagValue) {
209+
return tagValue
210+
}
209211
}
210212

211213
//2.check jsonTagKey
212-
tagValue = field.Tag.Get(jsonTagKey)
213-
if checkTagValidity(tagValue) {
214-
// support more tag property, as json tag omitempty 2018-07-13
215-
return strings.Split(tagValue, ",")[0]
214+
if enabledJsonTag {
215+
tagValue = field.Tag.Get(jsonTagKey)
216+
if checkTagValidity(tagValue) {
217+
// support more tag property, as json tag omitempty 2018-07-13
218+
return strings.Split(tagValue, ",")[0]
219+
}
216220
}
217221

218222
return ""

mapper_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ type (
2929
Sex bool
3030
BB string
3131
}
32+
33+
TagStruct struct {
34+
Name string `mapper:"UserName"`
35+
Sex bool `json:"UserSex"`
36+
}
3237
)
3338

3439
var testValue reflect.Value
@@ -61,6 +66,62 @@ func BenchmarkGetTypeName(b *testing.B) {
6166
}
6267
}
6368

69+
func Test_GetFieldNameFromMapperTag(t *testing.T) {
70+
v := TagStruct{}
71+
fieldName := GetFieldName(reflect.ValueOf(v), 0)
72+
if fieldName == "UserName" {
73+
t.Log("RunResult success:", fieldName)
74+
} else {
75+
t.Error("RunResult error: fieldName not match", fieldName)
76+
}
77+
}
78+
79+
func Test_GetFieldNameFromJsonTag(t *testing.T) {
80+
v := TagStruct{}
81+
fieldName := GetFieldName(reflect.ValueOf(v), 1)
82+
if fieldName == "UserSex" {
83+
t.Log("RunResult success:", fieldName)
84+
} else {
85+
t.Error("RunResult error: fieldName not match", fieldName)
86+
}
87+
}
88+
89+
func Test_SetEnableMapperTag(t *testing.T) {
90+
v := TagStruct{}
91+
SetEnabledMapperTag(false)
92+
fieldName := GetFieldName(reflect.ValueOf(v), 0)
93+
if fieldName == "Name" {
94+
t.Log("RunResult success:", fieldName)
95+
} else {
96+
t.Error("RunResult error: fieldName not match", fieldName)
97+
}
98+
SetEnabledMapperTag(true)
99+
fieldName = GetFieldName(reflect.ValueOf(v), 0)
100+
if fieldName == "UserName" {
101+
t.Log("RunResult success:", fieldName)
102+
} else {
103+
t.Error("RunResult error: fieldName not match", fieldName)
104+
}
105+
}
106+
107+
func Test_SetEnableJsonTag(t *testing.T) {
108+
v := TagStruct{}
109+
SetEnabledJsonTag(false)
110+
fieldName := GetFieldName(reflect.ValueOf(v), 1)
111+
if fieldName == "Sex" {
112+
t.Log("RunResult success:", fieldName)
113+
} else {
114+
t.Error("RunResult error: fieldName not match", fieldName)
115+
}
116+
SetEnabledJsonTag(true)
117+
fieldName = GetFieldName(reflect.ValueOf(v), 1)
118+
if fieldName == "UserSex" {
119+
t.Log("RunResult success:", fieldName)
120+
} else {
121+
t.Error("RunResult error: fieldName not match", fieldName)
122+
}
123+
}
124+
64125
func Test_GetFieldNameWithElem(t *testing.T) {
65126
fieldName := GetFieldName(testValue.Elem(), 0)
66127
if fieldName == "Name" {

version.md

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

3+
#### Version 0.7.6
4+
* Feature: add SetEnabledMapperTag to set enabled flag for 'Mapper' tag check
5+
* Feature: add SetEnabledJsonTag to set enabled flag for 'Json' tag check
6+
* Ops: add some test cases
7+
* Tips: Thanks to @aeramu for issue #12
8+
* 2021-10-19 12:00 in ShangHai
9+
310
#### Version 0.7.5
411
* Feature: Support for *[] to *[] with MapperSlice
512
* Ops: Definitive error messages

0 commit comments

Comments
 (0)