forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataheaderelem.go
147 lines (107 loc) · 3.6 KB
/
dataheaderelem.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
package v2
import (
"github.com/davyxu/tabtoy/v2/i18n"
"github.com/davyxu/tabtoy/v2/model"
"strings"
)
type DataHeaderElement struct {
FieldName string
FieldType string
FieldMeta string
Comment string
}
func checkElement(def *model.FieldDescriptor) int {
// 普通表头合法性检查
// 结构体单元格不能进行切分
//if def.Type == model.FieldType_Struct && def.ListSpliter() != "" {
//
// log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_StructCellCannotSplit),
// def.Name)
// return DataSheetHeader_FieldMeta
//}
return -1
}
func checkSameNameElement(exist, def *model.FieldDescriptor) int {
// 多个同名字段只允许repeated方式的字段
if !exist.IsRepeated {
log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_DuplicateFieldName), def.Name)
return DataSheetHeader_FieldName
}
// 多个repeated描述类型不一致
if exist.Type != def.Type {
log.Errorf("%s '%s' '%s' '%s'", i18n.String(i18n.DataHeader_RepeatedFieldTypeNotSameInMultiColumn),
def.Name,
model.FieldTypeToString(exist.Type),
model.FieldTypeToString(def.Type))
return DataSheetHeader_FieldType
}
// 多个repeated描述内建类型不一致
if exist.Complex != def.Complex {
log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_RepeatedFieldTypeNotSameInMultiColumn),
def.Name)
return DataSheetHeader_FieldType
}
// 多个repeated描述的meta不一致
if exist.Meta.String() != def.Meta.String() {
log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_RepeatedFieldMetaNotSameInMultiColumn),
def.Name)
return DataSheetHeader_FieldMeta
}
return -1
}
func (self *DataHeaderElement) Parse(def *model.FieldDescriptor, localFD *model.FileDescriptor, globalFD *model.FileDescriptor, headerByName map[string]*model.FieldDescriptor) int {
// ====================解析类型====================
testFileD := localFD
for {
if def.ParseType(testFileD, self.FieldType) {
break
}
if testFileD == localFD {
testFileD = globalFD
continue
}
break
}
// 依然找不到, 报错
if def.Type == model.FieldType_None {
log.Errorf("%s, '%s' (%s) raw: %s", i18n.String(i18n.DataHeader_TypeNotFound), def.Name, model.FieldTypeToString(def.Type), self.FieldType)
return DataSheetHeader_FieldType
}
// ====================解析特性====================
if err := def.Meta.Parse(self.FieldMeta); err != nil {
log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_MetaParseFailed), err)
return DataSheetHeader_FieldMeta
}
def.Comment = strings.Replace(self.Comment, "\n", " ", -1)
// 根据字段名查找, 处理repeated字段case
exist, ok := headerByName[def.Name]
if ok {
// 多个同名字段只允许repeated方式的字段
if !exist.IsRepeated {
log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_DuplicateFieldName), def.Name)
return DataSheetHeader_FieldName
}
// 多个repeated描述类型不一致
if exist.Type != def.Type {
log.Errorf("%s '%s' '%s' '%s'", i18n.String(i18n.DataHeader_RepeatedFieldTypeNotSameInMultiColumn),
def.Name,
model.FieldTypeToString(exist.Type),
model.FieldTypeToString(def.Type))
return DataSheetHeader_FieldType
}
// 多个repeated描述内建类型不一致
if exist.Complex != def.Complex {
log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_RepeatedFieldTypeNotSameInMultiColumn),
def.Name)
return DataSheetHeader_FieldType
}
// 多个repeated描述的meta不一致
if exist.Meta.String() != def.Meta.String() {
log.Errorf("%s '%s'", i18n.String(i18n.DataHeader_RepeatedFieldMetaNotSameInMultiColumn),
def.Name)
return DataSheetHeader_FieldMeta
}
def = exist
}
return -1
}