Skip to content

Commit

Permalink
修复gen_metabug,添加更多示例
Browse files Browse the repository at this point in the history
  • Loading branch information
821869798 committed Jun 10, 2023
1 parent 140300f commit 7951550
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 3 deletions.
15 changes: 15 additions & 0 deletions examples/cs_bin_meta/big_data.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
target = "big_data"
mode = ""
source_type = "excel"

sources = [
{ file_name = "big_data.xlsx", sheet_name = "Sheet1" },
]

fields = [
{ active = true, sname = "id" , tname = "id" , type = "int" , key = 1, desc = "id" },
{ active = true, sname = "name" , tname = "name" , type = "string" , key = 0, desc = "名字" },
{ active = true, sname = "age" , tname = "age" , type = "int" , key = 0, desc = "年龄" },
{ active = true, sname = "course" , tname = "course" , type = "[]int" , key = 0, desc = "学科id" },
{ active = true, sname = "score" , tname = "score" , type = "map[int]int" , key = 0, desc = "成绩组" },
]
13 changes: 13 additions & 0 deletions examples/cs_bin_meta/csv_test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target = "csv_test"
mode = ""
source_type = "csv"

sources = [
{ file_name = "csv_test.csv", sheet_name = "" },
]

fields = [
{ active = true, sname = "id" , tname = "id" , type = "int" , key = 1, desc = "id" },
{ active = true, sname = "name" , tname = "name" , type = "string" , key = 0, desc = "名字" },
{ active = true, sname = "age" , tname = "age" , type = "int" , key = 0, desc = "年龄" },
]
Binary file added examples/excels/big_data.xlsx
Binary file not shown.
122 changes: 122 additions & 0 deletions examples/projects/proj_cs_bin/src/gen/Tblbig_data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using Serialization;

using System.Collections.Generic;

namespace CfgTable
{

public partial class Tblbig_data
{

private readonly Dictionary<int, Cfgbig_data> _dataMap;
private readonly List<Cfgbig_data> _dataList;


public Tblbig_data(ByteBuf _buf)
{
//first read common data
_TbCommonbig_data _commonData = null;
var commonSize = _buf.ReadSize();
if( commonSize > 0)
{
_commonData = new _TbCommonbig_data(_buf);
}

var size = _buf.ReadSize();
_dataMap = new Dictionary<int, Cfgbig_data>(16);
_dataList = new List<Cfgbig_data>(size);

for (int i = 0; i < size; i++)
{
Cfgbig_data _v;
_v = Cfgbig_data.DeserializeCfgbig_data(_buf, _commonData);
_dataList.Add(_v);
_dataMap[_v.id] = _v;
}

PostInit();
}

public int DataCount => _dataList.Count;
public Dictionary<int, Cfgbig_data> DataMap => _dataMap;
public List<Cfgbig_data> DataList => _dataList;

public Cfgbig_data GetDataById(int __k0) { if (_dataMap.TryGetValue(__k0, out var __tmpv0)) { return __tmpv0; } return null; }

/// <summary>
/// post process table
/// </summary>
partial void PostInit();

}

public partial class Cfgbig_data
{
private Cfgbig_data(ByteBuf _buf, _TbCommonbig_data _commonData)
{

id = _buf.ReadInt();
{ int dataIndex = _buf.ReadInt() - 1; name = _commonData._field0[dataIndex]; }
age = _buf.ReadInt();
{ int dataIndex = _buf.ReadInt() - 1; course = _commonData._field1[dataIndex]; }
{ int dataIndex = _buf.ReadInt() - 1; score = _commonData._field2[dataIndex]; }
PostInit();
}

internal static Cfgbig_data DeserializeCfgbig_data(ByteBuf _buf, _TbCommonbig_data _commonData)
{
return new Cfgbig_data(_buf, _commonData);
}

/// <summary>
/// id
/// </summary>
public int id { get; private set; }

/// <summary>
/// 名字
/// </summary>
public string name { get; private set; }

/// <summary>
/// 年龄
/// </summary>
public int age { get; private set; }

/// <summary>
/// 学科id
/// </summary>
public int[] course { get; private set; }

/// <summary>
/// 成绩组
/// </summary>
public Dictionary<int, int> score { get; private set; }


/// <summary>
/// post process table
/// </summary>
partial void PostInit();
}

/// <summary>
/// internal common data.Optimize memory
/// </summary>
internal class _TbCommonbig_data
{

internal string[] _field0 { get; private set; }
internal int[][] _field1 { get; private set; }
internal Dictionary<int, int>[] _field2 { get; private set; }
internal _TbCommonbig_data(ByteBuf _buf)
{

{int __n0 = _buf.ReadSize(); _field0 = new string[__n0];for(var __i0 = 0 ; __i0 < __n0 ; __i0++ ){string __v0; __v0 = _buf.ReadString(); _field0[__i0] = __v0;} }
{int __n0 = _buf.ReadSize(); _field1 = new int[__n0][];for(var __i0 = 0 ; __i0 < __n0 ; __i0++ ){int[] __v0; {int __n1 = _buf.ReadSize(); __v0 = new int[__n1];for(var __i1 = 0 ; __i1 < __n1 ; __i1++ ){int __v1; __v1 = _buf.ReadInt(); __v0[__i1] = __v1;} } _field1[__i0] = __v0;} }
{int __n0 = _buf.ReadSize(); _field2 = new Dictionary<int, int>[__n0];for(var __i0 = 0 ; __i0 < __n0 ; __i0++ ){Dictionary<int, int> __v0; { int __n1 = _buf.ReadSize(); var __f1 = new Dictionary<int, int> (__n1 * 3 / 2) ; __v0 = __f1; for(var __i1 = 0 ; __i1 < __n1 ; __i1++ ) {int __k1; __k1 = _buf.ReadInt(); int __v1; __v1 = _buf.ReadInt(); __f1.Add(__k1, __v1); } } _field2[__i0] = __v0;} }
}

}

}
104 changes: 104 additions & 0 deletions examples/projects/proj_cs_bin/src/gen/Tblcsv_test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Serialization;

