From bf3cae5d1a61f64775d9020dd9136cd6bc704765 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 30 Jun 2021 12:01:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=B9=E6=80=A7=E3=80=91proto=20?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84typescript=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=8C=85=E5=90=ABconstructor=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=9E=84=E9=80=A0=E4=BA=86=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/Defs/DefAssemblyBase.cs | 1 + .../Source/Defs/TTypeTemplateCommonExtends.cs | 5 + .../NeedMarshalBoolPrefixVisitor.cs | 14 -- .../TypescriptBinDeserializeVisitor.cs | 5 - .../TypescriptBinSerializeVisitor.cs | 5 - ...scriptBinUnderingDeserializeVisitorBase.cs | 2 +- .../TypescriptBinUnderingSerializeVisitor.cs | 2 +- .../TypescriptCtorValueVisitor.cs | 125 ++++++++++++++++++ .../TypeVisitors/TypescriptDefineTypeName.cs | 2 +- src/Luban.Job.Common/Source/Types/TLong.cs | 13 +- .../Source/Generate/TypescriptRender.cs | 15 +++ 11 files changed, 159 insertions(+), 30 deletions(-) delete mode 100644 src/Luban.Job.Common/Source/TypeVisitors/NeedMarshalBoolPrefixVisitor.cs create mode 100644 src/Luban.Job.Common/Source/TypeVisitors/TypescriptCtorValueVisitor.cs diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index 88d508d..dc08761 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -128,6 +128,7 @@ namespace Luban.Job.Common.Defs case "int": return nullable ? TInt.NullableIns : TInt.Ins; case "fint": return nullable ? TFint.NullableIns : TFint.Ins; case "long": return nullable ? TLong.NullableIns : TLong.Ins; + case "bigint": return nullable ? TLong.NullableBigIns : TLong.BigIns; case "flong": return nullable ? TFlong.NullableIns : TFlong.Ins; case "float": return nullable ? TFloat.NullableIns : TFloat.Ins; case "double": return nullable ? TDouble.NullableIns : TDouble.Ins; diff --git a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs index af1490e..d141f9c 100644 --- a/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs +++ b/src/Luban.Job.Common/Source/Defs/TTypeTemplateCommonExtends.cs @@ -133,6 +133,11 @@ namespace Luban.Job.Common.Defs return $"{filedName}"; } + public static string TsCtorDefaultValue(TType type) + { + return type.Apply(TypescriptCtorValueVisitor.Ins); + } + public static string TsConstValue(TType type, string value) { return type.Apply(LuaConstValueVisitor.Ins, value); diff --git a/src/Luban.Job.Common/Source/TypeVisitors/NeedMarshalBoolPrefixVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/NeedMarshalBoolPrefixVisitor.cs deleted file mode 100644 index e7bc370..0000000 --- a/src/Luban.Job.Common/Source/TypeVisitors/NeedMarshalBoolPrefixVisitor.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Luban.Job.Common.Types; - -namespace Luban.Job.Common.TypeVisitors -{ - //public class NeedMarshalBoolPrefixVisitor : DecoratorFuncVisitor - //{ - // //public static NeedMarshalBoolPrefixVisitor Ins { get; } = new NeedMarshalBoolPrefixVisitor(); - - // public override bool DoAccept(TType type) - // { - // return type.IsNullable && !(type is TBean bean && bean.IsDynamic); - // } - //} -} diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinDeserializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinDeserializeVisitor.cs index dc99e43..e514fb4 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinDeserializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinDeserializeVisitor.cs @@ -18,10 +18,5 @@ namespace Luban.Job.Common.TypeVisitors return type.Apply(TypescriptBinUnderingDeserializeVisitor.Ins, byteBufName, fieldName); } } - - public override string Accept(TBean type, string bufName, string fieldName) - { - return type.Apply(TypescriptBinUnderingDeserializeVisitor.Ins, bufName, fieldName); - } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinSerializeVisitor.cs index ef1e3b7..9c3dc59 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinSerializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinSerializeVisitor.cs @@ -22,10 +22,5 @@ namespace Luban.Job.Common.TypeVisitors return type.Apply(TypescriptBinUnderingSerializeVisitor.Ins, bytebufName, fieldName); } } - - public override string Accept(TBean type, string bufName, string fieldName) - { - return type.Apply(TypescriptBinUnderingSerializeVisitor.Ins, bufName, fieldName); - } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs index dc92fef..d8ffa24 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingDeserializeVisitorBase.cs @@ -37,7 +37,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TLong type, string bufName, string fieldName) { - return $"{fieldName} = {bufName}.ReadLong();"; + return $"{fieldName} = {bufName}.{(type.IsBigInt ? "ReadLong" : "ReadLongAsNumber")}();"; } public string Accept(TFlong type, string bufName, string fieldName) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs index 4281ffe..403663c 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptBinUnderingSerializeVisitor.cs @@ -44,7 +44,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TLong type, string bufName, string fieldName) { - return $"{bufName}.WriteLong({fieldName});"; + return $"{bufName}.{(type.IsBigInt ? "WriteLong" : "WriteNumberAsLong")}({fieldName});"; } public string Accept(TFlong type, string bufName, string fieldName) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptCtorValueVisitor.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptCtorValueVisitor.cs new file mode 100644 index 0000000..b4bf22c --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptCtorValueVisitor.cs @@ -0,0 +1,125 @@ +using Luban.Job.Common.Types; +using System; + +namespace Luban.Job.Common.TypeVisitors +{ + public class TypescriptCtorValueVisitor : ITypeFuncVisitor + { + public static TypescriptCtorValueVisitor Ins { get; } = new(); + + public string Accept(TBool type) + { + return "false"; + } + + public string Accept(TByte type) + { + return "0"; + } + + public string Accept(TShort type) + { + return "0"; + } + + public string Accept(TFshort type) + { + return "0"; + } + + public string Accept(TInt type) + { + return "0"; + } + + public string Accept(TFint type) + { + return "0"; + } + + public string Accept(TLong type) + { + return "0"; + } + + public string Accept(TFlong type) + { + return "0"; + } + + public string Accept(TFloat type) + { + return "0"; + } + + public string Accept(TDouble type) + { + return "0"; + } + + public string Accept(TEnum type) + { + return "0"; + } + + public string Accept(TString type) + { + return "\"\""; + } + + public string Accept(TBytes type) + { + return "null"; + } + + public string Accept(TText type) + { + throw new NotImplementedException(); + } + + public string Accept(TBean type) + { + return type.IsDynamic ? "null" : $"new {type.Bean.FullName}()"; + } + + public string Accept(TArray type) + { + return "[]"; + } + + public string Accept(TList type) + { + return "[]"; + } + + public string Accept(TSet type) + { + return "new Set()"; + } + + public string Accept(TMap type) + { + return "new Map()"; + } + + public string Accept(TVector2 type) + { + return "new Vector2(0,0)"; + } + + public string Accept(TVector3 type) + { + return "new Vector3(0,0,0)"; + } + + public string Accept(TVector4 type) + { + return "new Vector4(0,0,0,0)"; + } + + public string Accept(TDateTime type) + { + return "0"; + } + } +} diff --git a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeName.cs index 53a1e1e..d2e33c7 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/TypescriptDefineTypeName.cs @@ -38,7 +38,7 @@ namespace Luban.Job.Common.TypeVisitors public string Accept(TLong type) { - return "bigint"; + return type.IsBigInt ? "bigint" : "number"; } public string Accept(TFlong type) diff --git a/src/Luban.Job.Common/Source/Types/TLong.cs b/src/Luban.Job.Common/Source/Types/TLong.cs index e204ebc..8771ce9 100644 --- a/src/Luban.Job.Common/Source/Types/TLong.cs +++ b/src/Luban.Job.Common/Source/Types/TLong.cs @@ -4,12 +4,19 @@ namespace Luban.Job.Common.Types { public class TLong : TType { - public static TLong Ins { get; } = new TLong(false); + public static TLong Ins { get; } = new TLong(false, false); - public static TLong NullableIns { get; } = new TLong(true); + public static TLong NullableIns { get; } = new TLong(true, false); - public TLong(bool isNullable) : base(isNullable) + public static TLong BigIns { get; } = new TLong(false, true); + + public static TLong NullableBigIns { get; } = new TLong(true, true); + + public bool IsBigInt { get; } + + public TLong(bool isNullable, bool isBigInt) : base(isNullable) { + IsBigInt = isBigInt; } public override bool TryParseFrom(string s) diff --git a/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs b/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs index 184a21c..b6a46e9 100644 --- a/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs +++ b/src/Luban.Job.Proto/Source/Generate/TypescriptRender.cs @@ -89,6 +89,14 @@ export {{if x.is_abstract_type}} abstract {{end}} class {{name}} extends {{if pa {{field.ts_style_name}}{{if field.is_nullable}}?{{end}} : {{ts_define_type field.ctype}} {{~end~}} + constructor() { + super() + {{~ for field in fields ~}} + this.{{field.ts_style_name}} = {{ts_ctor_default_value field.ctype}} + {{~end~}} + } + + serialize(_buf_ : Bright.Serialization.ByteBuf) { {{~if parent_def_type~}} super.serialize(_buf_) @@ -144,6 +152,13 @@ export class {{name}} extends Protocol { {{field.ts_style_name}}{{if field.is_nullable}}?{{end}} : {{ts_define_type field.ctype}} {{~end~}} + constructor() { + super() + {{~ for field in fields ~}} + this.{{field.ts_style_name}} = {{ts_ctor_default_value field.ctype}} + {{~end~}} + } + serialize(_buf_ : Bright.Serialization.ByteBuf) { {{~ for field in fields ~}} {{ts_bin_serialize ('this.' + field.ts_style_name) '_buf_' field.ctype}}