Skip to content

Commit

Permalink
修复数组列导出问题 #58
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Jul 31, 2020
1 parent 455ace3 commit 91f9ace
Show file tree
Hide file tree
Showing 35 changed files with 449 additions and 199 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var log = golog.New("main")

const (
Version = "3.0.0"
Version = "3.0.1"
)

var enableProfile = false
Expand Down
29 changes: 23 additions & 6 deletions v3/checker/checker_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/davyxu/tabtoy/v3/model"
"github.com/davyxu/tabtoy/v3/report"
"strconv"
"strings"
)

// 检查数据与定义类型是否匹配
Expand Down Expand Up @@ -35,12 +34,9 @@ func checkDataType(globals *model.Globals) {
crrCell = inputCell
currHeader = header

if inputCell.Value == "" {
continue
}

if header.TypeInfo.IsArray() {
for _, value := range strings.Split(inputCell.Value, header.TypeInfo.ArraySplitter) {
for _, value := range inputCell.ValueList {

err := checkSingleValue(header, value)
if err != nil {
report.ReportError("DataMissMatchTypeDefine", currHeader.TypeInfo.FieldType, crrCell.String())
Expand All @@ -61,31 +57,49 @@ func checkDataType(globals *model.Globals) {
func checkSingleValue(header *model.HeaderField, value string) error {
switch model.LanguagePrimitive(header.TypeInfo.FieldType, "go") {
case "int16":
if value == "" {
return nil
}
_, err := strconv.ParseInt(value, 10, 16)
if err != nil {
return err
}
case "int32":
if value == "" {
return nil
}
_, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return err
}
case "int64":
if value == "" {
return nil
}
_, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
case "uint16":
if value == "" {
return nil
}
_, err := strconv.ParseUint(value, 10, 16)
if err != nil {
return err
}
case "uint32":
if value == "" {
return nil
}
_, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return err
}
case "uint64":
if value == "" {
return nil
}
_, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
Expand All @@ -96,6 +110,9 @@ func checkSingleValue(header *model.HeaderField, value string) error {
return err
}
case "float64":
if value == "" {
return nil
}
_, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
Expand Down
21 changes: 11 additions & 10 deletions v3/checker/checker_enumvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package checker
import (
"github.com/davyxu/tabtoy/v3/model"
"github.com/davyxu/tabtoy/v3/report"
"strings"
)

// 枚举值的解析是放在输出端处理的, 例如json中, 所以在这里提前检查
Expand Down Expand Up @@ -32,17 +31,14 @@ func checkEnumValue(globals *model.Globals) {
continue
}

if inputCell.Value != "" {
if header.TypeInfo.IsArray() {

if header.TypeInfo.IsArray() {

for _, v := range strings.Split(inputCell.Value, header.TypeInfo.ArraySplitter) {
checkEnumFieldValue(globals, header, v, inputCell)
}

} else {
checkEnumFieldValue(globals, header, inputCell.Value, inputCell)
for _, v := range inputCell.ValueList {
checkEnumFieldValue(globals, header, v, inputCell)
}

} else {
checkEnumFieldValue(globals, header, inputCell.Value, inputCell)
}

}
Expand All @@ -52,6 +48,11 @@ func checkEnumValue(globals *model.Globals) {

// 检查枚举值是否存在有效
func checkEnumFieldValue(globals *model.Globals, header *model.HeaderField, value string, inputCell *model.Cell) {

if inputCell.Value == "" {
return
}

enumValue := globals.Types.GetEnumValue(header.TypeInfo.FieldType, value)
if enumValue == nil {
report.ReportError("UnknownEnumValue", header.TypeInfo.FieldType, inputCell.String())
Expand Down
49 changes: 47 additions & 2 deletions v3/checker/entry.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
package checker

import "github.com/davyxu/tabtoy/v3/model"
import (
"github.com/davyxu/tabtoy/v3/model"
"github.com/davyxu/tabtoy/v3/report"
)

func CheckData(globals *model.Globals) {
func PreCheck(dataList *model.DataTableList) {

type ArrayFieldDefine struct {
FieldName string
ObjectName string
}

fieldCountByField := map[ArrayFieldDefine]int{}

// 合并前的数据表
for _, tab := range dataList.AllTables() {

// 遍历输入数据的每一列
for _, header := range tab.Headers {

// 输入的列头,为空表示改列被注释
if header.TypeInfo == nil {
continue
}

fieldKey := ArrayFieldDefine{
FieldName: header.TypeInfo.FieldName,
ObjectName: header.TypeInfo.Name,
}

if header.TypeInfo.IsArray() {
arrayFieldCount := tab.ArrayFieldCount(header)
if preFieldCount, ok := fieldCountByField[fieldKey]; ok {

if preFieldCount != arrayFieldCount {
report.ReportError("ArrayMultiColumnDefineNotMatch")
}
} else {
fieldCountByField[fieldKey] = arrayFieldCount
}
}

}
}
}

// merge后检查
func PostCheck(globals *model.Globals) {

checkEnumValue(globals)
checkRepeat(globals)
Expand Down
3 changes: 2 additions & 1 deletion v3/compiler/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func Compile(globals *model.Globals) (ret error) {

report.Log.Debugln("Checking types...")
checker.CheckType(globals.Types)
checker.PreCheck(&dataList)

if kvList.Count() > 0 {
report.Log.Debugln("Merge key-value tables...")
Expand All @@ -77,7 +78,7 @@ func Compile(globals *model.Globals) (ret error) {
// 合并所有的数据表
MergeData(&dataList, &globals.Datas, globals.Types)

checker.CheckData(globals)
checker.PostCheck(globals)

return nil
}
24 changes: 11 additions & 13 deletions v3/compiler/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compiler
import (
"github.com/davyxu/tabtoy/v3/model"
"github.com/davyxu/tabtoy/v3/report"
"strings"
)

func createOutputTable(symbols *model.TypeTable, inputTab *model.DataTable) *model.DataTable {
Expand Down Expand Up @@ -114,23 +115,20 @@ func MergeData(inputList, outputList *model.DataTableList, symbols *model.TypeTa

func combineRepeatedCell(outputCell, inputCell *model.Cell, inputHeader *model.HeaderField, inputTab *model.DataTable) {

spliter := inputHeader.TypeInfo.ArraySplitter
// 数组列, 单列情况
if inputTab.ArrayFieldCount(inputHeader) == 1 {

if inputTab.RepeatedFieldIndex(inputHeader) > 0 {

if outputCell.Value != "" || model.LanguagePrimitive(inputHeader.TypeInfo.FieldType, "go") == "string" {
outputCell.Value += spliter
// 不为空时, 切割值为数组
if inputCell.Value != "" {
for _, element := range strings.Split(inputCell.Value, inputHeader.TypeInfo.ArraySplitter) {
outputCell.ValueList = append(outputCell.ValueList, element)
}
}
}

var inputValue string
if inputCell.Value == "" {
inputValue = model.FetchDefaultValue(inputHeader.TypeInfo.FieldType)
} else {
inputValue = inputCell.Value
}

// 将多个列中的数值合并到最终单元格的值中, Spliter分割, 分割过程在每种数据gen中处理
outputCell.Value += inputValue
// 数组列, 多列情况, 每列添加到单元格
outputCell.ValueList = append(outputCell.ValueList, inputCell.Value)
}

}
52 changes: 50 additions & 2 deletions v3/example/csharp/TabtoyExample/table_gen.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated by github.com/davyxu/tabtoy
// DO NOT EDIT!!
// Version: 3.0.1
// Version: 3.0.0
using System;
using System.Collections.Generic;

Expand All @@ -22,6 +22,8 @@ public partial class ExampleData : tabtoy.ITableSerializable
public float Rate = 0;
public ActorType Type = ActorType.None;
public List<Int32> Skill = new List<Int32>();
public Int32 Buff = 0;
public List<string> TagList = new List<string>();
public List<Int32> Multi = new List<Int32>();

#region Deserialize Code
Expand Down Expand Up @@ -57,7 +59,17 @@ public void Deserialize( tabtoy.TableReader reader )
reader.ReadInt32( ref Skill );
}
break;
case 0x660005:
case 0x20005:
{
reader.ReadInt32( ref Buff );
}
break;
case 0x6c0006:
{
reader.ReadString( ref TagList );
}
break;
case 0x660007:
{
reader.ReadInt32( ref Multi );
}
Expand All @@ -73,6 +85,34 @@ public void Deserialize( tabtoy.TableReader reader )
#endregion
}

public partial class ExtendData : tabtoy.ITableSerializable
{
public float Additive = 0;

#region Deserialize Code
public void Deserialize( tabtoy.TableReader reader )
{
UInt32 mamaSaidTagNameShouldBeLong = 0;
while ( reader.ReadTag(ref mamaSaidTagNameShouldBeLong) )
{
switch (mamaSaidTagNameShouldBeLong)
{
case 0x70000:
{
reader.ReadFloat( ref Additive );
}
break;
default:
{
reader.SkipFiled(mamaSaidTagNameShouldBeLong);
}
break;
}
}
}
#endregion
}

public partial class ExampleKV : tabtoy.ITableSerializable
{
public string ServerIP = string.Empty;
Expand Down Expand Up @@ -119,6 +159,8 @@ public partial class Table
{
// table: ExampleData
public List<ExampleData> ExampleData = new List<ExampleData>();
// table: ExtendData
public List<ExtendData> ExtendData = new List<ExtendData>();
// table: ExampleKV
public List<ExampleKV> ExampleKV = new List<ExampleKV>();

Expand All @@ -135,6 +177,7 @@ public ExampleKV GetKeyValue_ExampleKV()
public void ResetData( )
{
ExampleData.Clear();
ExtendData.Clear();
ExampleKV.Clear();
ExampleDataByID.Clear();
}
Expand All @@ -157,6 +200,11 @@ public void Deserialize( tabtoy.TableReader reader )
reader.ReadStruct(ref ExampleData);
}
break;
case "ExtendData":
{
reader.ReadStruct(ref ExtendData);
}
break;
case "ExampleKV":
{
reader.ReadStruct(ref ExampleKV);
Expand Down
17 changes: 14 additions & 3 deletions v3/example/csv/Data.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
ID,����,����,�����б�,����,�����б�,�����б�,����,����
100,������,3.14,3,����,1,2,1|2,3
200,��Ŭ��˹����,1.2,100,Genji,,90,4,
ID,����,����,�����б�,����,�����б�,�����б�,����,���,����,����
100,������,3.14,3,����,1,2,100,a|b,1,3
200,��Ŭ��˹����,1.2,100,����,,90,1,c,4,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
28 changes: 14 additions & 14 deletions v3/example/csv/Data2.csv
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
ID,����,����,����,�����б�
300,����,79.4,Դ��,
400,�����߶���,0.63,��ʹ,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
ID,����,����,����
300,����,79.4,Դ��
400,�����߶���,0.63,��ʹ
,,,
,,,
,,,
,,,
,,,
,,,
,,,
,,,
,,,
,,,
,,,
Loading

0 comments on commit 91f9ace

Please sign in to comment.