From d776373ee8ff1498f025b21f133cc9c55e610069 Mon Sep 17 00:00:00 2001 From: walon Date: Fri, 23 Jul 2021 11:21:14 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91=E8=B0=83?= =?UTF-8?q?=E6=95=B4db=20cs=E5=85=B3=E4=BA=8E=E5=AE=B9=E5=99=A8=E4=B8=8E?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=B9=E9=9D=A2=E7=9A=84=E7=94=9F=E6=88=90?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/Defs/TTypeTemplateExtends.cs | 4 +- .../Source/Generate/SyncCsRender.cs | 68 +++------ .../DbCsCompatibleDeserializeVisitor.cs | 6 +- .../DbCsCompatibleSerializeVisitor.cs | 6 +- .../TypeVisitors/DbCsDefineTypeVisitor.cs | 2 +- .../DbCsDeserializeFuncVisitor.cs | 138 ++++++++++++++++++ .../TypeVisitors/DbCsInitFieldVisitor.cs | 60 ++++---- .../TypeVisitors/DbCsSerializeFuncVisitor.cs | 138 ++++++++++++++++++ 8 files changed, 341 insertions(+), 81 deletions(-) create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbCsDeserializeFuncVisitor.cs create mode 100644 src/Luban.Job.Db/Source/TypeVisitors/DbCsSerializeFuncVisitor.cs diff --git a/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs index da759ee..33be112 100644 --- a/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Db/Source/Defs/TTypeTemplateExtends.cs @@ -23,9 +23,9 @@ namespace Luban.Job.Db.Defs return type.Apply(ImmutableTypeName.Ins); } - public static string DbCsInitField(string fieldName, string logType, TType type) + public static string DbCsInitField(string fieldName, TType type) { - return type.Apply(DbCsInitFieldVisitor.Ins, fieldName, logType); + return type.Apply(DbCsInitFieldVisitor.Ins, fieldName); } public static bool HasSetter(TType type) diff --git a/src/Luban.Job.Db/Source/Generate/SyncCsRender.cs b/src/Luban.Job.Db/Source/Generate/SyncCsRender.cs index a32ea2c..660f3c0 100644 --- a/src/Luban.Job.Db/Source/Generate/SyncCsRender.cs +++ b/src/Luban.Job.Db/Source/Generate/SyncCsRender.cs @@ -53,44 +53,45 @@ namespace {{x.namespace_with_top_module}} public interface {{readonly_name}} {{if parent_def_type}}: IReadOnly{{x.parent_def_type.name}} {{end}} { {{~ for field in fields~}} - {{db_cs_readonly_define_type field.ctype}} {{field.cs_style_name}} {get;} + {{db_cs_readonly_define_type field.ctype}} {{field.cs_style_name}} {get;} {{~end~}} } -public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Transaction.TxnBeanBase {{end}}, {{readonly_name}} , Bright.Transaction.IUnsafeBean +public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Transaction.TxnBeanBase {{end}}, {{readonly_name}} { {{~ for field in fields~}} - {{if is_abstract_type}}protected{{else}}private{{end}} {{db_cs_define_type field.ctype}} {{field.internal_name}}; + {{if is_abstract_type}}protected{{else}}private{{end}} {{db_cs_define_type field.ctype}} {{field.internal_name}}; {{~end}} public {{name}}() { {{~ for field in fields~}} - {{if cs_need_init field.ctype}}{{db_cs_init_field field.internal_name field.log_type field.ctype }} {{end}} + {{if cs_need_init field.ctype}}{{db_cs_init_field field.internal_name field.ctype}} {{end}} {{~end~}} } {{~ for field in fields~}} {{ctype = field.ctype}} - {{~if has_setter field.ctype~}} + {{~if has_setter ctype~}} - private sealed class {{field.log_type}} : Bright.Transaction.FieldLogger<{{name}}, {{db_cs_define_type field.ctype}}> + private sealed class {{field.log_type}} : Bright.Transaction.FieldLogger<{{name}}, {{db_cs_define_type ctype}}> { - public {{field.log_type}}({{name}} self, {{db_cs_define_type field.ctype}} value) : base(self, value) { } + public {{field.log_type}}({{name}} self, {{db_cs_define_type ctype}} value) : base(self, value) { } - public override long FieldId => host._objectId_ + {{field.id}}; + public override long FieldId => this._host.GetObjectId() + {{field.id}}; - public override void Commit() { this.host.{{field.internal_name}} = this.Value; } + public override int TagId => FieldTag.{{tag_name ctype}}; + + public override void Commit() { this._host.{{field.internal_name}} = this.Value; } public override void WriteBlob(ByteBuf _buf) { - _buf.WriteInt(FieldTag.{{tag_name field.ctype}}); - {{cs_write_blob '_buf' 'this.Value' field.ctype}} + {{cs_write_blob '_buf' 'this.Value' ctype}} } } - public {{db_cs_define_type field.ctype}} {{field.cs_style_name}} + public {{db_cs_define_type ctype}} {{field.cs_style_name}} { get { @@ -98,7 +99,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren { var txn = Bright.Transaction.TransactionContext.ThreadStaticCtx; if (txn == null) return {{field.internal_name}}; - var log = ({{field.log_type}})txn.GetField(_objectId_ + {{field.id}}); + var log = ({{field.log_type}})txn.GetField(this.GetObjectId() + {{field.id}}); return log != null ? log.Value : {{field.internal_name}}; } else @@ -114,8 +115,8 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren if (this.IsManaged) { var txn = Bright.Transaction.TransactionContext.ThreadStaticCtx; - txn.PutField(_objectId_ + {{field.id}}, new {{field.log_type}}(this, value)); - {{~if field.ctype.need_set_children_root}} + txn.PutField(this.GetObjectId() + {{field.id}}, new {{field.log_type}}(this, value)); + {{~if ctype.need_set_children_root}} value?.InitRoot(GetRoot()); {{end}} } @@ -126,30 +127,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren } } {{~else~}} - {{~if field.ctype.is_collection~}} - private class {{field.log_type}} : {{db_cs_define_type field.ctype}}.Log - { - private readonly {{name}} host; - public {{field.log_type}}({{name}} host, {{cs_immutable_type field.ctype}} value) : base(value) { this.host = host; } - - public override long FieldId => host._objectId_ + {{field.id}}; - - public override Bright.Transaction.TxnBeanBase Host => host; - - public override void Commit() - { - Commit(host.{{field.internal_name}}); - } - - public override void WriteBlob(ByteBuf _buf) - { - _buf.WriteInt(FieldTag.{{tag_name field.ctype}}); - {{cs_write_blob '_buf' 'this.Value' field.ctype}} - } - } - {{~end~}} - - public {{db_cs_define_type field.ctype}} {{field.cs_style_name}} => {{field.internal_name}}; + public {{db_cs_define_type ctype}} {{field.cs_style_name}} => {{field.internal_name}}; {{~end~}} {{~if ctype.bean || ctype.element_type ~}} @@ -184,7 +162,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren {{~else~}} public override void Serialize(ByteBuf _buf) { - _buf.WriteLong(_objectId_); + _buf.WriteLong(this.GetObjectId()); {{~ for field in hierarchy_fields~}} { _buf.WriteInt(FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT)); {{db_cs_compatible_serialize '_buf' field.internal_name field.ctype}} } {{~end}} @@ -192,7 +170,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren public override void Deserialize(ByteBuf _buf) { - _objectId_ = _buf.ReadLong(); + this.SetObjectId(_buf.ReadLong()); while(_buf.NotEmpty) { int _tag_ = _buf.ReadInt(); @@ -210,11 +188,13 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren public override int GetTypeId() => ID; {{~end~}} - void Bright.Transaction.IUnsafeBean.InitChildrenRoot(Bright.Storage.TKey root) + protected override void InitChildrenRoot(Bright.Storage.TKey root) { {{~ for field in hierarchy_fields~}} - {{if need_set_children_root field.ctype}}((Bright.Transaction.IUnsafeBean)({{field.internal_name}}))?.InitRoot(root);{{end}} - {{~end}} + {{~if need_set_children_root field.ctype~}} + UnsafeUtil.InitRoot({{field.internal_name}}, root); + {{~end~}} + {{~end~}} } public override string ToString() diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs index f8ea08a..58f417d 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleDeserializeVisitor.cs @@ -121,17 +121,17 @@ namespace Luban.Job.Db.TypeVisitors 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) {{ {BeginSegment(type.ElementType, bufName)} {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {EndSegment(type.ElementType, bufName)} {fieldName}.Add(_e);}}"; + return $"{fieldName}.Deserialize({bufName});"; } 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) {{ {BeginSegment(type.ElementType, bufName)} {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {EndSegment(type.ElementType, bufName)} {fieldName}.Add(_e);}}"; + return $"{fieldName}.Deserialize({bufName});"; } 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")} {BeginSegment(type.ValueType, bufName)} {type.ValueType.Apply(DbCsDefineTypeVisitor.Ins)} _v; {type.ValueType.Apply(this, bufName, "_v")} {EndSegment(type.ValueType, bufName)} {fieldName}.Add(_k, _v);}}"; + return $"{fieldName}.Deserialize({bufName});"; } 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 cfd6a77..4138ef2 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleSerializeVisitor.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsCompatibleSerializeVisitor.cs @@ -126,17 +126,17 @@ namespace Luban.Job.Db.TypeVisitors public string Accept(TList type, string bufName, string fieldName) { - 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)} }}"; + return $"{fieldName}.Serialize({bufName});"; } public string Accept(TSet type, string bufName, string fieldName) { - return $"{bufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, bufName, "_e")} }}"; + return $"{fieldName}.Serialize({bufName});"; } public 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)}); 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)} }}"; + return $"{fieldName}.Serialize({bufName});"; } public string Accept(TVector2 type, string bufName, string fieldName) diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbCsDefineTypeVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsDefineTypeVisitor.cs index e943ba0..1d64f59 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbCsDefineTypeVisitor.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsDefineTypeVisitor.cs @@ -20,7 +20,7 @@ namespace Luban.Job.Db.TypeVisitors public override string Accept(TSet type) { - return $"Bright.Transaction.Collections.PSet1<{type.ElementType.Apply(this)}>"; + return $"Bright.Transaction.Collections.PSet<{type.ElementType.Apply(this)}>"; } public override string Accept(TMap type) diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbCsDeserializeFuncVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsDeserializeFuncVisitor.cs new file mode 100644 index 0000000..74572d9 --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsDeserializeFuncVisitor.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 DbCsDeserializeFuncVisitor : ITypeFuncVisitor + { + public static DbCsDeserializeFuncVisitor Ins { get; } = new(); + + public string Accept(TBool type) + { + return "Bright.Common.SerializationUtil.DeserializeBool"; + } + + public string Accept(TByte type) + { + return "Bright.Common.SerializationUtil.DeserializeByte"; + } + + public string Accept(TShort type) + { + return "Bright.Common.SerializationUtil.DeserializeShort"; + } + + public string Accept(TFshort type) + { + return "Bright.Common.SerializationUtil.DeserializeFshort"; + } + + public string Accept(TInt type) + { + return "Bright.Common.SerializationUtil.DeserializeInt"; + } + + public string Accept(TFint type) + { + return "Bright.Common.SerializationUtil.DeserializeFint"; + } + + public string Accept(TLong type) + { + return "Bright.Common.SerializationUtil.DeserializeLong"; + } + + public string Accept(TFlong type) + { + return "Bright.Common.SerializationUtil.DeserializeFlong"; + } + + public string Accept(TFloat type) + { + return "Bright.Common.SerializationUtil.DeserializeFloat"; + } + + public string Accept(TDouble type) + { + return "Bright.Common.SerializationUtil.DeserializeDouble"; + } + + public string Accept(TEnum type) + { + return "Bright.Common.SerializationUtil.DeserializeInt"; + } + + public string Accept(TString type) + { + return "Bright.Common.SerializationUtil.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 $"{typeName}.Deserialize{type.Bean.Name}"; + } + else + { + return $"Bright.Common.SerializationUtil.DeserializeBean<{typeName}>"; + } + } + + 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 "Bright.Common.SerializationUtil.DeserializeVector2"; + } + + public string Accept(TVector3 type) + { + return "Bright.Common.SerializationUtil.DeserializeVector3"; + } + + public string Accept(TVector4 type) + { + return "Bright.Common.SerializationUtil.DeserializeVector4"; + } + + public string Accept(TDateTime type) + { + return "Bright.Common.SerializationUtil.DeserializeInt"; + } + } +} diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbCsInitFieldVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsInitFieldVisitor.cs index ba4461b..fb952ed 100644 --- a/src/Luban.Job.Db/Source/TypeVisitors/DbCsInitFieldVisitor.cs +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsInitFieldVisitor.cs @@ -4,81 +4,81 @@ using System; namespace Luban.Job.Db.TypeVisitors { - class DbCsInitFieldVisitor : ITypeFuncVisitor + class DbCsInitFieldVisitor : ITypeFuncVisitor { - public static DbCsInitFieldVisitor Ins { get; } = new DbCsInitFieldVisitor(); + public static DbCsInitFieldVisitor Ins { get; } = new(); - public string Accept(TBool type, string fieldName, string logType) + public string Accept(TBool type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TByte type, string fieldName, string logType) + public string Accept(TByte type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TShort type, string fieldName, string logType) + public string Accept(TShort type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TFshort type, string fieldName, string logType) + public string Accept(TFshort type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TInt type, string fieldName, string logType) + public string Accept(TInt type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TFint type, string fieldName, string logType) + public string Accept(TFint type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TLong type, string fieldName, string logType) + public string Accept(TLong type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TFlong type, string fieldName, string logType) + public string Accept(TFlong type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TFloat type, string fieldName, string logType) + public string Accept(TFloat type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TDouble type, string fieldName, string logType) + public string Accept(TDouble type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TEnum type, string fieldName, string logType) + public string Accept(TEnum type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TString type, string fieldName, string logType) + public string Accept(TString type, string fieldName) { return $"{fieldName} = \"\";"; } - public string Accept(TBytes type, string fieldName, string logType) + public string Accept(TBytes type, string fieldName) { return $"{fieldName} = System.Array.Empty();"; } - public string Accept(TText type, string fieldName, string logType) + public string Accept(TText type, string fieldName) { throw new NotSupportedException(); } - public string Accept(TBean type, string fieldName, string logType) + public string Accept(TBean type, string fieldName) { if (type.Bean.IsAbstractType) { @@ -90,42 +90,46 @@ namespace Luban.Job.Db.TypeVisitors } } - public string Accept(TArray type, string fieldName, string logType) + public string Accept(TArray type, string fieldName) { throw new NotSupportedException(); } - public string Accept(TList type, string fieldName, string logType) + public string Accept(TList type, string fieldName) { - return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(_v => new {logType}(this, _v));"; + var elementType = type.ElementType; + return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(FieldTag.{elementType.Apply(TagNameVisitor.Ins)}, {elementType.Apply(DbCsSerializeFuncVisitor.Ins)}, {elementType.Apply(DbCsDeserializeFuncVisitor.Ins)});"; } - public string Accept(TSet type, string fieldName, string logType) + public string Accept(TSet type, string fieldName) { - return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(_v => new {logType}(this, _v));"; + var elementType = type.ElementType; + return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(FieldTag.{elementType.Apply(TagNameVisitor.Ins)}, {elementType.Apply(DbCsSerializeFuncVisitor.Ins)}, {elementType.Apply(DbCsDeserializeFuncVisitor.Ins)});"; } - public string Accept(TMap type, string fieldName, string logType) + public string Accept(TMap type, string fieldName) { - return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(_v => new {logType}(this, _v));"; + var keyType = type.KeyType; + var valueType = type.ValueType; + return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(FieldTag.{keyType.Apply(TagNameVisitor.Ins)}, FieldTag.{valueType.Apply(TagNameVisitor.Ins)}, {keyType.Apply(DbCsSerializeFuncVisitor.Ins)}, {keyType.Apply(DbCsDeserializeFuncVisitor.Ins)}, {valueType.Apply(DbCsSerializeFuncVisitor.Ins)}, {valueType.Apply(DbCsDeserializeFuncVisitor.Ins)});"; } - public string Accept(TVector2 type, string fieldName, string logType) + public string Accept(TVector2 type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TVector3 type, string fieldName, string logType) + public string Accept(TVector3 type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TVector4 type, string fieldName, string logType) + public string Accept(TVector4 type, string fieldName) { return $"{fieldName} = default;"; } - public string Accept(TDateTime type, string x, string y) + public string Accept(TDateTime type, string fieldName) { throw new NotSupportedException(); } diff --git a/src/Luban.Job.Db/Source/TypeVisitors/DbCsSerializeFuncVisitor.cs b/src/Luban.Job.Db/Source/TypeVisitors/DbCsSerializeFuncVisitor.cs new file mode 100644 index 0000000..040b105 --- /dev/null +++ b/src/Luban.Job.Db/Source/TypeVisitors/DbCsSerializeFuncVisitor.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 DbCsSerializeFuncVisitor : ITypeFuncVisitor + { + public static DbCsSerializeFuncVisitor Ins { get; } = new(); + + public string Accept(TBool type) + { + return "Bright.Common.SerializationUtil.SerializeBool"; + } + + public string Accept(TByte type) + { + return "Bright.Common.SerializationUtil.SerializeByte"; + } + + public string Accept(TShort type) + { + return "Bright.Common.SerializationUtil.SerializeShort"; + } + + public string Accept(TFshort type) + { + return "Bright.Common.SerializationUtil.SerializeFshort"; + } + + public string Accept(TInt type) + { + return "Bright.Common.SerializationUtil.SerializeInt"; + } + + public string Accept(TFint type) + { + return "Bright.Common.SerializationUtil.SerializeFint"; + } + + public string Accept(TLong type) + { + return "Bright.Common.SerializationUtil.SerializeLong"; + } + + public string Accept(TFlong type) + { + return "Bright.Common.SerializationUtil.SerializeFlong"; + } + + public string Accept(TFloat type) + { + return "Bright.Common.SerializationUtil.SerializeFloat"; + } + + public string Accept(TDouble type) + { + return "Bright.Common.SerializationUtil.SerializeDouble"; + } + + public string Accept(TEnum type) + { + return "Bright.Common.SerializationUtil.SerializeInt"; + } + + public string Accept(TString type) + { + return "Bright.Common.SerializationUtil.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}"; + } + else + { + return $"Bright.Common.SerializationUtil.SerializeBean<{typeName}>"; + } + } + + 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 "Bright.Common.SerializationUtil.SerializeVector2"; + } + + public string Accept(TVector3 type) + { + return "Bright.Common.SerializationUtil.SerializeVector3"; + } + + public string Accept(TVector4 type) + { + return "Bright.Common.SerializationUtil.SerializeVector4"; + } + + public string Accept(TDateTime type) + { + return "Bright.Common.SerializationUtil.SerializeInt"; + } + } +}