fea:支持嵌套容器,暂时只支持list

main
Carson Lin 2022-07-12 22:30:14 +08:00
parent c0ddce497c
commit d6ad1e64c4
6 changed files with 103 additions and 36 deletions

View File

@ -8,7 +8,7 @@ namespace Luban.Job.Cfg.TypeVisitors
class CsJsonDeserialize : ITypeFuncVisitor<string, string, string>
{
public static CsJsonDeserialize Ins { get; } = new();
public int Flag { get; set; }
public string Accept(TBool type, string json, string x)
{
return $"{x} = {json}.GetBoolean();";
@ -86,17 +86,33 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TArray type, string json, string x)
{
return $"{{ var _json0 = {json}; int _n = _json0.GetArrayLength(); {x} = new {type.ElementType.CsUnderingDefineType()}[_n]; int _index=0; foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} }}";
Flag++;
string _n = $"_n{Flag}";
string __e = $"__e{Flag}";
string __v = $"__v{Flag}";
string __json = $"__json{Flag}";
string __index = $"__index{Flag}";
return $"{{ var {__json} = {json}; int {_n} = {__json}.GetArrayLength(); {x} = new {type.ElementType.CsUnderingDefineType()}[{_n}]; int {__index}=0; foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}")} {x}[{__index}++] = {__v}; }} }}";
}
public string Accept(TList type, string json, string x)
{
return $"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
Flag++;
string _n = $"_n{Flag}";
string __e = $"__e{Flag}";
string __v = $"__v{Flag}";
string __json = $"__json{Flag}";
return $"{{ var {__json} = {json}; {x} = new {type.CsUnderingDefineType()}({__json}.GetArrayLength()); foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}")} {x}.Add({__v}); }} }}";
}
public string Accept(TSet type, string json, string x)
{
return $"{{ var _json0 = {json}; {x} = new {type.CsUnderingDefineType()}(_json0.GetArrayLength()); foreach(JsonElement __e in _json0.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
Flag++;
string _n = $"_n{Flag}";
string __e = $"__e{Flag}";
string __v = $"__v{Flag}";
string __json = $"__json{Flag}";
return $"{{ var {__json} = {json}; {x} = new {type.CsUnderingDefineType()}({__json}.GetArrayLength()); foreach(JsonElement {__e} in {__json}.EnumerateArray()) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, $"{__e}", $"{__v}")} {x}.Add({__v}); }} }}";
}
public string Accept(TMap type, string json, string x)

View File

@ -8,6 +8,7 @@ namespace Luban.Job.Cfg.TypeVisitors
class CsUnityJsonDeserialize : ITypeFuncVisitor<string, string, string>
{
public static CsUnityJsonDeserialize Ins { get; } = new();
public int Flag { get; set; }
public string Accept(TBool type, string json, string x)
{
@ -86,20 +87,34 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TArray type, string json, string x)
{
string tempJsonName = $"_json1";
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} int _n = {tempJsonName}.Count; {x} = new {type.ElementType.CsUnderingDefineType()}[_n]; int _index=0; foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}[_index++] = __v; }} }}";
Flag++;
string _n = $"_n{Flag}";
string __e = $"__e{Flag}";
string __v = $"__v{Flag}";
string __json = $"__json{Flag}";
string __index = $"__index{Flag}";
string tempJsonName = __json;
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} int {_n} = {tempJsonName}.Count; {x} = new {type.ElementType.CsUnderingDefineType()}[{_n}]; int {__index}=0; foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v)} {x}[{__index}++] = {__v}; }} }}";
}
public string Accept(TList type, string json, string x)
{
string tempJsonName = $"_json1";
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
Flag++;
string __e = $"__e{Flag}";
string __v = $"__v{Flag}";
string __json = $"__json{Flag}";
string tempJsonName = __json;
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}({tempJsonName}.Count); foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v)} {x}.Add({__v}); }} }}";
}
public string Accept(TSet type, string json, string x)
{
string tempJsonName = $"_json1";
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}(/*{tempJsonName}.Count*/); foreach(JSONNode __e in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} __v; {type.ElementType.Apply(this, "__e", "__v")} {x}.Add(__v); }} }}";
Flag++;
string __e = $"__e{Flag}";
string __v = $"__v{Flag}";
string __json = $"__json{Flag}";
string tempJsonName = __json;
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} {x} = new {type.CsUnderingDefineType()}(/*{tempJsonName}.Count*/); foreach(JSONNode {__e} in {tempJsonName}.Children) {{ {type.ElementType.CsUnderingDefineType()} {__v}; {type.ElementType.Apply(this, __e, __v)} {x}.Add({__v}); }} }}";
}
public string Accept(TMap type, string json, string x)

