feat:支持多维数组嵌套

main
carson 2022-07-13 03:23:59 +08:00
parent d6ad1e64c4
commit 6620cca6e1
5 changed files with 57 additions and 12 deletions

View File

@ -92,7 +92,20 @@ namespace Luban.Job.Cfg.TypeVisitors
string __v = $"__v{Flag}"; string __v = $"__v{Flag}";
string __json = $"__json{Flag}"; string __json = $"__json{Flag}";
string __index = $"__index{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}; }} }}"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]";
if (type.Dimension > 1)
{
if (type.FinalElementType == null)
{
throw new System.Exception("¶àάÊý×éûÓÐÔªËØÀàÐÍ");
}
typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{_n}]";
for (int i = 0; i < type.Dimension - 1; i++)
{
typeStr += "[]";
}
}
return $"{{ var {__json} = {json}; int {_n} = {__json}.GetArrayLength(); {x} = new {typeStr}; 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) public string Accept(TList type, string json, string x)

View File

@ -94,7 +94,20 @@ namespace Luban.Job.Cfg.TypeVisitors
string __json = $"__json{Flag}"; string __json = $"__json{Flag}";
string __index = $"__index{Flag}"; string __index = $"__index{Flag}";
string tempJsonName = __json; 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}; }} }}"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{_n}]";
if (type.Dimension > 1)
{
if (type.FinalElementType == null)
{
throw new System.Exception("¶àάÊý×éûÓÐÔªËØÀàÐÍ");
}
typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{_n}]";
for (int i = 0; i < type.Dimension - 1; i++)
{
typeStr += "[]";
}
}
return $"{{ var {tempJsonName} = {json}; if(!{tempJsonName}.IsArray) {{ throw new SerializationException(); }} int {_n} = {tempJsonName}.Count; {x} = new {typeStr}; 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) public string Accept(TList type, string json, string x)

View File

@ -373,12 +373,7 @@ namespace Luban.Job.Common.Defs
{ {
case "array": case "array":
{ {
TType type = CreateType(module, elementType); return TArray.Create(false, containerTags, 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 "list": return TList.Create(false, containerTags, CreateType(module, elementType), true);
case "set": case "set":

View File

@ -88,10 +88,23 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TArray type, string bufName, string fieldName) public string Accept(TArray type, string bufName, string fieldName)
{ {
Flag++; Flag++;
string n = $"n{Flag}"; string __n = $"__n{Flag}";
string _e = $"_e{Flag}"; string __e = $"__e{Flag}";
string i = $"i{Flag}"; string __index = $"__index{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};}}}}"; string typeStr = $"{type.ElementType.Apply(CsDefineTypeName.Ins)}[{__n}]";
if (type.Dimension > 1)
{
if (type.FinalElementType == null)
{
throw new System.Exception("¶àάÊý×éûÓÐÔªËØÀàÐÍ");
}
typeStr = $"{type.FinalElementType.Apply(CsUnderingDefineTypeName.Ins)}[{__n}]";
for (int i = 0; i < type.Dimension - 1; i++)
{
typeStr += "[]";
}
}
return $"{{int {__n} = System.Math.Min({bufName}.ReadSize(), {bufName}.Size);{fieldName} = new {typeStr};for(var {__index} = 0 ; {__index} < {__n} ; {__index}++) {{ {type.ElementType.Apply(CsDefineTypeName.Ins)} {__e};{type.ElementType.Apply(this, bufName, $"{__e}")} {fieldName}[{__index}] = {__e};}}}}";
} }
public string Accept(TList type, string bufName, string fieldName) public string Accept(TList type, string bufName, string fieldName)

View File

@ -15,10 +15,21 @@ namespace Luban.Job.Common.Types
public override TType ElementType { get; } public override TType ElementType { get; }
public override string TypeName => "array"; public override string TypeName => "array";
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)
{ {
ElementType = elementType; ElementType = elementType;
if (!elementType.IsCollection)
{
FinalElementType = ElementType;
}
if (ElementType.TypeName == "array")
{
Dimension = (ElementType as TArray).Dimension + 1;
FinalElementType = (ElementType as TArray).FinalElementType;
}
} }
public override bool TryParseFrom(string s) public override bool TryParseFrom(string s)