Skip to content

Commit bc951d2

Browse files
committed
添加: C#索引及KV读取
1 parent e474564 commit bc951d2

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

v3/example/csharp/TabtoyExample/table_gen.cs

+27-2
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,39 @@ public void Deserialize( tabtoy.TableReader reader )
101101
// Combine struct
102102
public partial class Table
103103
{
104-
public List<ExampleData> ExampleData = new List<ExampleData>(); // table: ExampleData
105-
public List<ExampleKV> ExampleKV = new List<ExampleKV>(); // table: ExampleKV
104+
// table: ExampleData
105+
public List<ExampleData> ExampleData = new List<ExampleData>();
106+
// table: ExampleKV
107+
public List<ExampleKV> ExampleKV = new List<ExampleKV>();
108+
109+
// Indices
110+
public Dictionary<Int32,ExampleData> ExampleDataByID = new Dictionary<Int32,ExampleData>();
111+
112+
113+
// table: ExampleKV
114+
public ExampleKV GetKeyValue_ExampleKV()
115+
{
116+
return ExampleKV[0];
117+
}
118+
119+
public void ResetData( )
120+
{
121+
ExampleData.Clear();
122+
ExampleKV.Clear();
123+
ExampleDataByID.Clear();
124+
}
106125

107126
public void Deserialize( tabtoy.TableReader reader )
108127
{
109128
reader.ReadHeader();
110129
reader.ReadStruct(ref ExampleData);
111130
reader.ReadStruct(ref ExampleKV);
131+
132+
foreach( var kv in ExampleData )
133+
{
134+
ExampleDataByID[kv.ID] = kv;
135+
}
136+
112137
}
113138
}
114139
}

v3/gen/cssrc/text.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,35 @@ namespace {{.PackageName}}
4141
// Combine struct
4242
public partial class {{.CombineStructName}}
4343
{ {{range $ti, $tab := $.Datas.AllTables}}
44-
public List<{{$tab.HeaderType}}> {{$tab.HeaderType}} = new List<{{$tab.HeaderType}}>(); // table: {{$tab.HeaderType}} {{end}}
44+
// table: {{$tab.HeaderType}}
45+
public List<{{$tab.HeaderType}}> {{$tab.HeaderType}} = new List<{{$tab.HeaderType}}>(); {{end}}
46+
47+
// Indices {{range $ii, $idx := GetIndices $}}
48+
public Dictionary<{{CSType $idx.FieldInfo}},{{$idx.Table.HeaderType}}> {{$idx.Table.HeaderType}}By{{$idx.FieldInfo.FieldName}} = new Dictionary<{{CSType $idx.FieldInfo}},{{$idx.Table.HeaderType}}>(); {{end}}
49+
50+
{{if HasKeyValueTypes $}}
51+
//{{range $ti, $name := GetKeyValueTypeNames $}} table: {{$name}}
52+
public {{$name}} GetKeyValue_{{$name}}()
53+
{
54+
return {{$name}}[0];
55+
}{{end}}{{end}}
56+
57+
public void ResetData( )
58+
{ {{range $ti, $tab := $.Datas.AllTables}}
59+
{{$tab.HeaderType}}.Clear(); {{end}} {{range $ii, $idx := GetIndices $}}
60+
{{$idx.Table.HeaderType}}By{{$idx.FieldInfo.FieldName}}.Clear(); {{end}}
61+
}
4562
4663
public void Deserialize( tabtoy.TableReader reader )
4764
{
4865
reader.ReadHeader();{{range $ti, $tab := $.Datas.AllTables}}
4966
reader.ReadStruct(ref {{$tab.HeaderType}}); {{end}}
67+
{{range $ii, $idx := GetIndices $}}
68+
foreach( var kv in {{$idx.Table.HeaderType}} )
69+
{
70+
{{$idx.Table.HeaderType}}By{{$idx.FieldInfo.FieldName}}[kv.{{$idx.FieldInfo.FieldName}}] = kv;
71+
}
72+
{{end}}
5073
}
5174
}
5275
}

v3/gen/gosrc/func.go

-18
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@ package gosrc
22

33
import (
44
"fmt"
5-
"github.com/ahmetb/go-linq"
65
"github.com/davyxu/tabtoy/v3/model"
76
"strings"
87
"text/template"
98
)
109

1110
var UsefulFunc = template.FuncMap{}
1211

13-
func KeyValueTypeNames(globals *model.Globals) (ret []string) {
14-
linq.From(globals.IndexList).WhereT(func(pragma *model.IndexDefine) bool {
15-
return pragma.Kind == model.TableKind_KeyValue
16-
}).SelectT(func(pragma *model.IndexDefine) string {
17-
18-
return pragma.TableType
19-
}).Distinct().ToSlice(&ret)
20-
21-
return
22-
}
23-
2412
// 将定义用的类型,转换为不同语言对应的复合类型
2513

2614
func init() {
@@ -58,12 +46,6 @@ func init() {
5846
return sb.String()
5947
}
6048

61-
UsefulFunc["HasKeyValueTypes"] = func(globals *model.Globals) bool {
62-
return len(KeyValueTypeNames(globals)) > 0
63-
}
64-
65-
UsefulFunc["GetKeyValueTypeNames"] = KeyValueTypeNames
66-
6749
UsefulFunc["JsonTabOmit"] = func() string {
6850
return "`json:\"-\"`"
6951
}

v3/gen/sharefunc.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gen
22

33
import (
4+
"github.com/ahmetb/go-linq"
45
"github.com/davyxu/tabtoy/v3/model"
56
"text/template"
67
)
@@ -12,7 +13,23 @@ type TableIndices struct {
1213
FieldInfo *model.TypeDefine
1314
}
1415

16+
func KeyValueTypeNames(globals *model.Globals) (ret []string) {
17+
linq.From(globals.IndexList).WhereT(func(pragma *model.IndexDefine) bool {
18+
return pragma.Kind == model.TableKind_KeyValue
19+
}).SelectT(func(pragma *model.IndexDefine) string {
20+
21+
return pragma.TableType
22+
}).Distinct().ToSlice(&ret)
23+
24+
return
25+
}
26+
1527
func init() {
28+
UsefulFunc["HasKeyValueTypes"] = func(globals *model.Globals) bool {
29+
return len(KeyValueTypeNames(globals)) > 0
30+
}
31+
32+
UsefulFunc["GetKeyValueTypeNames"] = KeyValueTypeNames
1633

1734
UsefulFunc["GetIndices"] = func(globals *model.Globals) (ret []TableIndices) {
1835

0 commit comments

Comments
 (0)