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> 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,17 +86,33 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TArray type, string json, string x) 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) 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) 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) 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> 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)
{ {
@ -86,20 +87,34 @@ namespace Luban.Job.Cfg.TypeVisitors
public string Accept(TArray type, string json, string x) public string Accept(TArray type, string json, string x)
{ {
string tempJsonName = $"_json1"; Flag++;
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 _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) public string Accept(TList type, string json, string x)
{ {
string tempJsonName = $"_json1"; Flag++;
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); }} }}"; 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) public string Accept(TSet type, string json, string x)
{ {
string tempJsonName = $"_json1"; Flag++;
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); }} }}"; 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) 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>(); 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(); private readonly HashSet<string> _namespaces = new();
@ -275,6 +275,7 @@ namespace Luban.Job.Common.Defs
public TType CreateType(string module, string type) public TType CreateType(string module, string type)
{ {
type = DefUtil.TrimBracePairs(type, true);
int sepIndex = DefUtil.IndexOfBaseTypeEnd(type); int sepIndex = DefUtil.IndexOfBaseTypeEnd(type);
if (sepIndex > 0) if (sepIndex > 0)
{ {
@ -340,17 +341,17 @@ namespace Luban.Job.Common.Defs
case "time": case "time":
case "datetime": return SupportDatetimeType ? TDateTime.Create(nullable, tags) : throw new NotSupportedException($"只有配置支持datetime数据类型"); case "datetime": return SupportDatetimeType ? TDateTime.Create(nullable, tags) : throw new NotSupportedException($"只有配置支持datetime数据类型");
default: default:
{
var dtype = GetDefTType(module, type, nullable, tags);
if (dtype != null)
{ {
return dtype; var dtype = GetDefTType(module, type, nullable, tags);
if (dtype != null)
{
return dtype;
}
else
{
throw new ArgumentException($"invalid type. module:'{module}' type:'{type}'");
}
} }
else
{
throw new ArgumentException($"invalid type. module:'{module}' type:'{type}'");
}
}
} }
} }
@ -370,14 +371,30 @@ namespace Luban.Job.Common.Defs
{ {
switch (containerType) switch (containerType)
{ {
case "array": return TArray.Create(false, containerTags, CreateNotContainerType(module, elementType)); case "array":
case "list": return TList.Create(false, containerTags, CreateNotContainerType(module, elementType), true); {
case "set": return TSet.Create(false, containerTags, CreateNotContainerType(module, elementType), false); 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); case "map": return CreateMapType(module, containerTags, elementType, false);
default: default:
{ {
throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'"); throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'");
} }
} }
} }
} }

View File

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

View File

@ -7,6 +7,7 @@ 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)
{ {
@ -86,17 +87,29 @@ namespace Luban.Job.Common.TypeVisitors
public string Accept(TArray type, string bufName, string fieldName) 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) 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) 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) public string Accept(TMap type, string bufName, string fieldName)

View File

@ -136,7 +136,7 @@ namespace Luban.Job.Common.Utils
return -1; return -1;
} }
public static string TrimBracePairs(string rawType) public static string TrimBracePairs(string rawType, bool soft = false)
{ {
while (rawType.Length > 0 && rawType[0] == '(') while (rawType.Length > 0 && rawType[0] == '(')
{ {
@ -146,7 +146,14 @@ namespace Luban.Job.Common.Utils
} }
else else
{ {
throw new Exception($"type:{rawType} brace not match"); if (soft)
{
return rawType;
}
else
{
throw new Exception($"type:{rawType} brace not match");
}
} }
} }
return rawType; return rawType;
@ -200,7 +207,7 @@ namespace Luban.Job.Common.Utils
return (typeStr, attrs); return (typeStr, attrs);
} }
public static bool ParseOrientation(string value) public static bool ParseOrientation(string value)
{ {
switch (value.Trim()) switch (value.Trim())
{ {
@ -210,9 +217,9 @@ namespace Luban.Job.Common.Utils
case "c": case "c":
case "column": return false; case "column": return false;
default: default:
{ {
throw new Exception($"orientation 属性值只能为row|r|column|c"); throw new Exception($"orientation 属性值只能为row|r|column|c");
} }
} }
} }