/
callback.go
177 lines (158 loc) · 4.12 KB
/
callback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package callback
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
type (
RuleProvider interface {
Rule(ctx context.Context, dbname, table string) (*ChaosRule, error)
}
ChaosRule struct {
DML string `json:"dml"`
When string `json:"when"`
Then string `json:"then"`
}
Callback struct {
DBName string
RuleProvider RuleProvider
}
)
var (
DEBUG = os.Getenv("SQLCHAOS_DEBUG") != ""
SQLCHAOS_DEBUG = func(format string, args ...interface{}) {
if DEBUG {
fmt.Fprintf(os.Stdout, "SQLCHAOS:"+format+"\n", args...)
}
}
SQLCHAOS_ERROR = func(format string, args ...interface{}) { fmt.Fprintf(os.Stderr, "SQLCHAOS:"+format+"\n", args...) }
)
func (c *Callback) BeforeCreate() func(*gorm.DB) {
return func(db *gorm.DB) {
stmt := db.Statement
if stmt.Schema == nil {
fmt.Fprintln(os.Stderr, "statement scheme not provided")
return
}
table := db.Statement.Table
rule, err := c.GetTableRule(context.Background(), table, "CREATE")
if err != nil {
SQLCHAOS_ERROR("read rule failed:%+v", err)
return
}
if rule == nil {
return
}
SQLCHAOS_DEBUG("get chaos rule:%#v", rule)
if ApplyRule(rule, stmt) {
SQLCHAOS_ERROR("records have been modified")
}
}
}
func (c *Callback) BeforeUpdate() func(*gorm.DB) {
return func(db *gorm.DB) {
stmt := db.Statement
if stmt.Schema == nil {
fmt.Fprintln(os.Stderr, "statement scheme not provided")
return
}
table := db.Statement.Table
rule, err := c.GetTableRule(context.Background(), table, "UPDATE")
if err != nil {
SQLCHAOS_ERROR("read rule failed:%+v", err)
return
}
if rule == nil {
return
}
SQLCHAOS_DEBUG("get chaos rule:%#v", rule)
if ApplyRule(rule, stmt) {
SQLCHAOS_ERROR("records have been modified")
}
}
}
func ApplyRule(rule *ChaosRule, stmt *gorm.Statement) (applied bool) {
matcher, err := ParseWhenStatement(rule.When)
if err != nil {
SQLCHAOS_ERROR("when statement invalid:%+v", err)
return
}
applier, err := ParseThenStatement(rule.Then)
if err != nil {
SQLCHAOS_ERROR("then statement invalid:%+v", err)
return
}
return ApplyValuesIfMatch(stmt, matcher, applier)
}
func (c *Callback) GetTableRule(ctx context.Context, table, dml string) (rule *ChaosRule, err error) {
rule, err = c.RuleProvider.Rule(context.TODO(), c.DBName, table)
if err != nil || rule == nil {
return
}
if !strings.EqualFold(rule.DML, dml) {
return
}
return
}
// Canonical converts struct field name to database column name
func Canonical(schema *schema.Schema, v map[string]interface{}) {
for name, value := range v {
if dbname, ok := schema.FieldsByName[name]; ok {
v[dbname.DBName] = value
delete(v, name)
continue
}
v[name] = value
}
}
func ApplyValuesIfMatch(stmt *gorm.Statement, matcher Matcher, applier Applier) (applied bool) {
if v, ok := stmt.Dest.(map[string]interface{}); ok {
Canonical(stmt.Schema, v)
if matcher.Match(MatchByInterface(v)) {
applied = applier.Apply(ApplyByInterface(v))
}
} else if vs, ok := stmt.Dest.([]map[string]interface{}); ok {
for _, v := range vs {
Canonical(stmt.Schema, v)
if matcher.Match(MatchByInterface(v)) {
applied = applier.Apply(ApplyByInterface(v)) || applied
}
}
} else {
dest := reflect.Indirect(reflect.ValueOf(stmt.Dest))
switch dest.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < dest.Len(); i++ {
one := dest.Index(i)
vs := make(map[string]reflect.Value)
for name, field := range stmt.Schema.FieldsByName {
v := one.FieldByName(name)
if !v.IsValid() || (v.Kind() == reflect.Ptr && v.IsNil()) {
continue
}
vs[field.DBName] = v
}
if matcher.Match(MatchByReflectValue(vs)) {
applied = applier.Apply(ApplyByReflectValue(vs)) || applied
}
}
case reflect.Struct:
vs := make(map[string]reflect.Value)
for name, field := range stmt.Schema.FieldsByName {
v := dest.FieldByName(name)
if !v.IsValid() || (v.Kind() == reflect.Ptr && v.IsNil()) {
continue
}
vs[field.DBName] = v
}
if matcher.Match(MatchByReflectValue(vs)) {
applied = applier.Apply(ApplyByReflectValue(vs))
}
}
}
return
}