From 6e091c76408a80ea4f8a8d85f38c44bf11dd8ee4 Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 19 Jul 2021 18:08:33 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=AE=8C=E5=96=84=E3=80=91=E5=AE=8C?= =?UTF-8?q?=E5=96=84db=20typescript=20=E7=94=9F=E6=88=90=EF=BC=8C=E8=87=B3?= =?UTF-8?q?=E5=B0=91=E4=BF=9D=E8=AF=81=E6=9A=B4=E9=9C=B2=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=9A=84=E6=AD=A3=E7=A1=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Luban.Job.Cfg/Source/Defs/DefField.cs | 2 +- ...ypescriptJsonUnderingConstructorVisitor.cs | 8 +- .../Source/Defs/DefAssemblyBase.cs | 8 +- .../Source/Defs/TTypeTemplateCommonExtends.cs | 2 +- ...scriptBinUnderingDeserializeVisitorBase.cs | 25 +--- .../TypescriptBinUnderingSerializeVisitor.cs | 46 +----- ....cs => TypescriptDefineTypeNameVisitor.cs} | 12 +- src/Luban.Job.Db/Source/Defs/DefAssembly.cs | 1 + src/Luban.Job.Db/Source/Defs/DefField.cs | 2 + .../Source/Defs/TTypeTemplateExtends.cs | 47 +++--- .../Source/Generate/TypescriptRender.cs | 34 ++--- src/Luban.Job.Db/Source/JobController.cs | 10 +- .../CompatibleSerializeNeedEmbedVisitor.cs | 7 +- .../DbCsCompatibleDeserializeVisitor.cs | 34 ++++- .../DbCsCompatibleSerializeVisitor.cs | 113 ++++++++------ ...DbWriteBlob.cs => DbCsWriteBlobVisitor.cs} | 4 +- ...bTypescriptCompatibleDeserializeVisitor.cs | 67 +++++++++ .../DbTypescriptCompatibleSerializeVisitor.cs | 68 +++++++++ .../DbTypescriptDefineTypeNameVisitor.cs | 49 +++++++ .../DbTypescriptDeserializeFuncVisitor.cs | 138 ++++++++++++++++++ .../DbTypescriptInitFieldVisitor.cs | 138 ++++++++++++++++++ .../DbTypescriptSerializeFuncVisitor.cs | 138 ++++++++++++++++++ 22 files changed, 782 insertions(+), 171 deletions(-) rename src/Luban.Job.Common/Source/TypeVisitors/{TypescriptDefineTypeName.cs => TypescriptDefineTypeNameVisitor.cs} (88%) rename src/Luban.Job.Db/Source/TypeVisitors/{DbWriteBlob.cs => DbCsWriteBlobVisitor.cs} (75%) create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleDeserializeVisitor.cs create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleSerializeVisitor.cs create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDefineTypeNameVisitor.cs create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDeserializeFuncVisitor.cs create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptInitFieldVisitor.cs create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptSerializeFuncVisitor.cs diff --git a/src/Luban.Job.Cfg/Source/Defs/DefField.cs b/src/Luban.Job.Cfg/Source/Defs/DefField.cs index 3db94dd..818a48d 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefField.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefField.cs @@ -94,7 +94,7 @@ namespace Luban.Job.Cfg.Defs get { var table = Assembly.GetCfgTable(Ref.FirstTable); - return $"{TsRefVarName} : {table.ValueTType.Apply(TypescriptDefineTypeName.Ins)}"; + return $"{TsRefVarName} : {table.ValueTType.Apply(TypescriptDefineTypeNameVisitor.Ins)}"; } } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs index 22b0f96..fdb0296 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/TypescriptJsonUnderingConstructorVisitor.cs @@ -97,7 +97,7 @@ namespace Luban.Job.Cfg.TypeVisitors } else { - return $"{{ {fieldName} = []; for(let _ele of {jsonVarName}) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeName.Ins)}; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.push(_e);}}}}"; + return $"{{ {fieldName} = []; for(let _ele of {jsonVarName}) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.push(_e);}}}}"; } } @@ -109,7 +109,7 @@ namespace Luban.Job.Cfg.TypeVisitors } else { - return $"{{ {fieldName} = []; for(let _ele of {jsonVarName}) {{ let _e : {type.ElementType.Apply(TypescriptDefineTypeName.Ins)}; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.push(_e);}}}}"; + return $"{{ {fieldName} = []; for(let _ele of {jsonVarName}) {{ let _e : {type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.push(_e);}}}}"; } } @@ -121,13 +121,13 @@ namespace Luban.Job.Cfg.TypeVisitors } else { - return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(var _ele of {jsonVarName}) {{ let _e:{type.ElementType.Apply(TypescriptDefineTypeName.Ins)}; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.add(_e);}}}}"; + return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeNameVisitor.Ins)}(); for(var _ele of {jsonVarName}) {{ let _e:{type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.add(_e);}}}}"; } } public string Accept(TMap type, string jsonVarName, string fieldName) { - return $"{fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(var _entry_ of {jsonVarName}) {{ let _k:{type.KeyType.Apply(TypescriptDefineTypeName.Ins)}; {type.KeyType.Apply(this, "_entry_[0]", "_k")}; let _v:{type.ValueType.Apply(TypescriptDefineTypeName.Ins)}; {type.ValueType.Apply(this, "_entry_[1]", "_v")}; {fieldName}.set(_k, _v); }}"; + return $"{fieldName} = new {type.Apply(TypescriptDefineTypeNameVisitor.Ins)}(); for(var _entry_ of {jsonVarName}) {{ let _k:{type.KeyType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.KeyType.Apply(this, "_entry_[0]", "_k")}; let _v:{type.ValueType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.ValueType.Apply(this, "_entry_[1]", "_v")}; {fieldName}.set(_k, _v); }}"; } diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index dc08761..03ef09a 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -18,7 +18,9 @@ namespace Luban.Job.Common.Defs public string TopModule { get; protected set; } - public bool SupportDatetimeType { get; protected set; } + public bool SupportDatetimeType { get; protected set; } = false; + + public bool SupportNullable { get; protected set; } = true; public void AddType(DefTypeBase type) { @@ -112,6 +114,10 @@ namespace Luban.Job.Common.Defs bool nullable; if (type.EndsWith('?')) { + if (!SupportNullable) + { + throw new Exception($"not support nullable type:{module}.{type}"); + } nullable = true; type = type[0..^1]; } diff --git a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs index 09c2cad..45f604c 100644 --- a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs +++ b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs @@ -125,7 +125,7 @@ namespace Luban.Job.Common.Defs public static string TsDefineType(TType type) { - return type.Apply(TypescriptDefineTypeName.Ins); + return type.Apply(TypescriptDefineTypeNameVisitor.Ins); } public static string TsToString(string filedName, TType type) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs index b3b6619..e086dfe 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs @@ -76,16 +76,6 @@ namespace Luban.Job.Common.TypeVisitors } public abstract string Accept(TBean type, string bufVarName, string fieldName); - //{ - // if (type.Bean.IsAbstractType) - // { - // return $"{fieldName} = {type.Bean.FullName}.deserialize({bufVarName});"; - // } - // else - // { - // return $"{fieldName} = new {type.Bean.FullName}({bufVarName});"; - // } - //} private string GetNewArray(TArray arrayType, string size) { @@ -123,23 +113,22 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TArray type, string bufVarName, string fieldName) { - return $"{{ let n = Math.min({bufVarName}.ReadSize(), {bufVarName}.Size); {fieldName} = {GetNewArray(type, "n")}; for(let i = 0 ; i < n ; i++) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; { (IsRawArrayElementType(type.ElementType) ? $"{fieldName}[i] = _e" : $"{fieldName}.push(_e)") } }} }}"; + return $"{{ let n = Math.min({bufVarName}.ReadSize(), {bufVarName}.Size); {fieldName} = {GetNewArray(type, "n")}; for(let i = 0 ; i < n ; i++) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; { (IsRawArrayElementType(type.ElementType) ? $"{fieldName}[i] = _e" : $"{fieldName}.push(_e)") } }} }}"; } - public string Accept(TList type, string bufVarName, string fieldName) + public virtual string Accept(TList type, string bufVarName, string fieldName) { - return $"{{ {fieldName} = []; for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; {fieldName}.push(_e) }} }}"; + return $"{{ {fieldName} = []; for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _e :{type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; {fieldName}.push(_e) }} }}"; } - public string Accept(TSet type, string bufVarName, string fieldName) + public virtual string Accept(TSet type, string bufVarName, string fieldName) { - return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _e:{type.ElementType.Apply(TypescriptDefineTypeName.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; {fieldName}.add(_e);}}}}"; + return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeNameVisitor.Ins)}(); for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _e:{type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)};{type.ElementType.Apply(this, bufVarName, "_e")}; {fieldName}.add(_e);}}}}"; } - public string Accept(TMap type, string bufVarName, string fieldName) + public virtual string Accept(TMap type, string bufVarName, string fieldName) { - return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeName.Ins)}(); for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _k:{type.KeyType.Apply(TypescriptDefineTypeName.Ins)}; {type.KeyType.Apply(this, bufVarName, "_k")}; let _v:{type.ValueType.Apply(TypescriptDefineTypeName.Ins)}; {type.ValueType.Apply(this, bufVarName, "_v")}; {fieldName}.set(_k, _v); }} }}"; - + return $"{{ {fieldName} = new {type.Apply(TypescriptDefineTypeNameVisitor.Ins)}(); for(let i = 0, n = {bufVarName}.ReadSize() ; i < n ; i++) {{ let _k:{type.KeyType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.KeyType.Apply(this, bufVarName, "_k")}; let _v:{type.ValueType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.ValueType.Apply(this, bufVarName, "_v")}; {fieldName}.set(_k, _v); }} }}"; } public string Accept(TVector2 type, string bufVarName, string fieldName) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs index d84a537..e0028af 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs @@ -8,9 +8,9 @@ using System.Threading.Tasks; namespace Luban.Job.Common.TypeVisitors { - class TypescriptBinUnderingSerializeVisitor : ITypeFuncVisitor + public class TypescriptBinUnderingSerializeVisitor : ITypeFuncVisitor { - public static TypescriptBinUnderingSerializeVisitor Ins { get; } = new TypescriptBinUnderingSerializeVisitor(); + public static TypescriptBinUnderingSerializeVisitor Ins { get; } = new(); public string Accept(TBool type, string bufName, string fieldName) { @@ -102,7 +102,7 @@ namespace Luban.Job.Common.TypeVisitors return $"{bufVarName}.WriteInt({fieldName});"; } - public string Accept(TBean type, string bufVarName, string fieldName) + public virtual string Accept(TBean type, string bufVarName, string fieldName) { if (type.Bean.IsAbstractType) { @@ -114,56 +114,22 @@ namespace Luban.Job.Common.TypeVisitors } } - private static string GetNewArray(TArray arrayType, string size) - { - switch (arrayType.ElementType) - { - case TByte _: return $"new Uint8Array({size})"; - case TShort _: - case TFshort _: return $"new Int16Array({size})"; - case TInt _: - case TFint _: return $"new Int32Array({size})"; - case TLong _: - case TFlong _: return $"new Int64Array({size})"; - case TFloat _: return $"new Float32Array({size})"; - case TDouble _: return $"new Float64Array({size})"; - default: return "[]"; - } - } - - private static bool IsRawArrayElementType(TType elementType) - { - switch (elementType) - { - case TByte _: - case TShort _: - case TFshort _: - case TInt _: - case TFint _: - case TLong _: - case TFlong _: - case TFloat _: - case TDouble _: return true; - default: return false; - } - } - public string Accept(TArray type, string bufVarName, string fieldName) { return $"{{ {bufVarName}.WriteSize({fieldName}.length); for(let _e of {fieldName}) {{ {type.ElementType.Apply(this, bufVarName, "_e")} }} }}"; } - public string Accept(TList type, string bufVarName, string fieldName) + public virtual string Accept(TList type, string bufVarName, string fieldName) { return $"{{ {bufVarName}.WriteSize({fieldName}.length); for(let _e of {fieldName}) {{ {type.ElementType.Apply(this, bufVarName, "_e")} }} }}"; } - public string Accept(TSet type, string bufVarName, string fieldName) + public virtual string Accept(TSet type, string bufVarName, string fieldName) { return $"{{ {bufVarName}.WriteSize({fieldName}.size); for(let _e of {fieldName}) {{ {type.ElementType.Apply(this, bufVarName, "_e")} }} }}"; } - public string Accept(TMap type, string bufVarName, string fieldName) + public virtual string Accept(TMap type, string bufVarName, string fieldName) { return $"{{ {bufVarName}.WriteSize({fieldName}.size); for(let [_k, _v] of {fieldName}) {{ {type.KeyType.Apply(this, bufVarName, "_k")} {type.ValueType.Apply(this, bufVarName, "_v")} }} }}"; } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeNameVisitor.cs similarity index 88% rename from src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeName.cs rename to src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeNameVisitor.cs index d2e33c7..68462ad 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeNameVisitor.cs @@ -2,9 +2,9 @@ using Luban.Job.Common.Types; namespace Luban.Job.Common.TypeVisitors { - public class TypescriptDefineTypeName : ITypeFuncVisitor + public class TypescriptDefineTypeNameVisitor : ITypeFuncVisitor { - public static TypescriptDefineTypeName Ins { get; } = new TypescriptDefineTypeName(); + public static TypescriptDefineTypeNameVisitor Ins { get; } = new TypescriptDefineTypeNameVisitor(); public string Accept(TBool type) { @@ -99,22 +99,22 @@ namespace Luban.Job.Common.TypeVisitors } } - public string Accept(TArray type) + public virtual string Accept(TArray type) { return GetArrayType(type.ElementType); } - public string Accept(TList type) + public virtual string Accept(TList type) { return $"{type.ElementType.Apply(this)}[]"; } - public string Accept(TSet type) + public virtual string Accept(TSet type) { return $"Set<{type.ElementType.Apply(this)}>"; } - public string Accept(TMap type) + public virtual string Accept(TMap type) { return $"Map<{type.KeyType.Apply(this)}, {type.ValueType.Apply(this)}>"; } diff --git a/src/Luban.Job.Db/Source/Defs/DefAssembly.cs b/src/Luban.Job.Db/Source/Defs/DefAssembly.cs index af37af4..c4bfa24 100644 --- a/src/Luban.Job.Db/Source/Defs/DefAssembly.cs +++ b/src/Luban.Job.Db/Source/Defs/DefAssembly.cs @@ -30,6 +30,7 @@ namespace Luban.Job.Db.Defs public void Load(Defines defines, RemoteAgent agent) { + this.SupportNullable = false; this.Agent = agent; TopModule = defines.TopModule; diff --git a/src/Luban.Job.Db/Source/Defs/DefField.cs b/src/Luban.Job.Db/Source/Defs/DefField.cs index ea3908e..5f8e6d0 100644 --- a/src/Luban.Job.Db/Source/Defs/DefField.cs +++ b/src/Luban.Job.Db/Source/Defs/DefField.cs @@ -11,6 +11,8 @@ namespace Luban.Job.Db.Defs public string InternalName => "__" + Name; + public string InternalNameWithThis => "this." + "__" + Name; + public DefField(DefTypeBase host, Field f, int idOffset) : base(host, f, idOffset) { diff --git a/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs index 976f7a3..da759ee 100644 --- a/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs @@ -82,50 +82,41 @@ namespace Luban.Job.Db.Defs public static string DbTsDefineType(TType type) { - return type.Apply(TypescriptDefineTypeName.Ins); + return type.Apply(DbTypescriptDefineTypeNameVisitor.Ins); } public static string DbTsInitField(string fieldName, string logType, TType type) { - return "// ts init field";// type.Apply(DbCsInitFieldVisitor.Ins, fieldName, logType); + return type.Apply(DbTypescriptInitFieldVisitor.Ins, fieldName, logType); } public static string TsWriteBlob(string bufName, string fieldName, TType type) { - //return type.Apply(DbWriteBlob.Ins, bufName, valueName); - return "// ts write blob"; // DbCsCompatibleSerialize(bufName, fieldName, type); + return DbTsCompatibleSerialize(bufName, fieldName, type); } public static string DbTsCompatibleSerialize(string bufName, string fieldName, TType type) { - //if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) - //{ - // var sb = new StringBuilder($"{bufName}.BeginWriteSegment(out var _state_);"); - // sb.Append(type.Apply(DbCsCompatibleSerializeVisitor.Ins, bufName, fieldName)); - // sb.Append("_buf.EndWriteSegment(_state_);"); - // return sb.ToString(); - //} - //else - //{ - // return type.Apply(DbCsCompatibleSerializeVisitor.Ins, bufName, fieldName); - //} - return "/* db ts compatible serialize */"; + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return @$"{{let _state_ = {bufName}.BeginWriteSegment();{type.Apply(DbTypescriptCompatibleSerializeVisitor.Ins, bufName, fieldName)}; _buf.EndWriteSegment(_state_)}}"; + } + else + { + return type.Apply(DbTypescriptCompatibleSerializeVisitor.Ins, bufName, fieldName); + } } public static string DbTsCompatibleDeserialize(string bufName, string fieldName, TType type) { - //if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) - //{ - // var sb = new StringBuilder($"{bufName}.EnterSegment(out var _state_);"); - // sb.Append(type.Apply(DbCsCompatibleDeserializeVisitor.Ins, bufName, fieldName)); - // sb.Append("_buf.LeaveSegment(_state_);"); - // return sb.ToString(); - //} - //else - //{ - // return type.Apply(DbCsCompatibleDeserializeVisitor.Ins, bufName, fieldName); - //} - return "/* db ts compatible serialize */"; + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $@"{{ let _state_ = {bufName}.EnterSegment(); {type.Apply(DbTypescriptCompatibleDeserializeVisitor.Ins, bufName, fieldName)} {bufName}.LeaveSegment(_state_); }}"; + } + else + { + return type.Apply(DbTypescriptCompatibleDeserializeVisitor.Ins, bufName, fieldName); + } } } } diff --git a/src/Luban.Job.Db/Source/Generate/TypescriptRender.cs b/src/Luban.Job.Db/Source/Generate/TypescriptRender.cs index 42fd697..e5c132f 100644 --- a/src/Luban.Job.Db/Source/Generate/TypescriptRender.cs +++ b/src/Luban.Job.Db/Source/Generate/TypescriptRender.cs @@ -58,7 +58,7 @@ export {{x.ts_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x constructor() { super() {{~ for field in fields~}} - {{db_ts_init_field field.internal_name field.log_type field.ctype }} + {{db_ts_init_field field.internal_name_with_this field.log_type field.ctype }} {{~end~}} } @@ -66,8 +66,7 @@ export {{x.ts_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x {{ctype = field.ctype}} {{~if has_setter field.ctype~}} - private static {{field.log_type}} = class extends FieldLoggerGeneric2<{{name}}, {{db_ts_define_type field.ctype}}> - { + private static {{field.log_type}} = class extends FieldLoggerGeneric2<{{name}}, {{db_ts_define_type field.ctype}}> { constructor(self:{{name}}, value: {{db_ts_define_type field.ctype}}) { super(self, value) } get fieldId(): number { return this.host.getObjectId() + {{field.id}} } @@ -76,20 +75,21 @@ export {{x.ts_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x writeBlob(_buf: ByteBuf) { _buf.WriteInt(FieldTag.{{tag_name field.ctype}}); - {{ts_write_blob '_buf' 'this.Value' field.ctype}} + {{ts_write_blob '_buf' 'this.value' field.ctype}} } } get {{field.ts_style_name}}(): {{db_ts_define_type field.ctype}} { if (this.isManaged) { var txn = TransactionContext.current - if (txn == null) return this.{{field.internal_name}} + if (txn == null) return {{field.internal_name_with_this}} let log: any = txn.getField(this.getObjectId() + {{field.id}}) - return log != null ? log.value : this.{{field.internal_name}} + return log != null ? log.value : {{field.internal_name_with_this}} } else { - return this.{{field.internal_name}}; + return {{field.internal_name_with_this}}; } } + set {{field.ts_style_name}}(value: {{db_ts_define_type field.ctype}}) { {{~if db_field_cannot_null~}} if (value == null) throw new Error() @@ -97,20 +97,16 @@ export {{x.ts_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x if (this.isManaged) { let txn = TransactionContext.current txn.putFieldLong(this.getObjectId() + {{field.id}}, new {{name}}.{{field.log_type}}(this, value)) - {{~if field.ctype.need_set_children_root}} + {{~if field.ctype.need_set_children_root~}} value?.initRoot(this.getRoot()) - {{end}} + {{~end~}} } else { - this.{{field.internal_name}} = value + {{field.internal_name_with_this}} = value } } {{~else~}} - {{~if field.ctype.is_collection~}} - // collection logger - {{~end~}} - - get {{field.ts_style_name}}(): {{db_ts_define_type field.ctype}} { return this.{{field.internal_name}} } + get {{field.ts_style_name}}(): {{db_ts_define_type field.ctype}} { return {{field.internal_name_with_this}} } {{~end~}} {{~end~}} @@ -136,7 +132,7 @@ export {{x.ts_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x serialize(_buf: ByteBuf) { _buf.WriteNumberAsLong(this.getObjectId()) {{~ for field in hierarchy_fields~}} - { _buf.WriteInt(FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT)); {{db_ts_compatible_serialize '_buf' field.internal_name field.ctype}} } + { _buf.WriteInt(FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT)); {{db_ts_compatible_serialize '_buf' field.internal_name_with_this field.ctype}} } {{~end}} } @@ -146,7 +142,7 @@ export {{x.ts_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x let _tag_ = _buf.ReadInt() switch (_tag_) { {{~ for field in hierarchy_fields~}} - case FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT) : { {{db_ts_compatible_deserialize '_buf' field.internal_name field.ctype}} break; } + case FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT) : { {{db_ts_compatible_deserialize '_buf' field.internal_name_with_this field.ctype}} break; } {{~end~}} default: { _buf.SkipUnknownField(_tag_); break; } } @@ -159,7 +155,9 @@ export {{x.ts_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x initChildrenRoot(root: TKey) { {{~ for field in hierarchy_fields~}} - {{if need_set_children_root field.ctype}}// this.{{field.internal_name}}?.initRoot(root);{{end}} + {{~if need_set_children_root field.ctype~}} + {{field.internal_name_with_this}}?.initRoot(root) + {{~end~}} {{~end}} } diff --git a/src/Luban.Job.Db/Source/JobController.cs b/src/Luban.Job.Db/Source/JobController.cs index e4fc90b..c5a2983 100644 --- a/src/Luban.Job.Db/Source/JobController.cs +++ b/src/Luban.Job.Db/Source/JobController.cs @@ -146,8 +146,16 @@ namespace Luban.Job.Db fileContent.Add($"import TransactionContext from '{brightRequirePath}/transaction/TransactionContext'"); fileContent.Add($"import {{FieldTag}} from '{brightRequirePath}/serialization/FieldTag'"); fileContent.Add($"import TKey from '{brightRequirePath}/storage/TKey'"); + fileContent.Add($"import PList from '{brightRequirePath}/transaction/collections/PList'"); + fileContent.Add($"import PList1 from '{brightRequirePath}/transaction/collections/PList1'"); + fileContent.Add($"import PList2 from '{brightRequirePath}/transaction/collections/PList2'"); + fileContent.Add($"import PSet from '{brightRequirePath}/transaction/collections/PSet'"); + fileContent.Add($"import PMap from '{brightRequirePath}/transaction/collections/PMap'"); + fileContent.Add($"import PMap1 from '{brightRequirePath}/transaction/collections/PMap1'"); + fileContent.Add($"import PMap2 from '{brightRequirePath}/transaction/collections/PMap2'"); + fileContent.Add($"import SerializeFactory from '{brightRequirePath}/serialization/SerializeFactory'"); - fileContent.Add(@$"export namespace {ass.TopModule} {{"); + fileContent.Add($"export namespace {ass.TopModule} {{"); foreach (var type in exportTypes) diff --git a/src/Luban.Job.Db/Source/TypeVisitors/CompatibleSerializeNeedEmbedVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/CompatibleSerializeNeedEmbedVisitor.cs index 73d36df..439e23b 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/CompatibleSerializeNeedEmbedVisitor.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/CompatibleSerializeNeedEmbedVisitor.cs @@ -10,7 +10,12 @@ namespace Luban.Job.Db.TypeVisitors { class CompatibleSerializeNeedEmbedVisitor : AllFalseVisitor { - public static CompatibleSerializeNeedEmbedVisitor Ins { get; } = new CompatibleSerializeNeedEmbedVisitor(); + public static CompatibleSerializeNeedEmbedVisitor Ins { get; } = new(); + + public override bool Accept(TBean type) + { + return true; + } public override bool Accept(TArray type) { diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs index 5d3e063..f8ea08a 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs @@ -82,11 +82,11 @@ namespace Luban.Job.Db.TypeVisitors var bean = type.Bean; if (bean.IsNotAbstractType) { - return $"{bufName}.EnterSegment(out var _state2_); {fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(); {fieldName}.Deserialize({bufName}); {bufName}.LeaveSegment(_state2_);"; + return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(); {fieldName}.Deserialize({bufName});"; } else { - return $"{bufName}.EnterSegment(out var _state2_); {fieldName} = {bean.FullName}.Deserialize{bean.Name}({bufName}); {bufName}.LeaveSegment(_state2_);"; + return $"{fieldName} = {bean.FullName}.Deserialize{bean.Name}({bufName});"; } } @@ -95,19 +95,43 @@ namespace Luban.Job.Db.TypeVisitors throw new System.NotSupportedException(); } + private string BeginSegment(TType type, string bufName) + { + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"{bufName}.EnterSegment(out var _state2_);"; + } + else + { + return ""; + } + } + + private string EndSegment(TType type, string bufName) + { + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"{bufName}.LeaveSegment(_state2_);"; + } + else + { + return ""; + } + } + public string Accept(TList type, string bufName, string fieldName) { - return $"int _tagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_tagType == FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {fieldName}.Add(_e);}}"; + return $"int _tagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_tagType == FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {BeginSegment(type.ElementType, bufName)} {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {EndSegment(type.ElementType, bufName)} {fieldName}.Add(_e);}}"; } public string Accept(TSet type, string bufName, string fieldName) { - return $"int _tagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_tagType == FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {fieldName}.Add(_e);}}"; + return $"int _tagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_tagType == FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {BeginSegment(type.ElementType, bufName)} {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {EndSegment(type.ElementType, bufName)} {fieldName}.Add(_e);}}"; } public string Accept(TMap type, string bufName, string fieldName) { - return $"int _keyTagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_keyTagType == FieldTag.{type.KeyType.Apply(TagNameVisitor.Ins)}); int _valueTagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_valueTagType == FieldTag.{type.ValueType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {type.KeyType.Apply(DbCsDefineTypeVisitor.Ins)} _k; {type.KeyType.Apply(this, bufName, "_k")} {type.ValueType.Apply(DbCsDefineTypeVisitor.Ins)} _v; {type.ValueType.Apply(this, bufName, "_v")} {fieldName}.Add(_k, _v);}}"; + return $"int _keyTagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_keyTagType == FieldTag.{type.KeyType.Apply(TagNameVisitor.Ins)}); int _valueTagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_valueTagType == FieldTag.{type.ValueType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {type.KeyType.Apply(DbCsDefineTypeVisitor.Ins)} _k; {type.KeyType.Apply(this, bufName, "_k")} {BeginSegment(type.ValueType, bufName)} {type.ValueType.Apply(DbCsDefineTypeVisitor.Ins)} _v; {type.ValueType.Apply(this, bufName, "_v")} {EndSegment(type.ValueType, bufName)} {fieldName}.Add(_k, _v);}}"; } public string Accept(TVector2 type, string bufName, string fieldName) diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleSerializeVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleSerializeVisitor.cs index e94c309..cfd6a77 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleSerializeVisitor.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleSerializeVisitor.cs @@ -12,123 +12,146 @@ namespace Luban.Job.Db.TypeVisitors { public static DbCsCompatibleSerializeVisitor Ins { get; } = new DbCsCompatibleSerializeVisitor(); - public string Accept(TBool type, string byteBufName, string fieldName) + public string Accept(TBool type, string bufName, string fieldName) { - return $"{byteBufName}.WriteBool({fieldName});"; + return $"{bufName}.WriteBool({fieldName});"; } - public string Accept(TByte type, string byteBufName, string fieldName) + public string Accept(TByte type, string bufName, string fieldName) { - return $"{byteBufName}.WriteByte({fieldName});"; + return $"{bufName}.WriteByte({fieldName});"; } - public string Accept(TShort type, string byteBufName, string fieldName) + public string Accept(TShort type, string bufName, string fieldName) { - return $"{byteBufName}.WriteShort({fieldName});"; + return $"{bufName}.WriteShort({fieldName});"; } - public string Accept(TFshort type, string byteBufName, string fieldName) + public string Accept(TFshort type, string bufName, string fieldName) { - return $"{byteBufName}.WriteFshort({fieldName});"; + return $"{bufName}.WriteFshort({fieldName});"; } - public string Accept(TInt type, string byteBufName, string fieldName) + public string Accept(TInt type, string bufName, string fieldName) { - return $"{byteBufName}.WriteInt({fieldName});"; + return $"{bufName}.WriteInt({fieldName});"; } - public string Accept(TFint type, string byteBufName, string fieldName) + public string Accept(TFint type, string bufName, string fieldName) { - return $"{byteBufName}.WriteFint({fieldName});"; + return $"{bufName}.WriteFint({fieldName});"; } - public string Accept(TLong type, string byteBufName, string fieldName) + public string Accept(TLong type, string bufName, string fieldName) { - return $"{byteBufName}.WriteLong({fieldName});"; + return $"{bufName}.WriteLong({fieldName});"; } - public string Accept(TFlong type, string byteBufName, string fieldName) + public string Accept(TFlong type, string bufName, string fieldName) { - return $"{byteBufName}.WriteFlong({fieldName});"; + return $"{bufName}.WriteFlong({fieldName});"; } - public string Accept(TFloat type, string byteBufName, string fieldName) + public string Accept(TFloat type, string bufName, string fieldName) { - return $"{byteBufName}.WriteFloat({fieldName});"; + return $"{bufName}.WriteFloat({fieldName});"; } - public string Accept(TDouble type, string byteBufName, string fieldName) + public string Accept(TDouble type, string bufName, string fieldName) { - return $"{byteBufName}.WriteDouble({fieldName});"; + return $"{bufName}.WriteDouble({fieldName});"; } - public string Accept(TEnum type, string byteBufName, string fieldName) + public string Accept(TEnum type, string bufName, string fieldName) { - return $"{byteBufName}.WriteInt((int){fieldName});"; + return $"{bufName}.WriteInt((int){fieldName});"; } - public string Accept(TString type, string byteBufName, string fieldName) + public string Accept(TString type, string bufName, string fieldName) { - return $"{byteBufName}.WriteString({fieldName});"; + return $"{bufName}.WriteString({fieldName});"; } - public string Accept(TBytes type, string byteBufName, string fieldName) + public string Accept(TBytes type, string bufName, string fieldName) { - return $"{byteBufName}.WriteBytes({fieldName});"; + return $"{bufName}.WriteBytes({fieldName});"; } - public string Accept(TText type, string byteBufName, string fieldName) + public string Accept(TText type, string bufName, string fieldName) { - return $"{byteBufName}.WriteString({fieldName});"; + return $"{bufName}.WriteString({fieldName});"; } - public string Accept(TBean type, string byteBufName, string fieldName) + public string Accept(TBean type, string bufName, string fieldName) { var bean = type.Bean; if (bean.IsNotAbstractType) { - return $"{byteBufName}.BeginWriteSegment(out var _state2_); {fieldName}.Serialize({byteBufName}); {byteBufName}.EndWriteSegment(_state2_);"; + return $"{fieldName}.Serialize({bufName});"; } else { - return $"{byteBufName}.BeginWriteSegment(out var _state2_); {bean.FullName}.Serialize{bean.Name}({byteBufName}, {fieldName});{byteBufName}.EndWriteSegment(_state2_);"; + return $"{bean.FullName}.Serialize{bean.Name}({bufName}, {fieldName});"; } } - public string Accept(TArray type, string byteBufName, string fieldName) + public string Accept(TArray type, string bufName, string fieldName) { throw new NotSupportedException(); } - public string Accept(TList type, string byteBufName, string fieldName) + private string EnterSegment(TType type, string bufName) { - return $"{byteBufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, byteBufName, "_e")} }}"; + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"{bufName}.EnterSegment(out var _state2_);"; + } + else + { + return ""; + } } - public string Accept(TSet type, string byteBufName, string fieldName) + private string LeaveSegment(TType type, string bufName) { - return $"{byteBufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, byteBufName, "_e")} }}"; + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"{bufName}.LeaveSegment(_state2_);"; + } + else + { + return ""; + } } - public string Accept(TMap type, string byteBufName, string fieldName) + public string Accept(TList type, string bufName, string fieldName) { - return $"{byteBufName}.WriteInt(FieldTag.{type.KeyType.Apply(TagNameVisitor.Ins)}); {byteBufName}.WriteInt(FieldTag.{type.ValueType.Apply(TagNameVisitor.Ins)}); foreach((var _k, var _v) in {fieldName}) {{ {type.KeyType.Apply(this, byteBufName, "_k")} {type.ValueType.Apply(this, byteBufName, "_v")}}}"; - + return $"{bufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); foreach(var _e in {fieldName}) {{ {EnterSegment(type.ElementType, bufName)} {type.ElementType.Apply(this, bufName, "_e")} {LeaveSegment(type.ElementType, bufName)} }}"; } - public string Accept(TVector2 type, string byteBufName, string fieldName) + public string Accept(TSet type, string bufName, string fieldName) { - return $"{byteBufName}.WriteVector2({fieldName});"; + return $"{bufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, bufName, "_e")} }}"; } - public string Accept(TVector3 type, string byteBufName, string fieldName) + public string Accept(TMap type, string bufName, string fieldName) { - return $"{byteBufName}.WriteVector3({fieldName});"; + return $"{bufName}.WriteInt(FieldTag.{type.KeyType.Apply(TagNameVisitor.Ins)}); {bufName}.WriteInt(FieldTag.{type.ValueType.Apply(TagNameVisitor.Ins)}); foreach((var _k, var _v) in {fieldName}) {{ {type.KeyType.Apply(this, bufName, "_k")} {EnterSegment(type.ValueType, bufName)} {type.ValueType.Apply(this, bufName, "_v")} {LeaveSegment(type.ValueType, bufName)} }}"; } - public string Accept(TVector4 type, string byteBufName, string fieldName) + public string Accept(TVector2 type, string bufName, string fieldName) { - return $"{byteBufName}.WriteVector4({fieldName});"; + return $"{bufName}.WriteVector2({fieldName});"; + } + + public string Accept(TVector3 type, string bufName, string fieldName) + { + return $"{bufName}.WriteVector3({fieldName});"; + } + + public string Accept(TVector4 type, string bufName, string fieldName) + { + return $"{bufName}.WriteVector4({fieldName});"; } public string Accept(TDateTime type, string x, string y) diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbWriteBlob.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsWriteBlobVisitor.cs similarity index 75% rename from src/Luban.Job.Db/Source/TypeVisitors/DbWriteBlob.cs rename to src/Luban.Job.Db/Source/TypeVisitors/DbCsWriteBlobVisitor.cs index 8158b18..a4578d9 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbWriteBlob.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsWriteBlobVisitor.cs @@ -4,9 +4,9 @@ using System; namespace Luban.Job.Db.TypeVisitors { - class DbWriteBlob + class DbCsWriteBlobVisitor { - public static DbCsCompatibleSerializeVisitor Ins { get; } = new DbCsCompatibleSerializeVisitor(); + public static DbCsCompatibleSerializeVisitor Ins { get; } = new(); } } diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleDeserializeVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleDeserializeVisitor.cs new file mode 100644 index 0000000..3193a54 --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleDeserializeVisitor.cs @@ -0,0 +1,67 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Db.TypeVisitors +{ + class DbTypescriptCompatibleDeserializeVisitor : TypescriptBinUnderingDeserializeVisitorBase + { + public static DbTypescriptCompatibleDeserializeVisitor Ins { get; } = new(); + + public override string Accept(TBean type, string bufName, string fieldName) + { + if (type.Bean.IsAbstractType) + { + return $"{{ {fieldName} = {type.Bean.FullName}.deserialize{type.Bean.Name}Any({bufName}); }}"; + } + else + { + return $"{{ {fieldName} = new {type.Bean.FullName}(); {fieldName}.deserialize({bufName}); }}"; + } + } + + private string BeginSegment(TType type, string bufName) + { + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"let _state2_ = {bufName}.EnterSegment();"; + } + else + { + return ""; + } + } + + private string EndSegment(TType type, string bufName) + { + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"{bufName}.LeaveSegment(_state2_);"; + } + else + { + + return ""; + } + } + + public override string Accept(TList type, string bufName, string fieldName) + { + return $"{{ /*let _tagType_ = */{bufName}.ReadInt(); for(let i = 0, n = {bufName}.ReadSize() ; i < n ; i++) {{ {BeginSegment(type.ElementType, bufName)} let _e :{type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.ElementType.Apply(this, bufName, "_e")}; {EndSegment(type.ElementType, bufName)} {fieldName}.add(_e) }} }}"; + } + + public override string Accept(TSet type, string bufName, string fieldName) + { + return $"{{ /*let _tagType_ = */{bufName}.ReadInt(); for(let i = 0, n = {bufName}.ReadSize() ; i < n ; i++) {{ let _e:{type.ElementType.Apply(TypescriptDefineTypeNameVisitor.Ins)};{type.ElementType.Apply(this, bufName, "_e")}; {fieldName}.add(_e);}}}}"; + } + + public override string Accept(TMap type, string bufName, string fieldName) + { + return $"{{ /*let _keyTagType_ = */{bufName}.ReadInt(); let _valueTagType_ = {bufName}.ReadInt(); for(let i = 0, n = {bufName}.ReadSize() ; i < n ; i++) {{ let _k:{type.KeyType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.KeyType.Apply(this, bufName, "_k")}; {BeginSegment(type.ValueType, bufName)} let _v:{type.ValueType.Apply(TypescriptDefineTypeNameVisitor.Ins)}; {type.ValueType.Apply(this, bufName, "_v")}; {EndSegment(type.ValueType, bufName)} {fieldName}.set(_k, _v); }} }}"; + } + } +} diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleSerializeVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleSerializeVisitor.cs new file mode 100644 index 0000000..dbbff86 --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptCompatibleSerializeVisitor.cs @@ -0,0 +1,68 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Db.TypeVisitors +{ + class DbTypescriptCompatibleSerializeVisitor : TypescriptBinUnderingSerializeVisitor + { + public static new DbTypescriptCompatibleSerializeVisitor Ins { get; } = new(); + + public override string Accept(TBean type, string bufName, string fieldName) + { + var bean = type.Bean; + if (bean.IsNotAbstractType) + { + return $"{{{fieldName}.serialize({bufName});}}"; + } + else + { + return $"{{{bean.FullName}.serialize{bean.Name}Any({bufName}, {fieldName});}}"; + } + } + + private string BeginSegment(TType type, string bufName) + { + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"let _state2_ = {bufName}.BeginWriteSegment();"; + } + else + { + return ""; + } + } + + private string EndSegment(TType type, string bufName) + { + if (type.Apply(CompatibleSerializeNeedEmbedVisitor.Ins)) + { + return $"{bufName}.EndWriteSegment(_state2_);"; + } + else + { + + return ""; + } + } + + public override string Accept(TList type, string bufName, string fieldName) + { + return $"{{ {bufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); {bufName}.WriteSize({fieldName}.length); for(let _e of {fieldName}) {{ {BeginSegment(type.ElementType, bufName)} {type.ElementType.Apply(this, bufName, "_e")} {EndSegment(type.ElementType, bufName)} }} }}"; + } + + public override string Accept(TSet type, string bufName, string fieldName) + { + return $"{{ {bufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); {bufName}.WriteSize({fieldName}.length); for(let _e of {fieldName}) {{ {type.ElementType.Apply(this, bufName, "_e")} }} }}"; + } + + public override string Accept(TMap type, string bufName, string fieldName) + { + return $"{{ {bufName}.WriteInt(FieldTag.{type.KeyType.Apply(TagNameVisitor.Ins)}); {bufName}.WriteInt(FieldTag.{type.ValueType.Apply(TagNameVisitor.Ins)}); {bufName}.WriteSize({fieldName}.length); for(let [_k, _v] of {fieldName}.entries()) {{ {type.KeyType.Apply(this, bufName, "_k")} {BeginSegment(type.ValueType, bufName)} {type.ValueType.Apply(this, bufName, "_v")} {EndSegment(type.ValueType, bufName)} }} }}"; + } + } +} diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDefineTypeNameVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDefineTypeNameVisitor.cs new file mode 100644 index 0000000..93a9c15 --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDefineTypeNameVisitor.cs @@ -0,0 +1,49 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Db.TypeVisitors +{ + class DbTypescriptDefineTypeNameVisitor : TypescriptDefineTypeNameVisitor + { + public new static DbTypescriptDefineTypeNameVisitor Ins { get; } = new(); + + public override string Accept(TArray type) + { + throw new NotSupportedException(); + } + + public override string Accept(TList type) + { + if (type.ElementType.IsBean) + { + return $"PList2<{type.ElementType.Apply(this)}>"; + } + else + { + return $"PList1<{type.ElementType.Apply(this)}>"; + } + } + + public override string Accept(TSet type) + { + return $"PSet<{type.ElementType.Apply(this)}>"; + } + + public override string Accept(TMap type) + { + if (type.ValueType.IsBean) + { + return $"PMap2<{type.KeyType.Apply(this)}, {type.ValueType.Apply(this)}>"; + } + else + { + return $"PMap1<{type.KeyType.Apply(this)}, {type.ValueType.Apply(this)}>"; + } + } + } +} diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDeserializeFuncVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDeserializeFuncVisitor.cs new file mode 100644 index 0000000..ad66f5b --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptDeserializeFuncVisitor.cs @@ -0,0 +1,138 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Db.TypeVisitors +{ + class DbTypescriptDeserializeFuncVisitor : ITypeFuncVisitor + { + public static DbTypescriptDeserializeFuncVisitor Ins { get; } = new(); + + public string Accept(TBool type) + { + return "SerializeFactory.deserializeBool"; + } + + public string Accept(TByte type) + { + return "SerializeFactory.deserializeByte"; + } + + public string Accept(TShort type) + { + return "SerializeFactory.deserializeShort"; + } + + public string Accept(TFshort type) + { + return "SerializeFactory.deserializeFshort"; + } + + public string Accept(TInt type) + { + return "SerializeFactory.deserializeInt"; + } + + public string Accept(TFint type) + { + return "SerializeFactory.deserializeFint"; + } + + public string Accept(TLong type) + { + return "SerializeFactory.deserializeLong"; + } + + public string Accept(TFlong type) + { + return "SerializeFactory.deserializeFlong"; + } + + public string Accept(TFloat type) + { + return "SerializeFactory.deserializeFloat"; + } + + public string Accept(TDouble type) + { + return "SerializeFactory.deserializeDouble"; + } + + public string Accept(TEnum type) + { + return "SerializeFactory.deserializeInt"; + } + + public string Accept(TString type) + { + return "SerializeFactory.deserializeString"; + } + + public string Accept(TBytes type) + { + throw new NotImplementedException(); + } + + public string Accept(TText type) + { + throw new NotImplementedException(); + } + + public string Accept(TBean type) + { + var typeName = type.Apply(DbTypescriptDefineTypeNameVisitor.Ins); + if (type.IsDynamic) + { + return $"{type.Apply(DbTypescriptDefineTypeNameVisitor.Ins)}.deserialize{type.Bean.Name}Any"; + } + else + { + return $"(_buf: ByteBuf):{typeName} => {{ var x = new {typeName}(); x.deserialize(_buf); return x }}"; + } + } + + public string Accept(TArray type) + { + throw new NotImplementedException(); + } + + public string Accept(TList type) + { + throw new NotImplementedException(); + } + + public string Accept(TSet type) + { + throw new NotImplementedException(); + } + + public string Accept(TMap type) + { + throw new NotImplementedException(); + } + + public string Accept(TVector2 type) + { + return "SerializeFactory.deserializeVector2"; + } + + public string Accept(TVector3 type) + { + return "SerializeFactory.deserializeVector3"; + } + + public string Accept(TVector4 type) + { + return "SerializeFactory.deserializeVector4"; + } + + public string Accept(TDateTime type) + { + return "SerializeFactory.deserializeInt"; + } + } +} diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptInitFieldVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptInitFieldVisitor.cs new file mode 100644 index 0000000..2e793b1 --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptInitFieldVisitor.cs @@ -0,0 +1,138 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; + +namespace Luban.Job.Db.TypeVisitors +{ + class DbTypescriptInitFieldVisitor : ITypeFuncVisitor + { + public static DbTypescriptInitFieldVisitor Ins { get; } = new(); + + public string Accept(TBool type, string fieldName, string logType) + { + return $"{fieldName} = false"; + } + + public string Accept(TByte type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TShort type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TFshort type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TInt type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TFint type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TLong type, string fieldName, string logType) + { + return $"{fieldName} = {(type.IsBigInt ? "BigInt(0)" : "0")}"; + } + + public string Accept(TFlong type, string fieldName, string logType) + { + return $"{fieldName} = BigInt(0)"; + } + + public string Accept(TFloat type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TDouble type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TEnum type, string fieldName, string logType) + { + return $"{fieldName} = 0"; + } + + public string Accept(TString type, string fieldName, string logType) + { + return $"{fieldName} = \"\""; + } + + public string Accept(TBytes type, string fieldName, string logType) + { + return $"{fieldName} = new Uint8Array()"; + } + + public string Accept(TText type, string fieldName, string logType) + { + throw new NotSupportedException(); + } + + public string Accept(TBean type, string fieldName, string logType) + { + if (type.Bean.IsAbstractType) + { + return $"{fieldName} = null"; + } + else + { + return $"{fieldName} = new {type.Apply(DbTypescriptDefineTypeNameVisitor.Ins)}()"; + } + } + + public string Accept(TArray type, string fieldName, string logType) + { + throw new NotSupportedException(); + } + + public string Accept(TList type, string fieldName, string logType) + { + var elementType = type.ElementType; + return $"{fieldName} = new {type.Apply(DbTypescriptDefineTypeNameVisitor.Ins)}(FieldTag.{elementType.Apply(TagNameVisitor.Ins)}, {elementType.Apply(DbTypescriptSerializeFuncVisitor.Ins)}, {elementType.Apply(DbTypescriptDeserializeFuncVisitor.Ins)})"; + } + + public string Accept(TSet type, string fieldName, string logType) + { + var elementType = type.ElementType; + return $"{fieldName} = new {type.Apply(DbTypescriptDefineTypeNameVisitor.Ins)}(FieldTag.{elementType.Apply(TagNameVisitor.Ins)}, {elementType.Apply(DbTypescriptSerializeFuncVisitor.Ins)}, {elementType.Apply(DbTypescriptDeserializeFuncVisitor.Ins)})"; + } + + public string Accept(TMap type, string fieldName, string logType) + { + var keyType = type.KeyType; + var valueType = type.ValueType; + return $"{fieldName} = new {type.Apply(DbTypescriptDefineTypeNameVisitor.Ins)}(FieldTag.{keyType.Apply(TagNameVisitor.Ins)}, FieldTag.{valueType.Apply(TagNameVisitor.Ins)}, {keyType.Apply(DbTypescriptSerializeFuncVisitor.Ins)}, {keyType.Apply(DbTypescriptDeserializeFuncVisitor.Ins)}, {valueType.Apply(DbTypescriptSerializeFuncVisitor.Ins)}, {valueType.Apply(DbTypescriptDeserializeFuncVisitor.Ins)})"; + + } + + public string Accept(TVector2 type, string fieldName, string logType) + { + return $"{fieldName} = new Vector2()"; + } + + public string Accept(TVector3 type, string fieldName, string logType) + { + return $"{fieldName} = new Vector3()"; + } + + public string Accept(TVector4 type, string fieldName, string logType) + { + return $"{fieldName} = new Vector4()"; + } + + public string Accept(TDateTime type, string x, string y) + { + throw new NotSupportedException(); + } + } +} diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptSerializeFuncVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptSerializeFuncVisitor.cs new file mode 100644 index 0000000..6ead739 --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbTypescriptSerializeFuncVisitor.cs @@ -0,0 +1,138 @@ +using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Db.TypeVisitors +{ + class DbTypescriptSerializeFuncVisitor : ITypeFuncVisitor + { + public static DbTypescriptSerializeFuncVisitor Ins { get; } = new(); + + public string Accept(TBool type) + { + return "SerializeFactory.serializeBool"; + } + + public string Accept(TByte type) + { + return "SerializeFactory.serializeByte"; + } + + public string Accept(TShort type) + { + return "SerializeFactory.serializeShort"; + } + + public string Accept(TFshort type) + { + return "SerializeFactory.serializeFshort"; + } + + public string Accept(TInt type) + { + return "SerializeFactory.serializeInt"; + } + + public string Accept(TFint type) + { + return "SerializeFactory.serializeFint"; + } + + public string Accept(TLong type) + { + return "SerializeFactory.serializeLong"; + } + + public string Accept(TFlong type) + { + return "SerializeFactory.serializeFlong"; + } + + public string Accept(TFloat type) + { + return "SerializeFactory.serializeFloat"; + } + + public string Accept(TDouble type) + { + return "SerializeFactory.serializeDouble"; + } + + public string Accept(TEnum type) + { + return "SerializeFactory.serializeInt"; + } + + public string Accept(TString type) + { + return "SerializeFactory.serializeString"; + } + + public string Accept(TBytes type) + { + throw new NotImplementedException(); + } + + public string Accept(TText type) + { + throw new NotImplementedException(); + } + + public string Accept(TBean type) + { + var typeName = type.Apply(DbTypescriptDefineTypeNameVisitor.Ins); + if (type.IsDynamic) + { + return $"{typeName}.serialize{type.Bean.Name}Any"; + } + else + { + return "SerializeFactory.serializeBean"; + } + } + + public string Accept(TArray type) + { + throw new NotImplementedException(); + } + + public string Accept(TList type) + { + throw new NotImplementedException(); + } + + public string Accept(TSet type) + { + throw new NotImplementedException(); + } + + public string Accept(TMap type) + { + throw new NotImplementedException(); + } + + public string Accept(TVector2 type) + { + return "SerializeFactory.serializeVector2"; + } + + public string Accept(TVector3 type) + { + return "SerializeFactory.serializeVector3"; + } + + public string Accept(TVector4 type) + { + return "SerializeFactory.serializeVector4"; + } + + public string Accept(TDateTime type) + { + return "SerializeFactory.serializeInt"; + } + } +}