feat: 在TType添加CollectionLevel,用于连续嵌套容器时生成解析代码

main
Carson Lin 2022-07-13 10:28:09 +08:00
parent 6620cca6e1
commit 79940dcf09
7 changed files with 68 additions and 47 deletions

View File

@ -8,7 +8,6 @@ namespace Luban.Job.Cfg.TypeVisitors
class CsJsonDeserialize : ITypeFuncVisitor<string, string, string> class CsJsonDeserialize : ITypeFuncVisitor<string, string, string>
{ {
public static CsJsonDeserialize Ins { get; } = new(); public static CsJsonDeserialize Ins { get; } = new();
public int Flag { get; set; }
public string Accept(TBool type, string json, string x) public string Accept(TBool type, string json, string x)
{ {
return $"{x} = {json}.GetBoolean();"; return $"{x} = {json}.GetBoolean();";
@ -86,12 +85,12 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TArray type, string json, string x) public string Accept(TArray type, string json, string x)
{ {
Flag++; int level = type.CollectionLevel;
string _n = $"_n{Flag}"; string _n = $"_n{level}";
string __e = $"__e{Flag}"; string __e = $"__e{level}";
string __v = $"__v{Flag}"; string __v = $"__v{level}";
string __json = $"__json{Flag}"; string __json = $"__json{level}";
string __index = $"__index{Flag}"; string __index = $"__index{level}";
string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]";
if (type.Dimension > 1) if (type.Dimension > 1)
{ {
@ -110,21 +109,19 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TList type, string json, string x) public string Accept(TList type, string json, string x)
{ {
Flag++; int level = type.CollectionLevel;
string _n = $"_n{Flag}"; string __e = $"__e{level}";
string __e = $"__e{Flag}"; string __v = $"__v{level}";
string __v = $"__v{Flag}"; string __json = $"__json{level}";
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}); }} }}"; 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) public string Accept(TSet type, string json, string x)
{ {
Flag++; int level = type.CollectionLevel;
string _n = $"_n{Flag}"; string __e = $"__e{level}";
string __e = $"__e{Flag}"; string __v = $"__v{level}";
string __v = $"__v{Flag}"; string __json = $"__json{level}";
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}); }} }}"; 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}); }} }}";
} }

View File

@ -8,7 +8,6 @@ namespace Luban.Job.Cfg.TypeVisitors
class CsUnityJsonDeserialize : ITypeFuncVisitor<string, string, string> class CsUnityJsonDeserialize : ITypeFuncVisitor<string, string, string>
{ {
public static CsUnityJsonDeserialize Ins { get; } = new(); public static CsUnityJsonDeserialize Ins { get; } = new();
public int Flag { get; set; }
public string Accept(TBool type, string json, string x) public string Accept(TBool type, string json, string x)
{ {
@ -87,12 +86,12 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TArray type, string json, string x) public string Accept(TArray type, string json, string x)
{ {
Flag++; int level = type.CollectionLevel;
string _n = $"_n{Flag}"; string _n = $"_n{level}";
string __e = $"__e{Flag}"; string __e = $"__e{level}";
string __v = $"__v{Flag}"; string __v = $"__v{level}";
string __json = $"__json{Flag}"; string __json = $"__json{level}";
string __index = $"__index{Flag}"; string __index = $"__index{level}";
string tempJsonName = __json; string tempJsonName = __json;
string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]";
if (type.Dimension > 1) if (type.Dimension > 1)
@ -112,20 +111,20 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TList type, string json, string x) public string Accept(TList type, string json, string x)
{ {
Flag++; int level = type.CollectionLevel;
string __e = $"__e{Flag}"; string __e = $"__e{level}";
string __v = $"__v{Flag}"; string __v = $"__v{level}";
string __json = $"__json{Flag}"; string __json = $"__json{level}";
string tempJsonName = __json; 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}); }} }}"; 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) public string Accept(TSet type, string json, string x)
{ {
Flag++; int level = type.CollectionLevel;
string __e = $"__e{Flag}"; string __e = $"__e{level}";
string __v = $"__v{Flag}"; string __v = $"__v{level}";
string __json = $"__json{Flag}"; string __json = $"__json{level}";
string tempJsonName = __json; 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}); }} }}"; 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}); }} }}";
} }

View File