using System.Collections.Generic;

namespace CfgTable
{

public partial class Tblcsv_test
{

private readonly Dictionary<int, Cfgcsv_test> _dataMap;
private readonly List<Cfgcsv_test> _dataList;


public Tblcsv_test(ByteBuf _buf)
{
//first read common data
_TbCommoncsv_test _commonData = null;
var commonSize = _buf.ReadSize();
if( commonSize > 0)
{
_commonData = new _TbCommoncsv_test(_buf);
}

var size = _buf.ReadSize();
_dataMap = new Dictionary<int, Cfgcsv_test>(16);
_dataList = new List<Cfgcsv_test>(size);

for (int i = 0; i < size; i++)
{
Cfgcsv_test _v;
_v = Cfgcsv_test.DeserializeCfgcsv_test(_buf, _commonData);
_dataList.Add(_v);
_dataMap[_v.id] = _v;
}

PostInit();
}

public int DataCount => _dataList.Count;
public Dictionary<int, Cfgcsv_test> DataMap => _dataMap;
public List<Cfgcsv_test> DataList => _dataList;

public Cfgcsv_test GetDataById(int __k0) { if (_dataMap.TryGetValue(__k0, out var __tmpv0)) { return __tmpv0; } return null; }

/// <summary>
/// post process table
/// </summary>
partial void PostInit();

}

public partial class Cfgcsv_test
{
private Cfgcsv_test(ByteBuf _buf, _TbCommoncsv_test _commonData)
{

id = _buf.ReadInt();
name = _buf.ReadString();
age = _buf.ReadInt();
PostInit();
}

internal static Cfgcsv_test DeserializeCfgcsv_test(ByteBuf _buf, _TbCommoncsv_test _commonData)
{
return new Cfgcsv_test(_buf, _commonData);
}

/// <summary>
/// id
/// </summary>
public int id { get; private set; }

/// <summary>
/// 名字
/// </summary>
public string name { get; private set; }

/// <summary>
/// 年龄
/// </summary>
public int age { get; private set; }


/// <summary>
/// post process table
/// </summary>
partial void PostInit();
}

/// <summary>
/// internal common data.Optimize memory
/// </summary>
internal class _TbCommoncsv_test
{

internal _TbCommoncsv_test(ByteBuf _buf)
{

}

}

}
Binary file not shown.
Binary file not shown.
22 changes: 19 additions & 3 deletions meta/gen_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func (g *GenMeta) Run() {
sourceSlice := strings.Split(g.genSource, ",")

inputOk := false
isCsv := false
if len(sourceSlice) == 2 && strings.HasSuffix(sourceSlice[1], ".csv") {
inputOk = true
isCsv = true
}
if len(sourceSlice) == 3 && !strings.HasSuffix(sourceSlice[1], ".csv") {
inputOk = true
Expand All @@ -55,7 +57,7 @@ func (g *GenMeta) Run() {
sheetName := ""
var rows [][]string
var err error
if len(sourceSlice) == 3 {
if !isCsv {
sheetName = sourceSlice[2]
rows, err = readExcelFile(filePath, sheetName)
} else {
Expand All @@ -73,7 +75,12 @@ func (g *GenMeta) Run() {
rtm := NewRawTableMeta()
rtm.Target = targetName
rtm.Mode = ""
rtm.SourceType = "excel"
if isCsv {
rtm.SourceType = "csv"
} else {
rtm.SourceType = "excel"
}

rtm.Sources = []*RawTableSource{
&RawTableSource{
Table: srcFileName,
Expand Down Expand Up @@ -127,5 +134,14 @@ func readCsvFile(filePath string) ([][]string, error) {
}
csvReader := csv.NewReader(csvFile) //创建一个新的写入文件流
csvReader.LazyQuotes = true
return csvReader.ReadAll()
rowData, err := csvReader.ReadAll()
if err != nil {
return nil, err
}
//去除utf-8的bom
if len(rowData) > 0 && len(rowData[0]) > 0 {
firstData := rowData[0][0]
rowData[0][0] = strings.TrimPrefix(firstData, "\uFEFF")
}
return rowData, nil
}

0 comments on commit 7951550

Please sign in to comment.