From 953521acb4114c55e7bb047424309f375e24fb70 Mon Sep 17 00:00:00 2001 From: davyxu Date: Tue, 4 Aug 2020 15:29:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +-- .../csharp/ProtoPlus/FastBitConverter.cs | 0 .../csharp/ProtoPlus/InputStream.cs | 0 .../csharp/ProtoPlus/MessageMeta.cs | 0 .../csharp/ProtoPlus/OutputStream.cs | 0 .../csharp/ProtoPlus/ProtoPlus.csproj | 0 .../csharp/ProtoPlus/ProtoStruct.cs | 0 .../csharp/ProtoPlus/SizeCaculator.cs | 0 {example => api}/csharp/ProtoPlus/WireType.cs | 0 example/csharp/Example.sln | 10 +++---- example/csharp/Example/Example.csproj | 2 +- example/csharp/Example/ProtoGen.cs | 6 ++--- example/csharp/Example/Test.cs | 27 +++++++++++++++++-- gen/csharp/func.go | 21 +++++++++++++-- gen/csharp/text.go | 9 +++---- parser/parse_descriptor.go | 25 +++++++---------- tests/Make.sh | 7 +---- tests/code_test.go | 26 ++++++++++++++++++ 18 files changed, 96 insertions(+), 41 deletions(-) rename {example => api}/csharp/ProtoPlus/FastBitConverter.cs (100%) rename {example => api}/csharp/ProtoPlus/InputStream.cs (100%) rename {example => api}/csharp/ProtoPlus/MessageMeta.cs (100%) rename {example => api}/csharp/ProtoPlus/OutputStream.cs (100%) rename {example => api}/csharp/ProtoPlus/ProtoPlus.csproj (100%) rename {example => api}/csharp/ProtoPlus/ProtoStruct.cs (100%) rename {example => api}/csharp/ProtoPlus/SizeCaculator.cs (100%) rename {example => api}/csharp/ProtoPlus/WireType.cs (100%) diff --git a/.gitignore b/.gitignore index ec4359b..2dbb55a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ example/csharp/.vs example/csharp/Example/bin example/csharp/Example/obj -example/csharp/ProtoPlus/obj -example/csharp/ProtoPlus/bin +api/csharp/ProtoPlus/obj +api/csharp/ProtoPlus/bin diff --git a/example/csharp/ProtoPlus/FastBitConverter.cs b/api/csharp/ProtoPlus/FastBitConverter.cs similarity index 100% rename from example/csharp/ProtoPlus/FastBitConverter.cs rename to api/csharp/ProtoPlus/FastBitConverter.cs diff --git a/example/csharp/ProtoPlus/InputStream.cs b/api/csharp/ProtoPlus/InputStream.cs similarity index 100% rename from example/csharp/ProtoPlus/InputStream.cs rename to api/csharp/ProtoPlus/InputStream.cs diff --git a/example/csharp/ProtoPlus/MessageMeta.cs b/api/csharp/ProtoPlus/MessageMeta.cs similarity index 100% rename from example/csharp/ProtoPlus/MessageMeta.cs rename to api/csharp/ProtoPlus/MessageMeta.cs diff --git a/example/csharp/ProtoPlus/OutputStream.cs b/api/csharp/ProtoPlus/OutputStream.cs similarity index 100% rename from example/csharp/ProtoPlus/OutputStream.cs rename to api/csharp/ProtoPlus/OutputStream.cs diff --git a/example/csharp/ProtoPlus/ProtoPlus.csproj b/api/csharp/ProtoPlus/ProtoPlus.csproj similarity index 100% rename from example/csharp/ProtoPlus/ProtoPlus.csproj rename to api/csharp/ProtoPlus/ProtoPlus.csproj diff --git a/example/csharp/ProtoPlus/ProtoStruct.cs b/api/csharp/ProtoPlus/ProtoStruct.cs similarity index 100% rename from example/csharp/ProtoPlus/ProtoStruct.cs rename to api/csharp/ProtoPlus/ProtoStruct.cs diff --git a/example/csharp/ProtoPlus/SizeCaculator.cs b/api/csharp/ProtoPlus/SizeCaculator.cs similarity index 100% rename from example/csharp/ProtoPlus/SizeCaculator.cs rename to api/csharp/ProtoPlus/SizeCaculator.cs diff --git a/example/csharp/ProtoPlus/WireType.cs b/api/csharp/ProtoPlus/WireType.cs similarity index 100% rename from example/csharp/ProtoPlus/WireType.cs rename to api/csharp/ProtoPlus/WireType.cs diff --git a/example/csharp/Example.sln b/example/csharp/Example.sln index fc95afb..53b8293 100644 --- a/example/csharp/Example.sln +++ b/example/csharp/Example.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.28010.2046 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example", "Example\Example.csproj", "{660CB175-1256-4A1C-B76C-08B45E9F0159}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProtoPlus", "ProtoPlus\ProtoPlus.csproj", "{59F81F0A-5130-4882-90A3-100358537F2E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProtoPlus", "..\..\api\csharp\ProtoPlus\ProtoPlus.csproj", "{1E171D88-E6CB-4493-98D1-5C7A36CA422B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +17,10 @@ Global {660CB175-1256-4A1C-B76C-08B45E9F0159}.Debug|Any CPU.Build.0 = Debug|Any CPU {660CB175-1256-4A1C-B76C-08B45E9F0159}.Release|Any CPU.ActiveCfg = Release|Any CPU {660CB175-1256-4A1C-B76C-08B45E9F0159}.Release|Any CPU.Build.0 = Release|Any CPU - {59F81F0A-5130-4882-90A3-100358537F2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {59F81F0A-5130-4882-90A3-100358537F2E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {59F81F0A-5130-4882-90A3-100358537F2E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {59F81F0A-5130-4882-90A3-100358537F2E}.Release|Any CPU.Build.0 = Release|Any CPU + {1E171D88-E6CB-4493-98D1-5C7A36CA422B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1E171D88-E6CB-4493-98D1-5C7A36CA422B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1E171D88-E6CB-4493-98D1-5C7A36CA422B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1E171D88-E6CB-4493-98D1-5C7A36CA422B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/example/csharp/Example/Example.csproj b/example/csharp/Example/Example.csproj index 86932a4..3fb65b0 100644 --- a/example/csharp/Example/Example.csproj +++ b/example/csharp/Example/Example.csproj @@ -16,7 +16,7 @@ - + diff --git a/example/csharp/Example/ProtoGen.cs b/example/csharp/Example/ProtoGen.cs index 18f473f..5ecbca3 100644 --- a/example/csharp/Example/ProtoGen.cs +++ b/example/csharp/Example/ProtoGen.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using ProtoPlus; -namespace proto +namespace Proto { public enum MyEnum @@ -496,14 +496,14 @@ public static void RegisterGeneratedMeta(MessageMeta meta) meta.RegisterMeta(new MetaInfo { Type = typeof(MySubType), - ID = 33606, + ID = 16166, SourcePeer = "client", TargetPeer = "game", }); meta.RegisterMeta(new MetaInfo { Type = typeof(MyType), - ID = 9980, + ID = 28380, SourcePeer = "", TargetPeer = "", }); diff --git a/example/csharp/Example/Test.cs b/example/csharp/Example/Test.cs index 99bded8..483e3b2 100644 --- a/example/csharp/Example/Test.cs +++ b/example/csharp/Example/Test.cs @@ -110,6 +110,28 @@ static MyType MakeMyType() }; } + static void TestSkipField() + { + byte[] data = new byte[256]; + var s = new OutputStream(data); + + var myType = MakeMyType(); + + s.Marshal(myType); + + var s2 = new InputStream(); + s2.Init(data, 0, s.Position); + + var myType2 = InputStream.CreateStruct(); + + s2.Unmarshal(myType2); + + Debug.Assert(myType2.Bool == myType2.Bool && myType2.Float32 == myType.Float32 && + myType2.Int32 == myType.Int32 && myType2.Int64 == myType.Int64 + && myType2.UInt64 == myType.UInt64 && myType2.UInt32 == myType.UInt32 + && myType2.Str == myType.Str); + } + static void TestFull() { byte[] data = new byte[256]; @@ -133,11 +155,11 @@ static void TestMessage() { var mm = new MessageMeta(); MessageMetaRegister.RegisterGeneratedMeta(mm); - var msg = mm.CreateMessageByID(33606); + var msg = mm.CreateMessageByID(28380); var meta = mm.GetMetaByType(msg.GetType()); - Debug.Assert(meta.ID == 33606); + Debug.Assert(meta.ID == 28380); Debug.Assert(meta.SourcePeer == "client"); } @@ -146,6 +168,7 @@ static void Main(string[] args) { TestFull(); TestMessage(); + TestSkipField(); } } diff --git a/gen/csharp/func.go b/gen/csharp/func.go index fa978ad..c992d26 100644 --- a/gen/csharp/func.go +++ b/gen/csharp/func.go @@ -42,11 +42,28 @@ func init() { return CSTypeNameFull(fd) } - UsefulFunc["IsPrimitiveSlice"] = func(raw interface{}) bool { + // 原生类型/结构体 | 数组 | 导出方式 + // 原生类型 无初始化 + // 原生类型 是 new Type[]() + // 结构体 MessageMeta.NewStruct() + // 结构体 是 new Type[]() + + UsefulFunc["NoneStructCanInit"] = func(raw interface{}) bool { + + fd := raw.(*model.FieldDescriptor) + + if fd.Kind == model.Kind_Primitive && fd.Type == "string" { + return false + } + + return !fd.TagExists("NoInit") && fd.Repeatd + } + + UsefulFunc["StructCanInit"] = func(raw interface{}) bool { fd := raw.(*model.FieldDescriptor) - return fd.Repeatd && fd.Kind != model.Kind_Struct + return !fd.TagExists("NoInit") && !fd.Repeatd && fd.Kind == model.Kind_Struct } UsefulFunc["IsStructSlice"] = func(raw interface{}) bool { diff --git a/gen/csharp/text.go b/gen/csharp/text.go index 4d38196..1b5aec5 100644 --- a/gen/csharp/text.go +++ b/gen/csharp/text.go @@ -23,15 +23,14 @@ namespace {{.PackageName}} {{end}} #region Serialize Code public void Init( ) - { {{range .Fields}}{{if IsPrimitiveSlice .}} - {{.Name}} = new {{CSTypeNameFull .}}(); {{end}}{{end}} - {{range .Fields}}{{if IsStruct .}} - {{.Name}} = ({{CSTypeNameFull .}}) InputStream.CreateStruct(typeof({{CSTypeNameFull .}})); {{end}} {{end}} + { {{range .Fields}}{{if NoneStructCanInit . }} + {{.Name}} = new {{CSTypeNameFull .}}(); {{end}}{{end}}{{range .Fields}}{{if StructCanInit .}} + {{.Name}} = ({{CSTypeNameFull .}}) MessageMeta.NewStruct(typeof({{CSTypeNameFull .}})); {{end}} {{end}} } public void Marshal(OutputStream stream) { {{range .Fields}} - stream.Write{{CodecName .}}({{PbTagNumber $obj .}}, {{.Name}} ); {{end}} + stream.Write{{CodecName .}}({{PbTagNumber $obj .}}, {{.Name}}); {{end}} } public int GetSize() diff --git a/parser/parse_descriptor.go b/parser/parse_descriptor.go index 5678631..b4c5242 100644 --- a/parser/parse_descriptor.go +++ b/parser/parse_descriptor.go @@ -23,30 +23,25 @@ func parseObject(ctx *Context) { for ctx.TokenID() != Token_CurlyBraceR { switch ctx.Descriptor.Kind { - case model.Kind_Struct, model.Kind_Enum: + case model.Kind_Struct: var fd model.FieldDescriptor ctx.FieldDescriptor = &fd - if ctx.TokenID() == Token_BracketL { - fd.TagSet = parseTagSet(ctx) - } - - case model.Kind_Service: - var sc model.ServiceCall - ctx.ServiceCall = &sc - if ctx.TokenID() == Token_BracketL { - sc.TagSet = parseTagSet(ctx) - } - } - - switch ctx.Descriptor.Kind { - case model.Kind_Struct: parseStructField(ctx) case model.Kind_Enum: + var fd model.FieldDescriptor + ctx.FieldDescriptor = &fd parseEnumField(ctx) case model.Kind_Service: + var sc model.ServiceCall + ctx.ServiceCall = &sc parseSvcCallField(ctx) } + // 读取字段后面的[Tag项] + if ctx.TokenID() == Token_BracketL { + ctx.FieldDescriptor.TagSet = parseTagSet(ctx) + } + } ctx.Expect(Token_CurlyBraceR) diff --git a/tests/Make.sh b/tests/Make.sh index 861bb26..accbf94 100644 --- a/tests/Make.sh +++ b/tests/Make.sh @@ -1,12 +1,7 @@ #!/usr/bin/env bash -CURRDIR=`pwd` -cd ../../../../.. -export GOPATH=`pwd` -cd ${CURRDIR} - go build -v -o=${GOPATH}/bin/protoplus github.com/davyxu/protoplus/cmd/protoplus # code.proto为输入文件 ${GOPATH}/bin/protoplus -go_out=code_gen.go -package=tests code.proto -${GOPATH}/bin/protoplus -cs_out=../example/csharp/Example/ProtoGen.cs -genreg -package=proto code.proto +${GOPATH}/bin/protoplus -cs_out=../example/csharp/Example/ProtoGen.cs -genreg -package=Proto code.proto ${GOPATH}/bin/protoplus -pb_out=pb_gen.proto -package=proto code.proto \ No newline at end of file diff --git a/tests/code_test.go b/tests/code_test.go index 4c8c444..1bfc5d0 100644 --- a/tests/code_test.go +++ b/tests/code_test.go @@ -1,6 +1,7 @@ package tests import ( + "encoding/json" "github.com/davyxu/protoplus/proto" "github.com/davyxu/protoplus/wire" "github.com/stretchr/testify/assert" @@ -105,6 +106,31 @@ func TestIntSlice(t *testing.T) { verifyWire(t, &input) } +func TestSkipField(t *testing.T) { + + input := makeMyType() + + data, err := proto.Marshal(&input) + assert.Equal(t, err, nil) + + jsondata, _ := json.Marshal(&input) + + var mini MyTypeMini + assert.Equal(t, proto.Unmarshal(data, &mini), nil) + + var miniJson MyTypeMini + json.Unmarshal(jsondata, &miniJson) + assert.Equal(t, miniJson, mini) +} + +func TestPtrField(t *testing.T) { + + input := MyType{} + data, err := proto.Marshal(&input) + t.Log(data, err) + +} + func TestText(t *testing.T) { input := makeMyType()