diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs index a108c7a..2e38343 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsJsonDeserialize.cs @@ -8,7 +8,7 @@ namespace Luban.Job.Cfg.TypeVisitors class CsJsonDeserialize : ITypeFuncVisitor { 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) diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs index c6e3a01..a459cd1 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CsUnityJsonDeserialize.cs @@ -8,6 +8,7 @@ namespace Luban.Job.Cfg.TypeVisitors class CsUnityJsonDeserialize : ITypeFuncVisitor { 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) diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index 233a692..c4e0778 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -34,7 +34,7 @@ namespace Luban.Job.Common.Defs public Dictionary Types { get; } = new Dictionary(); - private readonly Dictionary _notCaseSenseTypes = new (); + private readonly Dictionary _notCaseSenseTypes = new(); private readonly HashSet _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) { @@ -340,17 +341,17 @@ namespace Luban.Job.Common.Defs case "time": case "datetime": return SupportDatetimeType ? TDateTime.Create(nullable, tags) : throw new NotSupportedException($"只有配置支持datetime数据类型"); 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) { - 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: - { - throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'"); - } + { + throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'"); + } } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs index 19f7dbe..abf6b63 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsDeserializeVisitor.cs @@ -5,7 +5,6 @@ namespace Luban.Job.Common.TypeVisitors public class CsDeserializeVisitor : DecoratorFuncVisitor { public static CsDeserializeVisitor Ins { get; } = new CsDeserializeVisitor(); - public override string DoAccept(TType type, string bufName, string fieldName) { if (type.IsNullable) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs index a4a8c0f..af23211 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CsUnderingDeserializeVisitor.cs @@ -7,6 +7,7 @@ namespace Luban.Job.Common.TypeVisitors class CsUnderingDeserializeVisitor : ITypeFuncVisitor { 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) diff --git a/src/Luban.Job.Common/Source/Utils/DefUtil.cs b/src/Luban.Job.Common/Source/Utils/DefUtil.cs index 399130a..b17069a 100644 --- a/src/Luban.Job.Common/Source/Utils/DefUtil.cs +++ b/src/Luban.Job.Common/Source/Utils/DefUtil.cs @@ -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] == '(') { @@ -146,7 +146,14 @@ namespace Luban.Job.Common.Utils } else { - throw new Exception($"type:{rawType} brace not match"); + if (soft) + { + return rawType; + } + else + { + throw new Exception($"type:{rawType} brace not match"); + } } } return rawType; @@ -200,7 +207,7 @@ namespace Luban.Job.Common.Utils return (typeStr, attrs); } - public static bool ParseOrientation(string value) + public static bool ParseOrientation(string value) { switch (value.Trim()) { @@ -210,9 +217,9 @@ namespace Luban.Job.Common.Utils case "c": case "column": return false; default: - { - throw new Exception($"orientation 属性值只能为row|r|column|c"); - } + { + throw new Exception($"orientation 属性值只能为row|r|column|c"); + } } }