Skip to content

Commit dc8801c

Browse files
committed
优化代码
1 parent 23d7c7e commit dc8801c

File tree

11 files changed

+35
-94
lines changed

11 files changed

+35
-94
lines changed

v3/example/protobuf/Make.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# protoc 下载
2-
# wget https://github.com/protocolbuffers/protobuf/releases/download/v3.13.0/protoc-3.13.0-win64.zip
1+
# protoc 下载 https://github.com/protocolbuffers/protobuf/releases
32
go install google.golang.org/protobuf/cmd/protoc-gen-go
43
./protoc --go_out=. ./table.proto

v3/example/protobuf/pb.bin

-184 Bytes
Binary file not shown.

v3/example/protobuf/protoc

-3.56 MB
Binary file not shown.
File renamed without changes.

v3/example/xlsx/Run.bat

-1
This file was deleted.

v3/gen/bindata/value.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ func writeValue(globals *model.Globals, structWriter *BinaryWriter, fieldType *m
8383
if value == "" {
8484
return structWriter.WriteBool(false)
8585
} else {
86-
var v bool
87-
switch value {
88-
case "是", "yes", "YES", "1", "true", "TRUE", "True":
89-
v = true
90-
default:
86+
v, err := model.ParseBool(value)
87+
if err != nil {
88+
return err
9189
}
9290

9391
return structWriter.WriteBool(v)

v3/gen/cssrc/func.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ func wrapSingleValue(globals *model.Globals, valueType *model.TypeDefine, value
3131
return ""
3232
case valueType.FieldType == "bool":
3333

34-
switch value {
35-
case "是", "yes", "YES", "1", "true", "TRUE", "True":
34+
v, _ := model.ParseBool(value)
35+
if v {
3636
return "true"
37-
case "否", "no", "NO", "0", "false", "FALSE", "False":
38-
return "false"
3937
}
4038

4139
return "false"

v3/gen/javasrc/func.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ func wrapSingleValue(globals *model.Globals, valueType *model.TypeDefine, value
2626
return ""
2727
case valueType.FieldType == "bool":
2828

29-
switch value {
30-
case "是", "yes", "YES", "1", "true", "TRUE", "True":
29+
v, _ := model.ParseBool(value)
30+
if v {
3131
return "true"
32-
case "否", "no", "NO", "0", "false", "FALSE", "False":
33-
return "false"
3432
}
3533

3634
return "false"

v3/gen/pbdata/gen.go

+8-51
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,7 @@ func Generate(globals *model.Globals) (data []byte, err error) {
131131

132132
func tableValue2PbValueList(globals *model.Globals, cell *model.Cell, valueType *model.TypeDefine, list protoreflect.List) {
133133

134-
pbType := model.LanguagePrimitive(valueType.FieldType, "go")
135-
136-
// go类型转pb
137-
switch pbType {
138-
case "int16", "int8":
139-
pbType = "int32"
140-
case "uint16", "uint8":
141-
pbType = "uint32"
142-
case "float32":
143-
pbType = "float"
144-
case "float64":
145-
pbType = "double"
146-
}
134+
pbType := model.LanguagePrimitive(valueType.FieldType, "pb")
147135

148136
switch pbType {
149137
case "int32":
@@ -172,12 +160,8 @@ func tableValue2PbValueList(globals *model.Globals, cell *model.Cell, valueType
172160

173161
for _, str := range cell.ValueList {
174162

175-
switch str {
176-
case "是", "yes", "YES", "1", "true", "TRUE", "True":
177-
list.Append(protoreflect.ValueOfBool(true))
178-
default:
179-
list.Append(protoreflect.ValueOfBool(false))
180-
}
163+
v, _ := model.ParseBool(str)
164+
list.Append(protoreflect.ValueOfBool(v))
181165
}
182166

183167
case "string":
@@ -204,19 +188,7 @@ func tableValue2PbValueList(globals *model.Globals, cell *model.Cell, valueType
204188

205189
func tableValue2PbValue(globals *model.Globals, cell *model.Cell, valueType *model.TypeDefine) protoreflect.Value {
206190

207-
pbType := model.LanguagePrimitive(valueType.FieldType, "go")
208-
209-
// go类型转pb
210-
switch pbType {
211-
case "int16", "int8":
212-
pbType = "int32"
213-
case "uint16", "uint8":
214-
pbType = "uint32"
215-
case "float32":
216-
pbType = "float"
217-
case "float64":
218-
pbType = "double"
219-
}
191+
pbType := model.LanguagePrimitive(valueType.FieldType, "pb")
220192

221193
switch pbType {
222194
case "int32":
@@ -259,12 +231,9 @@ func tableValue2PbValue(globals *model.Globals, cell *model.Cell, valueType *mod
259231
if cell.Value == "" {
260232
return protoreflect.ValueOfBool(false)
261233
}
262-
switch cell.Value {
263-
case "是", "yes", "YES", "1", "true", "TRUE", "True":
264-
return protoreflect.ValueOfBool(true)
265-
default:
266-
return protoreflect.ValueOfBool(false)
267-
}
234+
235+
v, _ := model.ParseBool(cell.Value)
236+
return protoreflect.ValueOfBool(v)
268237
case "string":
269238
return protoreflect.ValueOfString(cell.Value)
270239
default:
@@ -286,19 +255,7 @@ func tableValue2PbValue(globals *model.Globals, cell *model.Cell, valueType *mod
286255
}
287256

288257
func tableType2PbType(globals *model.Globals, def *model.TypeDefine, pbDesc *descriptorpb.FieldDescriptorProto) {
289-
pbType := model.LanguagePrimitive(def.FieldType, "go")
290-
291-
// go类型转pb
292-
switch pbType {
293-
case "int16", "int8":
294-
pbType = "int32"
295-
case "uint16", "uint8":
296-
pbType = "uint32"
297-
case "float32":
298-
pbType = "float"
299-
case "float64":
300-
pbType = "double"
301-
}
258+
pbType := model.LanguagePrimitive(def.FieldType, "pb")
302259

303260
switch pbType {
304261
case "int32":

v3/gen/pbsrc/func.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@ var UsefulFunc = template.FuncMap{}
1414
func init() {
1515
UsefulFunc["PbType"] = func(tf *model.TypeDefine) string {
1616

17-
pbType := model.LanguagePrimitive(tf.FieldType, "go")
18-
19-
switch pbType {
20-
case "int16", "int8":
21-
pbType = "int32"
22-
case "uint16", "uint8":
23-
pbType = "uint32"
24-
case "float32":
25-
pbType = "float"
26-
case "float64":
27-
pbType = "double"
28-
}
17+
pbType := model.LanguagePrimitive(tf.FieldType, "pb")
2918

3019
if tf.IsArray() {
3120
return "repeated " + pbType

v3/model/fieldtype.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,31 @@ import (
55
)
66

77
type FieldType struct {
8-
InputFieldName string `tb_name:"输入字段"`
9-
GoFieldName string `tb_name:"Go字段"`
8+
InputFieldName string `tb_name:"输入字段"` // 表中输入的类型
9+
GoFieldName string `tb_name:"Go字段"` // 转换为go的类型
1010
CSFieldName string `tb_name:"C#字段"`
1111
JavaFieldName string `tb_name:"Java字段"`
12+
PBFieldName string `tb_name:"Protobuf字段"`
1213
DefaultValue string `tb_name:"默认值"`
1314
}
1415

1516
// 将表中输入的字段类型转换为各种语言类型
1617

1718
var (
1819
FieldTypes = []*FieldType{
19-
{"int16", "int16", "Int16", "int", "0"},
20-
{"int32", "int32", "Int32", "int", "0"},
21-
{"int64", "int64", "Int64", "long", "0"},
22-
{"int", "int32", "Int32", "int", "0"},
23-
{"uint16", "uint16", "UInt16", "int", "0"},
24-
{"uint32", "uint32", "UInt32", "int", "0"},
25-
{"uint64", "uint64", "UInt64", "long", "0"},
26-
{"float", "float32", "float", "float", "0"},
27-
{"double", "float64", "double", "double", "0"},
28-
{"float32", "float32", "float", "float", "0"},
29-
{"float64", "float64", "double", "double", "0"},
30-
{"bool", "bool", "bool", "boolean", "FALSE"},
31-
{"string", "string", "string", "String", ""},
20+
{"int16", "int16", "Int16", "int", "int32", "0"},
21+
{"int32", "int32", "Int32", "int", "int32", "0"},
22+
{"int64", "int64", "Int64", "long", "int64", "0"},
23+
{"int", "int32", "Int32", "int", "int32", "0"},
24+
{"uint16", "uint16", "UInt16", "int", "uint32", "0"},
25+
{"uint32", "uint32", "UInt32", "int", "uint32", "0"},
26+
{"uint64", "uint64", "UInt64", "long", "uint64", "0"},
27+
{"float", "float32", "float", "float", "float", "0"},
28+
{"double", "float64", "double", "double", "double", "0"},
29+
{"float32", "float32", "float", "float", "float", "0"},
30+
{"float64", "float64", "double", "double", "double", "0"},
31+
{"bool", "bool", "bool", "boolean", "bool", "FALSE"},
32+
{"string", "string", "string", "String", "string", ""},
3233
}
3334

3435
FieldTypeByType = map[string]*FieldType{}
@@ -62,6 +63,8 @@ func LanguagePrimitive(fieldType string, lanType string) string {
6263
return ft.GoFieldName
6364
case "java":
6465
return ft.JavaFieldName
66+
case "pb":
67+
return ft.PBFieldName
6568
default:
6669
panic("unknown lan type: " + lanType)
6770
}

0 commit comments

Comments
 (0)