field.go
3.5 KB
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
package domain
import (
"fmt"
"github.com/linmadan/egglib-go/utils/xtime"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"strings"
)
// Field 字段
type Field struct {
// 字段Id
// FieldId int `json:"fieldId"`
// 索引序号
Index int `json:"index"`
// 名称
Name string `json:"name"`
// 对应数据库名称
SQLName string `json:"sqlName"`
// 对应数据库类型
SQLType string `json:"sqlType"`
// 描述
Description string `json:"description"`
// 标识 1.主键 2:主表字段 3:手动添加
Flag int `json:"flag"`
}
func (f *Field) Valid() error {
if _, ok := SQLTypeMap[strings.ToUpper(f.SQLType)]; !ok {
return fmt.Errorf("unknown sql type:%v", f.SQLType)
}
if f.Index == 0 && f.Flag == ManualField {
return fmt.Errorf("field:%v index is 0", f.Name)
}
return nil
}
type Fields []*Field
func (fields Fields) ToMap() map[string]*Field {
m := make(map[string]*Field)
for i := range fields {
m[fields[i].Name] = fields[i]
}
return m
}
func (fields Fields) NameArrayString() []string {
m := make([]string, 0)
for i := range fields {
m = append(m, fields[i].Name)
}
return m
}
func (fields Fields) Select(options map[string]interface{}) []*Field {
var result []*Field
for _, field := range fields {
if v, ok := options["flag"]; ok {
if v.(int) != field.Flag {
continue
}
}
result = append(result, field)
}
return result
}
func ValidFields(fields []*Field) error {
m := (Fields)(fields).ToMap()
if len(m) != len(fields) {
return fmt.Errorf("列名重复发")
}
for _, f := range fields {
if err := f.Valid(); err != nil {
return err
}
}
return nil
}
func FieldsChange(oldFields []*Field, newFields []*Field) (reserve []*Field, delete []*Field, add []*Field) {
var oldFieldsMap = (Fields)(oldFields).ToMap()
var newFieldsMap = (Fields)(newFields).ToMap()
for _, f := range newFields {
if _, ok := oldFieldsMap[f.Name]; ok {
reserve = append(reserve, f)
continue
} else {
add = append(add, f)
continue
}
}
for _, f := range oldFields {
if _, ok := newFieldsMap[f.Name]; !ok {
delete = append(delete, f)
continue
}
}
return
}
type FieldValues struct {
Number int `json:"number"`
FieldValues []*FieldValue `json:"fieldValues"`
}
func (f *FieldValues) Valid() error {
for _, item := range f.FieldValues {
if err := item.Valid(); err != nil {
return err
}
}
return nil
}
type FieldValue struct {
*Field
// 字段值(当前)
Value string `json:"value,omitempty"`
// 字段值(旧的,更新时有效)
OldValue string `json:"oldValue,omitempty"`
typeValue interface{}
}
func (f *FieldValue) CheckValue() error {
val, err := ValueToType(f.Value, f.SQLType)
if err != nil {
return err
}
f.typeValue = val
return nil
}
func (f *FieldValue) TypeValue() interface{} {
if f.typeValue == nil {
f.typeValue, _ = ValueToType(f.Value, f.SQLType)
}
return f.typeValue
}
func ValueToType(value string, sqlType string) (interface{}, error) {
var toTypeVal interface{}
var err error
numberString := utils.NewNumberString(value)
switch sqlType {
case String.ToString():
toTypeVal = value
case Int.ToString():
toTypeVal, err = numberString.Int()
case Float.ToString():
toTypeVal, err = numberString.Float64()
case Date.ToString():
toTypeVal, err = xtime.Parse(value)
return nil, nil
case Datetime.ToString():
toTypeVal, err = xtime.Parse(value)
return nil, nil
default:
return nil, fmt.Errorf("unknow sql type :%v", sqlType)
}
return toTypeVal, err
}