-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
185 lines (167 loc) · 3.52 KB
/
config.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
178
179
180
181
182
183
184
185
package CoreSQLConfig
import (
"database/sql/driver"
"encoding/json"
"errors"
CoreFilter "github.com/fotomxq/weeekj_core/v5/core/filter"
)
// FieldsConfigType 简化得扩展结构
type FieldsConfigType struct {
//标识码
Mark string `db:"mark" json:"mark" check:"mark" empty:"true"`
//值
Val string `db:"val" json:"val"`
}
// Value sql底层处理器
func (t FieldsConfigType) Value() (driver.Value, error) {
return json.Marshal(t)
}
func (t *FieldsConfigType) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, &t)
}
// FieldsConfigsType 简化得扩展结构
type FieldsConfigsType []FieldsConfigType
// Value sql底层处理器
func (t FieldsConfigsType) Value() (driver.Value, error) {
return json.Marshal(t)
}
func (t *FieldsConfigsType) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, &t)
}
// CheckVal 检查一组数据内的值是否匹配
func (t FieldsConfigsType) CheckVal(mark string, val string) (nowVal string, b bool) {
for _, v := range t {
if v.Mark == mark {
if v.Val == val {
return val, true
} else {
return val, false
}
}
}
return
}
// GetVal 抽取指定的值
func (t FieldsConfigsType) GetVal(mark string) (val string, b bool) {
for _, v := range t {
if v.Mark == mark {
val = v.Val
b = true
return
}
}
return
}
func (t FieldsConfigsType) GetValNoErr(mark string) (val string) {
for _, v := range t {
if v.Mark == mark {
val = v.Val
return
}
}
return
}
func (t FieldsConfigsType) GetValNoBool(mark string) (val string) {
for _, v := range t {
if v.Mark == mark {
val = v.Val
return
}
}
return
}
// GetValFloat64 抽取指定的值
func (t FieldsConfigsType) GetValFloat64(mark string) (val float64, b bool) {
var valStr string
valStr, b = t.GetVal(mark)
if !b {
return
}
var err error
val, err = CoreFilter.GetFloat64ByString(valStr)
if err != nil {
return
}
b = true
return
}
// GetValInt 抽取指定的值
func (t FieldsConfigsType) GetValInt(mark string) (val int, b bool) {
var valStr string
valStr, b = t.GetVal(mark)
if !b {
return
}
var err error
val, err = CoreFilter.GetIntByString(valStr)
if err != nil {
return
}
b = true
return
}
// GetValIntNoBool 抽取指定的值
func (t FieldsConfigsType) GetValIntNoBool(mark string) (val int) {
val, _ = t.GetValInt(mark)
return
}
// GetValInt64 抽取指定的值
func (t FieldsConfigsType) GetValInt64(mark string) (val int64, b bool) {
var valStr string
valStr, b = t.GetVal(mark)
if !b {
return
}
var err error
val, err = CoreFilter.GetInt64ByString(valStr)
if err != nil {
return
}
b = true
return
}
func (t FieldsConfigsType) GetValInt64NoBool(mark string) (val int64) {
val, _ = t.GetValInt64(mark)
return
}
// GetValBool 抽取指定的值
func (t FieldsConfigsType) GetValBool(mark string) (val bool, b bool) {
var valStr string
valStr, b = t.GetVal(mark)
if !b {
return
}
var err error
val, err = CoreFilter.GetBoolByInterface(valStr)
if err != nil {
return
}
b = true
return
}
// Set 写入数据
func Set(data FieldsConfigsType, mark string, val interface{}) FieldsConfigsType {
valStr, err := CoreFilter.GetStringByInterface(val)
if err != nil {
valStr = ""
}
for k, v := range data {
if v.Mark == mark {
data[k].Val = valStr
return data
}
}
data = append(data, FieldsConfigType{
Mark: mark,
Val: valStr,
})
return data
}