Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Aug 4, 2020
1 parent f14500b commit 953521a
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions example/csharp/Example.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/csharp/Example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ProtoPlus\ProtoPlus.csproj" />
<ProjectReference Include="..\..\..\api\csharp\ProtoPlus\ProtoPlus.csproj" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions example/csharp/Example/ProtoGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Collections.Generic;
using ProtoPlus;

namespace proto
namespace Proto
{

public enum MyEnum
Expand Down Expand Up @@ -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 = "",
});
Expand Down
27 changes: 25 additions & 2 deletions example/csharp/Example/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyTypeMini>();

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];
Expand All @@ -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");
}
Expand All @@ -146,6 +168,7 @@ static void Main(string[] args)
{
TestFull();
TestMessage();
TestSkipField();
}
}

Expand Down
21 changes: 19 additions & 2 deletions gen/csharp/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions gen/csharp/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 10 additions & 15 deletions parser/parse_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions tests/Make.sh
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions tests/code_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"encoding/json"
"github.com/davyxu/protoplus/proto"
"github.com/davyxu/protoplus/wire"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 953521a

Please sign in to comment.