Skip to content
This repository was archived by the owner on Jun 19, 2019. It is now read-only.

Commit 7daacb2

Browse files
yonekawalunny
authored andcommitted
Fix potential data race in Column.fieldPath (#21)
1 parent 2fbe2c7 commit 7daacb2

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

column.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ type Column struct {
3232
IsDeleted bool
3333
IsCascade bool
3434
IsVersion bool
35-
fieldPath []string
3635
DefaultIsEmpty bool
3736
EnumOptions map[string]int
3837
SetOptions map[string]int
@@ -59,7 +58,6 @@ func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable
5958
IsDeleted: false,
6059
IsCascade: false,
6160
IsVersion: false,
62-
fieldPath: nil,
6361
DefaultIsEmpty: false,
6462
EnumOptions: make(map[string]int),
6563
}
@@ -121,32 +119,30 @@ func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
121119

122120
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
123121
var fieldValue reflect.Value
124-
if col.fieldPath == nil {
125-
col.fieldPath = strings.Split(col.FieldName, ".")
126-
}
122+
fieldPath := strings.Split(col.FieldName, ".")
127123

128124
if dataStruct.Type().Kind() == reflect.Map {
129-
keyValue := reflect.ValueOf(col.fieldPath[len(col.fieldPath)-1])
125+
keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
130126
fieldValue = dataStruct.MapIndex(keyValue)
131127
return &fieldValue, nil
132128
} else if dataStruct.Type().Kind() == reflect.Interface {
133129
structValue := reflect.ValueOf(dataStruct.Interface())
134130
dataStruct = &structValue
135131
}
136132

137-
level := len(col.fieldPath)
138-
fieldValue = dataStruct.FieldByName(col.fieldPath[0])
133+
level := len(fieldPath)
134+
fieldValue = dataStruct.FieldByName(fieldPath[0])
139135
for i := 0; i < level-1; i++ {
140136
if !fieldValue.IsValid() {
141137
break
142138
}
143139
if fieldValue.Kind() == reflect.Struct {
144-
fieldValue = fieldValue.FieldByName(col.fieldPath[i+1])
140+
fieldValue = fieldValue.FieldByName(fieldPath[i+1])
145141
} else if fieldValue.Kind() == reflect.Ptr {
146142
if fieldValue.IsNil() {
147143
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
148144
}
149-
fieldValue = fieldValue.Elem().FieldByName(col.fieldPath[i+1])
145+
fieldValue = fieldValue.Elem().FieldByName(fieldPath[i+1])
150146
} else {
151147
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
152148
}

0 commit comments

Comments
 (0)