【特性】对c#语言,为联合索引和多重索引的table生成索引代码

main
walon 2022-01-25 13:23:21 +08:00
parent a41fd92b43
commit 582edb6cc6
5 changed files with 122 additions and 9 deletions

View File

@ -8,6 +8,8 @@ using System.Linq;
namespace Luban.Job.Cfg.Defs
{
public record class IndexInfo(TType Type, DefField IndexField, int IndexFieldIdIndex);
public class DefTable : CfgDefTypeBase
{
private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger();
@ -62,7 +64,7 @@ namespace Luban.Job.Cfg.Defs
public bool IsUnionIndex { get; private set; }
public List<(TType Type, DefField IndexField, int IndexFieldIdIndex)> IndexList { get; } = new();
public List<IndexInfo> IndexList { get; } = new();
public bool NeedExport => Assembly.NeedExport(this.Groups);
@ -139,7 +141,7 @@ namespace Luban.Job.Cfg.Defs
{
IndexField = f;
IndexFieldIdIndex = i;
this.IndexList.Add((f.CType, f, i));
this.IndexList.Add(new IndexInfo(f.CType, f, i));
}
else
{

View File

@ -4,6 +4,7 @@ using Luban.Job.Common.Defs;
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
using System;
using System.Linq;
namespace Luban.Job.Cfg.Utils
{
@ -207,6 +208,26 @@ namespace Luban.Job.Cfg.Utils
return type.Apply(RustJsonConstructorVisitor.Ins, jsonFieldName);
}
public static string CsTableUnionMapTypeName(DefTable table)
{
return $"Dictionary<({string.Join(", ", table.IndexList.Select(idx => CsDefineType(idx.Type)))}), {CsDefineType(table.ValueTType)}>";
}
public static string CsTableKeyList(DefTable table, string varName)
{
return string.Join(", ", table.IndexList.Select(idx => $"{varName}.{idx.IndexField.ConventionName}"));
}
public static string CsTableGetParamDefList(DefTable table)
{
return string.Join(", ", table.IndexList.Select(idx => $"{CsDefineType(idx.Type)} {idx.IndexField.Name}"));
}
public static string CsTableGetParamNameList(DefTable table)
{
return string.Join(", ", table.IndexList.Select(idx => $"{idx.IndexField.Name}"));
}
//public static string DeserializeTextKeyField(DefField field, string lan, string bufName)
//{
// switch (lan)

View File

@ -63,7 +63,15 @@ public sealed class {{name}}
}
{{~else if x.is_list_table ~}}
private readonly List<{{cs_define_type value_type}}> _dataList;
{{~if x.is_union_index~}}
private {{cs_table_union_map_type_name x}} _dataMapUnion;
{{~else if !x.index_list.empty?~}}
{{~for idx in x.index_list~}}
private Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}> _dataMap_{{idx.index_field.name}};
{{~end~}}
{{~end~}}
public {{name}}(ByteBuf _buf)
{
_dataList = new List<{{cs_define_type value_type}}>();
@ -74,12 +82,34 @@ public sealed class {{name}}
{{cs_deserialize '_buf' '_v' value_type}}
_dataList.Add(_v);
}
{{~if x.is_union_index~}}
_dataMapUnion = new {{cs_table_union_map_type_name x}}();
foreach(var _v in _dataList)
{
_dataMapUnion.Add(({{cs_table_key_list x "_v"}}), _v);
}
{{~else if !x.index_list.empty?~}}
{{~for idx in x.index_list~}}
_dataMap_{{idx.index_field.name}} = new Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}>();
{{~end~}}
foreach(var _v in _dataList)
{
{{~for idx in x.index_list~}}
_dataMap_{{idx.index_field.name}}.Add(_v.{{idx.index_field.convention_name}}, _v);
{{~end~}}
}
{{~end~}}
}
public List<{{cs_define_type value_type}}> DataList => _dataList;
public {{cs_define_type value_type}} Get(int index) => _dataList[index];
public {{cs_define_type value_type}} this[int index] => _dataList[index];
{{~if x.is_union_index~}}
public {{cs_define_type value_type}} Get({{cs_table_get_param_def_list x}}) => _dataMapUnion.TryGetValue(({{cs_table_get_param_name_list x}}), out {{cs_define_type value_type}} __v) ? __v : null;
{{~else if !x.index_list.empty? ~}}
{{~for idx in x.index_list~}}
public {{cs_define_type value_type}} GetBy{{idx.index_field.convention_name}}({{cs_define_type idx.type}} key) => _dataMap_{{idx.index_field.name}}.TryGetValue(key, out {{cs_define_type value_type}} __v) ? __v : null;
{{~end~}}
{{~end~}}
public void Resolve(Dictionary<string, object> _tables)
{

View File

@ -66,6 +66,14 @@ public sealed class {{name}}
{{~else if x.is_list_table ~}}
private readonly List<{{cs_define_type value_type}}> _dataList;
{{~if x.is_union_index~}}
private {{cs_table_union_map_type_name x}} _dataMapUnion;
{{~else if !x.index_list.empty?~}}
{{~for idx in x.index_list~}}
private Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}> _dataMap_{{idx.index_field.name}};
{{~end~}}
{{~end~}}
public {{name}}(JsonElement _json)
{
@ -76,12 +84,34 @@ public sealed class {{name}}
var _v = {{cs_define_type value_type}}.Deserialize{{value_type.bean.name}}(_row);
_dataList.Add(_v);
}
{{~if x.is_union_index~}}
_dataMapUnion = new {{cs_table_union_map_type_name x}}();
foreach(var _v in _dataList)
{
_dataMapUnion.Add(({{cs_table_key_list x "_v"}}), _v);
}
{{~else if !x.index_list.empty?~}}
{{~for idx in x.index_list~}}
_dataMap_{{idx.index_field.name}} = new Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}>();
{{~end~}}
foreach(var _v in _dataList)
{
{{~for idx in x.index_list~}}
_dataMap_{{idx.index_field.name}}.Add(_v.{{idx.index_field.convention_name}}, _v);
{{~end~}}
}
{{~end~}}
}
public List<{{cs_define_type value_type}}> DataList => _dataList;
public {{cs_define_type value_type}} Get(int index) => _dataList[index];
public {{cs_define_type value_type}} this[int index] => _dataList[index];
{{~if x.is_union_index~}}
public {{cs_define_type value_type}} Get({{cs_table_get_param_def_list x}}) => _dataMapUnion.TryGetValue(({{cs_table_get_param_name_list x}}), out {{cs_define_type value_type}} __v) ? __v : null;
{{~else if !x.index_list.empty? ~}}
{{~for idx in x.index_list~}}
public {{cs_define_type value_type}} GetBy{{idx.index_field.convention_name}}({{cs_define_type idx.type}} key) => _dataMap_{{idx.index_field.name}}.TryGetValue(key, out {{cs_define_type value_type}} __v) ? __v : null;
{{~end~}}
{{~end~}}
public void Resolve(Dictionary<string, object> _tables)
{

View File

@ -67,6 +67,14 @@ public sealed class {{name}}
{{~else if x.is_list_table ~}}
private readonly List<{{cs_define_type value_type}}> _dataList;
{{~if x.is_union_index~}}
private {{cs_table_union_map_type_name x}} _dataMapUnion;
{{~else if !x.index_list.empty?~}}
{{~for idx in x.index_list~}}
private Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}> _dataMap_{{idx.index_field.name}};
{{~end~}}
{{~end~}}
public {{name}}(JSONNode _json)
{
_dataList = new List<{{cs_define_type value_type}}>();
@ -76,12 +84,34 @@ public sealed class {{name}}
var _v = {{cs_define_type value_type}}.Deserialize{{value_type.bean.name}}(_row);
_dataList.Add(_v);
}
{{~if x.is_union_index~}}
_dataMapUnion = new {{cs_table_union_map_type_name x}}();
foreach(var _v in _dataList)
{
_dataMapUnion.Add(({{cs_table_key_list x "_v"}}), _v);
}
{{~else if !x.index_list.empty?~}}
{{~for idx in x.index_list~}}
_dataMap_{{idx.index_field.name}} = new Dictionary<{{cs_define_type idx.type}}, {{cs_define_type value_type}}>();
{{~end~}}
foreach(var _v in _dataList)
{
{{~for idx in x.index_list~}}
_dataMap_{{idx.index_field.name}}.Add(_v.{{idx.index_field.convention_name}}, _v);
{{~end~}}
}
{{~end~}}
}
public List<{{cs_define_type value_type}}> DataList => _dataList;
public {{cs_define_type value_type}} Get(int index) => _dataList[index];
public {{cs_define_type value_type}} this[int index] => _dataList[index];
{{~if x.is_union_index~}}
public {{cs_define_type value_type}} Get({{cs_table_get_param_def_list x}}) => _dataMapUnion.TryGetValue(({{cs_table_get_param_name_list x}}), out {{cs_define_type value_type}} __v) ? __v : null;
{{~else if !x.index_list.empty? ~}}
{{~for idx in x.index_list~}}
public {{cs_define_type value_type}} GetBy{{idx.index_field.convention_name}}({{cs_define_type idx.type}} key) => _dataMap_{{idx.index_field.name}}.TryGetValue(key, out {{cs_define_type value_type}} __v) ? __v : null;
{{~end~}}
{{~end~}}
public void Resolve(Dictionary<string, object> _tables)
{