@ -7,7 +7,6 @@ namespace Luban.Job.Common.TypeVisitors
class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string> class CsUnderingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
{ {
public static CsUnderingDeserializeVisitor Ins { get; } = new CsUnderingDeserializeVisitor(); public static CsUnderingDeserializeVisitor Ins { get; } = new CsUnderingDeserializeVisitor();
public int Flag { get; set; }
public string Accept(TBool type, string bufName, string fieldName) public string Accept(TBool type, string bufName, string fieldName)
{ {
@ -87,10 +86,10 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TArray type, string bufName, string fieldName) public string Accept(TArray type, string bufName, string fieldName)
{ {
Flag++; int level = type.CollectionLevel;
string __n = $"__n{Flag}"; string __n = $"__n{level}";
string __e = $"__e{Flag}"; string __e = $"__e{level}";
string __index = $"__index{Flag}"; string __index = $"__index{level}";
string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]";
if (type.Dimension > 1) if (type.Dimension > 1)
{ {
@ -109,19 +108,19 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TList type, string bufName, string fieldName) public string Accept(TList type, string bufName, string fieldName)
{ {
Flag++; int level = type.CollectionLevel;
string n = $"n{Flag}"; string n = $"n{level}";
string _e = $"_e{Flag}"; string _e = $"_e{level}";
string i = $"i{Flag}"; string i = $"i{level}";
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});}}}}"; 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) public string Accept(TSet type, string bufName, string fieldName)
{ {
Flag++; int level = type.CollectionLevel;
string n = $"n{Flag}"; string n = $"n{level}";
string _e = $"_e{Flag}"; string _e = $"_e{level}";
string i = $"i{Flag}"; string i = $"i{level}";
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});}}}}"; 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});}}}}";
} }

View File

@ -16,7 +16,6 @@ namespace Luban.Job.Common.Types
public override string TypeName => "array"; public override string TypeName => "array";
public int Dimension { get; } = 1; public int Dimension { get; } = 1;
public TType FinalElementType { get; }
private TArray(bool isNullable, Dictionary<string, string> tags, TType elementType) : base(isNullable, tags) private TArray(bool isNullable, Dictionary<string, string> tags, TType elementType) : base(isNullable, tags)
{ {
@ -24,11 +23,16 @@ namespace Luban.Job.Common.Types
if (!elementType.IsCollection) if (!elementType.IsCollection)
{ {
FinalElementType = ElementType; FinalElementType = ElementType;
CollectionLevel = 0;
}
else
{
CollectionLevel = ElementType.CollectionLevel + 1;
FinalElementType = ElementType.FinalElementType;
} }
if (ElementType.TypeName == "array") if (ElementType.TypeName == "array")
{ {
Dimension = (ElementType as TArray).Dimension + 1; Dimension = (ElementType as TArray).Dimension + 1;
FinalElementType = (ElementType as TArray).FinalElementType;
} }
} }

View File

@ -22,6 +22,16 @@ namespace Luban.Job.Common.Types
{ {
ElementType = elementType; ElementType = elementType;
IsArrayList = isArrayList; IsArrayList = isArrayList;
if (!elementType.IsCollection)
{
FinalElementType = ElementType;
CollectionLevel = 0;
}
else
{
CollectionLevel = ElementType.CollectionLevel + 1;
FinalElementType = ElementType.FinalElementType;
}
} }
public override bool TryParseFrom(string s) public override bool TryParseFrom(string s)

View File

@ -22,6 +22,16 @@ namespace Luban.Job.Common.Types
{ {
ElementType = elementType; ElementType = elementType;
IsOrderSet = isOrderSet; IsOrderSet = isOrderSet;
if (!elementType.IsCollection)
{
FinalElementType = ElementType;
CollectionLevel = 0;
}
else
{
CollectionLevel = ElementType.CollectionLevel + 1;
FinalElementType = ElementType.FinalElementType;
}
} }
public override bool TryParseFrom(string s) public override bool TryParseFrom(string s)

View File

@ -19,6 +19,8 @@ namespace Luban.Job.Common.Types
} }
public abstract string TypeName { get; } public abstract string TypeName { get; }
public TType FinalElementType { get; protected set; }
public int CollectionLevel { get; protected set; }
public bool HasTag(string attrName) public bool HasTag(string attrName)
{ {