View File

@ -34,7 +34,7 @@ namespace Luban.Job.Common.Defs
public Dictionary<string, DefTypeBase> Types { get; } = new Dictionary<string, DefTypeBase>();
private readonly Dictionary<string, DefTypeBase> _notCaseSenseTypes = new ();
private readonly Dictionary<string, DefTypeBase> _notCaseSenseTypes = new();
private readonly HashSet<string> _namespaces = new();
@ -275,6 +275,7 @@ namespace Luban.Job.Common.Defs
public TType CreateType(string module, string type)
{
type = DefUtil.TrimBracePairs(type, true);
int sepIndex = DefUtil.IndexOfBaseTypeEnd(type);
if (sepIndex > 0)
{
@ -370,9 +371,25 @@ namespace Luban.Job.Common.Defs
{
switch (containerType)
{
case "array": return TArray.Create(false, containerTags, CreateNotContainerType(module, elementType));
case "list": return TList.Create(false, containerTags, CreateNotContainerType(module, elementType), true);
case "set": return TSet.Create(false, containerTags, CreateNotContainerType(module, elementType), false);
case "array":
{
TType type = CreateType(module, elementType);
if (type.TypeName == "array")
{
throw new Exception("不支持多维数组请改为嵌套list");
}
return TArray.Create(false, containerTags, type);
}
case "list": return TList.Create(false, containerTags, CreateType(module, elementType), true);
case "set":
{
TType type = CreateType(module, elementType);
if (type.IsCollection)
{
throw new Exception("set的元素不支持容器类型请改为嵌套list");
}
return TSet.Create(false, containerTags, type, false);
}
case "map": return CreateMapType(module, containerTags, elementType, false);
default:
{

View File

@ -5,7 +5,6 @@ namespace Luban.Job.Common.TypeVisitors
public class CsDeserializeVisitor : DecoratorFuncVisitor<string, string, string>
{
public static CsDeserializeVisitor Ins { get; } = new CsDeserializeVisitor();
public override string DoAccept(TType type, string bufName, string fieldName)
{
if (type.IsNullable)

View File

@ -7,6 +7,7 @@ namespace Luban.Job.Common.TypeVisitors
class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
{
public static CsUnderingDeserializeVisitor Ins { get; } = new CsUnderingDeserializeVisitor();
public int Flag { get; set; }
public string Accept(TBool type, string bufName, string fieldName)
{
@ -86,17 +87,29 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TArray type, string bufName, string fieldName)
{
return $"{{int n = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.ElementType.Apply(CsDefineTypeName.Ins)}[n];for(var i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} _e;{type.ElementType.Apply(this, bufName, "_e")} {fieldName}[i] = _e;}}}}";
Flag++;
string n = $"n{Flag}";
string _e = $"_e{Flag}";
string i = $"i{Flag}";
return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.ElementType.Apply(CsDefineTypeName.Ins)}[{n}];for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e};{type.ElementType.Apply(this, bufName, $"{_e}")} {fieldName}[{i}] = {_e};}}}}";
}
public string Accept(TList type, string bufName, string fieldName)
{
return $"{{int n = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}(n);for(var i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {fieldName}.Add(_e);}}}}";
Flag++;
string n = $"n{Flag}";
string _e = $"_e{Flag}";
string i = $"i{Flag}";
return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}({n});for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e}; {type.ElementType.Apply(this, bufName, $"{_e}")} {fieldName}.Add({_e});}}}}";
}
public string Accept(TSet type, string bufName, string fieldName)
{
return $"{{int n = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}(/*n * 3 / 2*/);for(var i = 0 ; i < n ; i++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {fieldName}.Add(_e);}}}}";
Flag++;
string n = $"n{Flag}";
string _e = $"_e{Flag}";
string i = $"i{Flag}";
return $"{{int {n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {type.Apply(CsDefineTypeName.Ins)}(/*{n} * 3 / 2*/);for(var {i} = 0 ; {i} < {n} ; {i}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {_e}; {type.ElementType.Apply(this, bufName, $"{_e}")} {fieldName}.Add({_e});}}}}";
}
public string Accept(TMap type, string bufName, string fieldName)

View File

@ -136,7 +136,7 @@ namespace Luban.Job.Common.Utils
return -1;
}
public static string TrimBracePairs(string rawType)
public static string TrimBracePairs(string rawType, bool soft = false)
{
while (rawType.Length > 0 && rawType[0] == '(')
{
@ -145,10 +145,17 @@ namespace Luban.Job.Common.Utils
rawType = rawType.Substring(1, rawType.Length - 2);
}
else
{
if (soft)
{
return rawType;
}
else
{
throw new Exception($"type:{rawType} brace not match");
}
}
}
return rawType;
}