Skip to content

Commit 402c4af

Browse files
Fix data race in dbo plugin
1 parent cdf54c3 commit 402c4af

1 file changed

Lines changed: 2 additions & 90 deletions

File tree

dbo/plugin.go

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dbo
22

33
import (
4-
"bytes"
54
"fmt"
65
"reflect"
76
"strings"
@@ -13,9 +12,7 @@ import (
1312

1413
type xPluginCallback func(db *gorm.DB)
1514

16-
type xPlugin struct {
17-
tags map[string]*xPluginTag
18-
}
15+
type xPlugin struct{}
1916

2017
func (x *xPlugin) Name() string {
2118
return "gokit-dbo"
@@ -41,22 +38,6 @@ func (x *xPlugin) onBeforeQuery() xPluginCallback {
4138
if method.IsValid() && !method.IsZero() {
4239
db = db.Preload(field.Name, method.Interface())
4340
}
44-
45-
tag := x.getFieldTag(field)
46-
47-
if tag != nil {
48-
if tag.Preload {
49-
args := make([]any, 0)
50-
51-
method := model.MethodByName(field.Name + "Preloader")
52-
53-
if method.IsValid() && !method.IsZero() {
54-
args = append(args, method.Interface())
55-
}
56-
57-
db = db.Preload(field.Name, args...)
58-
}
59-
}
6041
}
6142

6243
// Process Scopes
@@ -82,81 +63,12 @@ func (x *xPlugin) onAfterQuery() xPluginCallback {
8263
}
8364
}
8465

85-
// ==================================
86-
8766
func (x *xPlugin) key(field *schema.Field) (value string) {
8867
return fmt.Sprintf("%v.%v", field.Schema.Name, field.Name)
8968
}
9069

91-
func (x *xPlugin) getFieldTag(field *schema.Field) (tag *xPluginTag) {
92-
key := x.key(field)
93-
94-
if val, ok := x.tags[key]; ok {
95-
tag = val
96-
return
97-
}
98-
99-
tagString := field.Tag.Get("gokit-dbo")
100-
101-
if tagString == "" {
102-
return
103-
}
104-
105-
tag = &xPluginTag{}
106-
107-
var isValue bool
108-
var kbuff, vbuff bytes.Buffer
109-
110-
lastindex := len(tagString) - 1
111-
112-
for i, char := range tagString {
113-
switch char {
114-
case ':':
115-
isValue = true
116-
117-
case ';':
118-
x.updateTagValue(tag, &kbuff, &vbuff)
119-
isValue = false
120-
121-
default:
122-
if isValue {
123-
vbuff.WriteRune(char)
124-
} else {
125-
kbuff.WriteRune(char)
126-
}
127-
}
128-
129-
if i == lastindex {
130-
x.updateTagValue(tag, &kbuff, &vbuff)
131-
isValue = false
132-
continue
133-
}
134-
}
135-
136-
x.tags[key] = tag
137-
138-
return
139-
}
140-
141-
func (x *xPlugin) updateTagValue(tag *xPluginTag, kbuff, vbuff *bytes.Buffer) {
142-
defer kbuff.Reset()
143-
defer vbuff.Reset()
144-
145-
key := kbuff.String()
146-
147-
switch key {
148-
case "preload":
149-
tag.Preload = true
150-
151-
case "hidden":
152-
tag.Hidden = true
153-
}
154-
}
155-
15670
// ==============================================
15771

15872
func NewPlugin() (v *xPlugin) {
159-
return &xPlugin{
160-
tags: make(map[string]*xPluginTag),
161-
}
73+
return &xPlugin{}
16274
}

0 commit comments

Comments
 (0)