diff --git a/cmd/protoplus/main.go b/cmd/protoplus/main.go index c8a2d59..9b8cfce 100644 --- a/cmd/protoplus/main.go +++ b/cmd/protoplus/main.go @@ -38,9 +38,10 @@ type GenEntry struct { var ( genEntryList = []*GenEntry{ // 单文件生成 - {name: "ppgo_out", usage: "output protoplus message serialize golang source file", outfile: ppgo.GenGo}, + //{name: "ppgo_out", usage: "output protoplus message serialize golang source file", outfile: ppgo.GenGo}, + //{name: "ppcs_out", usage: "output protoplus message serialize csharp source file", outfile: ppcs.GenCS}, + {name: "ppgoreg_out", usage: "output protoplus message register entry in golang", outfile: ppgo.GenGoReg}, - {name: "ppcs_out", usage: "output protoplus message serialize csharp source file", outfile: ppcs.GenCS}, {name: "ppcsreg_out", usage: "output protoplus message register entry in csharp", outfile: ppcs.GenCSReg}, {name: "pbscheme_out", usage: "output google protobuf schema file as single file", outfile: pbscheme.GenProto}, diff --git a/example/csharp/Example/ProtoGen.cs b/example/csharp/Example/ProtoGen.cs index f73a018..8a3975f 100644 --- a/example/csharp/Example/ProtoGen.cs +++ b/example/csharp/Example/ProtoGen.cs @@ -8,416 +8,7 @@ namespace Proto { - public enum MyEnum - { - - Zero = 0, - One = 1, - Two = 2, - } - - - public partial class MyTypeMini : IProtoStruct - { - public bool Bool; - public int Int32; - public uint UInt32; - public long Int64; - public ulong UInt64; - public float Float32; - public double Float64; - public string Str; - - #region Serialize Code - public void Init( ) - { - } - - public void Marshal(OutputStream stream) - { - stream.WriteBool(1, Bool); - stream.WriteInt32(2, Int32); - stream.WriteUInt32(3, UInt32); - stream.WriteInt64(4, Int64); - stream.WriteUInt64(5, UInt64); - stream.WriteFloat(6, Float32); - stream.WriteDouble(7, Float64); - stream.WriteString(8, Str); - } - - public int GetSize() - { - int size = 0; - size += OutputStream.SizeBool(1, Bool); - size += OutputStream.SizeInt32(2, Int32); - size += OutputStream.SizeUInt32(3, UInt32); - size += OutputStream.SizeInt64(4, Int64); - size += OutputStream.SizeUInt64(5, UInt64); - size += OutputStream.SizeFloat(6, Float32); - size += OutputStream.SizeDouble(7, Float64); - size += OutputStream.SizeString(8, Str); - return size; - } - - public bool Unmarshal(InputStream stream, int fieldNumber, WireFormat.WireType wt) - { - switch (fieldNumber) - { - case 1: - stream.ReadBool(wt, ref Bool); - break; - case 2: - stream.ReadInt32(wt, ref Int32); - break; - case 3: - stream.ReadUInt32(wt, ref UInt32); - break; - case 4: - stream.ReadInt64(wt, ref Int64); - break; - case 5: - stream.ReadUInt64(wt, ref UInt64); - break; - case 6: - stream.ReadFloat(wt, ref Float32); - break; - case 7: - stream.ReadDouble(wt, ref Float64); - break; - case 8: - stream.ReadString(wt, ref Str); - break; - default: - return true; - } - - return false; - } - #endregion - } - - - public partial class MySubType : IProtoStruct - { - public bool Bool; - public int Int32; - public uint UInt32; - public long Int64; - public ulong UInt64; - public float Float32; - public double Float64; - public string Str; - public byte[] BytesSlice; - public List BoolSlice; - public List Int32Slice; - public List UInt32Slice; - public List Int64Slice; - public List UInt64Slice; - public List Float32Slice; - public List Float64Slice; - public List StrSlice; - public MyEnum Enum; - public List EnumSlice; - - #region Serialize Code - public void Init( ) - { - BoolSlice = new List(); - Int32Slice = new List(); - UInt32Slice = new List(); - Int64Slice = new List(); - UInt64Slice = new List(); - Float32Slice = new List(); - Float64Slice = new List(); - EnumSlice = new List(); - } - - public void Marshal(OutputStream stream) - { - stream.WriteBool(1, Bool); - stream.WriteInt32(2, Int32); - stream.WriteUInt32(3, UInt32); - stream.WriteInt64(4, Int64); - stream.WriteUInt64(5, UInt64); - stream.WriteFloat(6, Float32); - stream.WriteDouble(7, Float64); - stream.WriteString(8, Str); - stream.WriteBytes(9, BytesSlice); - stream.WriteBool(10, BoolSlice); - stream.WriteInt32(11, Int32Slice); - stream.WriteUInt32(12, UInt32Slice); - stream.WriteInt64(13, Int64Slice); - stream.WriteUInt64(14, UInt64Slice); - stream.WriteFloat(15, Float32Slice); - stream.WriteDouble(16, Float64Slice); - stream.WriteString(17, StrSlice); - stream.WriteEnum(18, Enum); - stream.WriteEnum(19, EnumSlice); - } - - public int GetSize() - { - int size = 0; - size += OutputStream.SizeBool(1, Bool); - size += OutputStream.SizeInt32(2, Int32); - size += OutputStream.SizeUInt32(3, UInt32); - size += OutputStream.SizeInt64(4, Int64); - size += OutputStream.SizeUInt64(5, UInt64); - size += OutputStream.SizeFloat(6, Float32); - size += OutputStream.SizeDouble(7, Float64); - size += OutputStream.SizeString(8, Str); - size += OutputStream.SizeBytes(9, BytesSlice); - size += OutputStream.SizeBool(10, BoolSlice); - size += OutputStream.SizeInt32(11, Int32Slice); - size += OutputStream.SizeUInt32(12, UInt32Slice); - size += OutputStream.SizeInt64(13, Int64Slice); - size += OutputStream.SizeUInt64(14, UInt64Slice); - size += OutputStream.SizeFloat(15, Float32Slice); - size += OutputStream.SizeDouble(16, Float64Slice); - size += OutputStream.SizeString(17, StrSlice); - size += OutputStream.SizeEnum(18, Enum); - size += OutputStream.SizeEnum(19, EnumSlice); - return size; - } - - public bool Unmarshal(InputStream stream, int fieldNumber, WireFormat.WireType wt) - { - switch (fieldNumber) - { - case 1: - stream.ReadBool(wt, ref Bool); - break; - case 2: - stream.ReadInt32(wt, ref Int32); - break; - case 3: - stream.ReadUInt32(wt, ref UInt32); - break; - case 4: - stream.ReadInt64(wt, ref Int64); - break; - case 5: - stream.ReadUInt64(wt, ref UInt64); - break; - case 6: - stream.ReadFloat(wt, ref Float32); - break; - case 7: - stream.ReadDouble(wt, ref Float64); - break; - case 8: - stream.ReadString(wt, ref Str); - break; - case 9: - stream.ReadBytes(wt, ref BytesSlice); - break; - case 10: - stream.ReadBool(wt, ref BoolSlice); - break; - case 11: - stream.ReadInt32(wt, ref Int32Slice); - break; - case 12: - stream.ReadUInt32(wt, ref UInt32Slice); - break; - case 13: - stream.ReadInt64(wt, ref Int64Slice); - break; - case 14: - stream.ReadUInt64(wt, ref UInt64Slice); - break; - case 15: - stream.ReadFloat(wt, ref Float32Slice); - break; - case 16: - stream.ReadDouble(wt, ref Float64Slice); - break; - case 17: - stream.ReadString(wt, ref StrSlice); - break; - case 18: - stream.ReadEnum(wt, ref Enum); - break; - case 19: - stream.ReadEnum(wt, ref EnumSlice); - break; - default: - return true; - } - - return false; - } - #endregion - } - - public partial class MyType : IProtoStruct - { - public bool Bool; - public int Int32; - public uint UInt32; - public long Int64; - public ulong UInt64; - public float Float32; - public double Float64; - public string Str; - public MySubType Struct; - public byte[] BytesSlice; - public List BoolSlice; - public List Int32Slice; - public List UInt32Slice; - public List Int64Slice; - public List UInt64Slice; - public List Float32Slice; - public List Float64Slice; - public List StrSlice; - public List StructSlice; - public MyEnum Enum; - public List EnumSlice; - - #region Serialize Code - public void Init( ) - { - BoolSlice = new List(); - Int32Slice = new List(); - UInt32Slice = new List(); - Int64Slice = new List(); - UInt64Slice = new List(); - Float32Slice = new List(); - Float64Slice = new List(); - StructSlice = new List(); - EnumSlice = new List(); - Struct = (MySubType) MessageMeta.NewStruct(typeof(MySubType)); - } - - public void Marshal(OutputStream stream) - { - stream.WriteBool(1, Bool); - stream.WriteInt32(2, Int32); - stream.WriteUInt32(3, UInt32); - stream.WriteInt64(4, Int64); - stream.WriteUInt64(5, UInt64); - stream.WriteFloat(6, Float32); - stream.WriteDouble(7, Float64); - stream.WriteString(8, Str); - stream.WriteStruct(9, Struct); - stream.WriteBytes(10, BytesSlice); - stream.WriteBool(11, BoolSlice); - stream.WriteInt32(12, Int32Slice); - stream.WriteUInt32(13, UInt32Slice); - stream.WriteInt64(14, Int64Slice); - stream.WriteUInt64(15, UInt64Slice); - stream.WriteFloat(16, Float32Slice); - stream.WriteDouble(17, Float64Slice); - stream.WriteString(18, StrSlice); - stream.WriteStruct(19, StructSlice); - stream.WriteEnum(20, Enum); - stream.WriteEnum(21, EnumSlice); - } - - public int GetSize() - { - int size = 0; - size += OutputStream.SizeBool(1, Bool); - size += OutputStream.SizeInt32(2, Int32); - size += OutputStream.SizeUInt32(3, UInt32); - size += OutputStream.SizeInt64(4, Int64); - size += OutputStream.SizeUInt64(5, UInt64); - size += OutputStream.SizeFloat(6, Float32); - size += OutputStream.SizeDouble(7, Float64); - size += OutputStream.SizeString(8, Str); - size += OutputStream.SizeStruct(9, Struct); - size += OutputStream.SizeBytes(10, BytesSlice); - size += OutputStream.SizeBool(11, BoolSlice); - size += OutputStream.SizeInt32(12, Int32Slice); - size += OutputStream.SizeUInt32(13, UInt32Slice); - size += OutputStream.SizeInt64(14, Int64Slice); - size += OutputStream.SizeUInt64(15, UInt64Slice); - size += OutputStream.SizeFloat(16, Float32Slice); - size += OutputStream.SizeDouble(17, Float64Slice); - size += OutputStream.SizeString(18, StrSlice); - size += OutputStream.SizeStruct(19, StructSlice); - size += OutputStream.SizeEnum(20, Enum); - size += OutputStream.SizeEnum(21, EnumSlice); - return size; - } - - public bool Unmarshal(InputStream stream, int fieldNumber, WireFormat.WireType wt) - { - switch (fieldNumber) - { - case 1: - stream.ReadBool(wt, ref Bool); - break; - case 2: - stream.ReadInt32(wt, ref Int32); - break; - case 3: - stream.ReadUInt32(wt, ref UInt32); - break; - case 4: - stream.ReadInt64(wt, ref Int64); - break; - case 5: - stream.ReadUInt64(wt, ref UInt64); - break; - case 6: - stream.ReadFloat(wt, ref Float32); - break; - case 7: - stream.ReadDouble(wt, ref Float64); - break; - case 8: - stream.ReadString(wt, ref Str); - break; - case 9: - stream.ReadStruct(wt, ref Struct); - break; - case 10: - stream.ReadBytes(wt, ref BytesSlice); - break; - case 11: - stream.ReadBool(wt, ref BoolSlice); - break; - case 12: - stream.ReadInt32(wt, ref Int32Slice); - break; - case 13: - stream.ReadUInt32(wt, ref UInt32Slice); - break; - case 14: - stream.ReadInt64(wt, ref Int64Slice); - break; - case 15: - stream.ReadUInt64(wt, ref UInt64Slice); - break; - case 16: - stream.ReadFloat(wt, ref Float32Slice); - break; - case 17: - stream.ReadDouble(wt, ref Float64Slice); - break; - case 18: - stream.ReadString(wt, ref StrSlice); - break; - case 19: - stream.ReadStruct(wt, ref StructSlice); - break; - case 20: - stream.ReadEnum(wt, ref Enum); - break; - case 21: - stream.ReadEnum(wt, ref EnumSlice); - break; - default: - return true; - } - - return false; - } - #endregion - } - public partial class LoginREQ : IProtoStruct { diff --git a/gen/pbscheme/func.go b/gen/pbscheme/func.go index 859c2f0..11f6345 100644 --- a/gen/pbscheme/func.go +++ b/gen/pbscheme/func.go @@ -1,32 +1,41 @@ package pbscheme import ( + "fmt" "github.com/davyxu/protoplus/model" "text/template" ) var UsefulFunc = template.FuncMap{} +func PrimitiveToPbType(primitiveType string) string { + switch primitiveType { + case "uint16": + return "uint32" + case "int16": + return "int32" + case "float32": + return "float" + case "float64": + return "double" + default: + return primitiveType + } +} + func init() { UsefulFunc["PbTypeName"] = func(raw interface{}) (ret string) { fd := raw.(*model.FieldDescriptor) - if fd.Repeatd { + switch { + case fd.IsMap(): + ret = fmt.Sprintf("map<%s,%s>", PrimitiveToPbType(fd.MapKey), PrimitiveToPbType(fd.MapValue)) + case fd.Repeatd: ret += "repeated " - } - - switch fd.Type { - case "uint16": - ret += "uint32" - case "int16": - ret += "int32" - case "float32": - ret += "float" - case "float64": - ret += "double" + ret += PrimitiveToPbType(fd.Type) default: - ret += fd.Type + ret = PrimitiveToPbType(fd.Type) } return diff --git a/go.mod b/go.mod index 3445606..e88dacc 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/davyxu/golexer v0.1.1-0.20200202091144-a15ddde83f6a github.com/davyxu/ulexer v0.0.0-20200713054812-c9bb8db3521f github.com/golang/protobuf v1.5.2 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/stretchr/testify v1.7.0 google.golang.org/protobuf v1.28.1 // indirect ) diff --git a/go.sum b/go.sum index 6c1ebfd..2f9f4a4 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/model/field.go b/model/field.go index ef12743..3269825 100644 --- a/model/field.go +++ b/model/field.go @@ -1,5 +1,7 @@ package model +import "strings" + type FieldDescriptor struct { Comment TagSet @@ -12,11 +14,22 @@ type FieldDescriptor struct { Tag int `json:",omitempty"` Repeatd bool `json:",omitempty"` + MapKey string + MapValue string + Descriptor *Descriptor `json:"-"` // 字段归属的父级描述符 } +func (self *FieldDescriptor) IsMap() bool { + return self.MapKey != "" && self.MapValue != "" +} + func (self *FieldDescriptor) ParseType(str string) { + if strings.Contains(str, "map") { + println(str) + } + self.Type = SchemeType2Type[str] if self.Type != "" { self.Kind = Kind_Primitive diff --git a/parser/parse_descriptorset.go b/parser/parse_descriptorset.go index fab9182..e746333 100644 --- a/parser/parse_descriptorset.go +++ b/parser/parse_descriptorset.go @@ -48,14 +48,17 @@ func rawParse(ctx *Context, reader io.Reader) (retErr error) { d.Kind = model.Kind_Enum parseObject(ctx, &d) case Token_Import: - parseImport(ctx) + retErr = parseImport(ctx) + if retErr != nil { + return retErr + } default: panic(errors.New("Unknown token: " + ctx.TokenValue())) } } - return nil + return } func checkAndFix(ctx *Context) error { @@ -69,13 +72,17 @@ func checkAndFix(ctx *Context) error { if fd.Kind == "" { - // 将字段中使用的结构体的Kind确认为struct - findD := ctx.ObjectByName(fd.Type) - if findD == nil { - return errors.New(fmt.Sprintf("type not found: %s at %s", fd.Type, ctx.QuerySymbolPosString(fd))) + if fd.IsMap() { + + } else { + // 将字段中使用的结构体的Kind确认为struct + findD := ctx.ObjectByName(fd.Type) + if findD == nil { + return errors.New(fmt.Sprintf("type not found: %s at %s", fd.Type, ctx.QuerySymbolPosString(fd))) + } + fd.Kind = findD.Kind } - fd.Kind = findD.Kind } } diff --git a/parser/parse_import.go b/parser/parse_import.go index 22c2d1f..0379b16 100644 --- a/parser/parse_import.go +++ b/parser/parse_import.go @@ -1,6 +1,8 @@ package parser -import "os" +import ( + "os" +) func parseImport(ctx *Context) error { diff --git a/parser/parse_test.go b/parser/parse_test.go index 9e4efe4..c437cdb 100644 --- a/parser/parse_test.go +++ b/parser/parse_test.go @@ -14,7 +14,7 @@ func checkString(t *testing.T, script string) *model.DescriptorSet { t.Error(err) t.FailNow() } else { - if data, err := json.Marshal(ds); err != nil { + if data, err := json.MarshalIndent(ds, "", "\t"); err != nil { t.Error(err) t.FailNow() } else { @@ -284,3 +284,15 @@ struct Person { } } + +func TestMap(t *testing.T) { + ds := checkString(t, ` +struct Data { + + idList []int32 + Mapper map[int32]string +} +`) + + ds.ObjectByName("Data") +} diff --git a/parser/parser.go b/parser/parser.go index f8bcb29..5597c3d 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -34,6 +34,7 @@ const ( Token_Service // service Token_RPC // rpc Token_Import // import + Token_Map // map ) type protoParser struct { @@ -139,6 +140,7 @@ func newProtoParser(srcName string) *protoParser { l.AddMatcher(golexer.NewKeywordMatcher(Token_Service, "service")) l.AddMatcher(golexer.NewKeywordMatcher(Token_RPC, "rpc")) l.AddMatcher(golexer.NewKeywordMatcher(Token_Import, "import")) + l.AddMatcher(golexer.NewKeywordMatcher(Token_Map, "map")) l.AddMatcher(golexer.NewIdentifierMatcher(Token_Identifier)) diff --git a/parser/structfield.go b/parser/structfield.go index 588c52a..f036328 100644 --- a/parser/structfield.go +++ b/parser/structfield.go @@ -19,16 +19,24 @@ func parseStructField(ctx *Context, fd *model.FieldDescriptor) { tp := ctx.TokenPos() - // [ 数组类型 - if ctx.TokenID() == Token_BracketL { + switch ctx.TokenID() { + case Token_BracketL: // [ 数组类型 ctx.NextToken() ctx.Expect(Token_BracketR) fd.Repeatd = true + // 延后在所有解析完后,检查TypeName是否合法,通过symbol还原位置并报错 + fd.ParseType(ctx.Expect(Token_Identifier).Value()) + case Token_Map: + ctx.NextToken() + ctx.Expect(Token_BracketL) + fd.MapKey = ctx.Expect(Token_Identifier).Value() + ctx.Expect(Token_BracketR) + fd.MapValue = ctx.Expect(Token_Identifier).Value() + default: + // 延后在所有解析完后,检查TypeName是否合法,通过symbol还原位置并报错 + fd.ParseType(ctx.Expect(Token_Identifier).Value()) } - // 延后在所有解析完后,检查TypeName是否合法,通过symbol还原位置并报错 - fd.ParseType(ctx.Expect(Token_Identifier).Value()) - fd.Comment = ctx.CommentGroupByLine(nameToken.Line()) ctx.AddSymbol(fd, tp) diff --git a/tests/Make.sh b/tests/Make.sh index 54ae890..dfcd274 100644 --- a/tests/Make.sh +++ b/tests/Make.sh @@ -7,9 +7,11 @@ go build -v -o=${GOPATH}/bin/protoplus github.com/davyxu/protoplus/cmd/protoplus # code.proto为输入文件 ProtoPlusBin=${GOPATH}/bin/protoplus -${ProtoPlusBin} -ppgo_out=code_gen.go -package=tests filelist.proto +# 原生输出请使用pb +#${ProtoPlusBin} -ppgo_out=code_gen.go -package=tests filelist.proto +#${ProtoPlusBin} -ppcs_out=../example/csharp/Example/ProtoGen.cs -package=Proto filelist.proto + ${ProtoPlusBin} -ppgoreg_out=reg_gen.go -package=tests filelist.proto -${ProtoPlusBin} -ppcs_out=../example/csharp/Example/ProtoGen.cs -package=Proto filelist.proto ${ProtoPlusBin} -ppcsreg_out=../example/csharp/Example/ProtoGenReg.cs -package=Proto filelist.proto ${ProtoPlusBin} -pbscheme_out=pb_gen.proto -package=proto filelist.proto ${ProtoPlusBin} -route_out=route.json -package=proto filelist.proto \ No newline at end of file diff --git a/tests/code_gen.go b/tests/code_gen.go index 2614931..36dffd6 100644 --- a/tests/code_gen.go +++ b/tests/code_gen.go @@ -14,547 +14,6 @@ var ( _ unsafe.Pointer ) -type MyEnum int32 - -const ( - MyEnum_Zero MyEnum = 0 - MyEnum_One MyEnum = 1 - MyEnum_Two MyEnum = 2 -) - -var ( - MyEnumMapperValueByName = map[string]int32{ - "Zero": 0, - "One": 1, - "Two": 2, - } - - MyEnumMapperNameByValue = map[int32]string{ - 0: "Zero", - 1: "One", - 2: "Two", - } -) - -func (self MyEnum) String() string { - return MyEnumMapperNameByValue[int32(self)] -} - -type MyTypeMini struct { - Bool bool - Int32 int32 - UInt32 uint32 - Int64 int64 - UInt64 uint64 - Float32 float32 - Float64 float64 - Str string -} - -func (self *MyTypeMini) String() string { return ppgo.CompactTextString(self) } - -func (self *MyTypeMini) Size() (ret int) { - - ret += wire.SizeBool(1, self.Bool) - - ret += wire.SizeInt32(2, self.Int32) - - ret += wire.SizeUInt32(3, self.UInt32) - - ret += wire.SizeInt64(4, self.Int64) - - ret += wire.SizeUInt64(5, self.UInt64) - - ret += wire.SizeFloat32(6, self.Float32) - - ret += wire.SizeFloat64(7, self.Float64) - - ret += wire.SizeString(8, self.Str) - - return -} - -func (self *MyTypeMini) Marshal(buffer *wire.Buffer) error { - - wire.MarshalBool(buffer, 1, self.Bool) - - wire.MarshalInt32(buffer, 2, self.Int32) - - wire.MarshalUInt32(buffer, 3, self.UInt32) - - wire.MarshalInt64(buffer, 4, self.Int64) - - wire.MarshalUInt64(buffer, 5, self.UInt64) - - wire.MarshalFloat32(buffer, 6, self.Float32) - - wire.MarshalFloat64(buffer, 7, self.Float64) - - wire.MarshalString(buffer, 8, self.Str) - - return nil -} - -func (self *MyTypeMini) Unmarshal(buffer *wire.Buffer, fieldIndex uint64, wt wire.WireType) error { - switch fieldIndex { - case 1: - v, err := wire.UnmarshalBool(buffer, wt) - self.Bool = v - return err - case 2: - v, err := wire.UnmarshalInt32(buffer, wt) - self.Int32 = v - return err - case 3: - v, err := wire.UnmarshalUInt32(buffer, wt) - self.UInt32 = v - return err - case 4: - v, err := wire.UnmarshalInt64(buffer, wt) - self.Int64 = v - return err - case 5: - v, err := wire.UnmarshalUInt64(buffer, wt) - self.UInt64 = v - return err - case 6: - v, err := wire.UnmarshalFloat32(buffer, wt) - self.Float32 = v - return err - case 7: - v, err := wire.UnmarshalFloat64(buffer, wt) - self.Float64 = v - return err - case 8: - v, err := wire.UnmarshalString(buffer, wt) - self.Str = v - return err - - } - - return wire.ErrUnknownField -} - -type MySubType struct { - Bool bool - Int32 int32 - UInt32 uint32 - Int64 int64 - UInt64 uint64 - Float32 float32 - Float64 float64 - Str string - BytesSlice []byte - BoolSlice []bool - Int32Slice []int32 - UInt32Slice []uint32 - Int64Slice []int64 - UInt64Slice []uint64 - Float32Slice []float32 - Float64Slice []float64 - StrSlice []string - Enum MyEnum - EnumSlice []MyEnum -} - -func (self *MySubType) String() string { return ppgo.CompactTextString(self) } - -func (self *MySubType) Size() (ret int) { - - ret += wire.SizeBool(1, self.Bool) - - ret += wire.SizeInt32(2, self.Int32) - - ret += wire.SizeUInt32(3, self.UInt32) - - ret += wire.SizeInt64(4, self.Int64) - - ret += wire.SizeUInt64(5, self.UInt64) - - ret += wire.SizeFloat32(6, self.Float32) - - ret += wire.SizeFloat64(7, self.Float64) - - ret += wire.SizeString(8, self.Str) - - ret += wire.SizeBytes(9, self.BytesSlice) - - ret += wire.SizeBoolSlice(10, self.BoolSlice) - - ret += wire.SizeInt32Slice(11, self.Int32Slice) - - ret += wire.SizeUInt32Slice(12, self.UInt32Slice) - - ret += wire.SizeInt64Slice(13, self.Int64Slice) - - ret += wire.SizeUInt64Slice(14, self.UInt64Slice) - - ret += wire.SizeFloat32Slice(15, self.Float32Slice) - - ret += wire.SizeFloat64Slice(16, self.Float64Slice) - - ret += wire.SizeStringSlice(17, self.StrSlice) - - ret += wire.SizeInt32(18, int32(self.Enum)) - - ret += wire.SizeInt32Slice(19, *(*[]int32)(unsafe.Pointer(&self.EnumSlice))) - - return -} - -func (self *MySubType) Marshal(buffer *wire.Buffer) error { - - wire.MarshalBool(buffer, 1, self.Bool) - - wire.MarshalInt32(buffer, 2, self.Int32) - - wire.MarshalUInt32(buffer, 3, self.UInt32) - - wire.MarshalInt64(buffer, 4, self.Int64) - - wire.MarshalUInt64(buffer, 5, self.UInt64) - - wire.MarshalFloat32(buffer, 6, self.Float32) - - wire.MarshalFloat64(buffer, 7, self.Float64) - - wire.MarshalString(buffer, 8, self.Str) - - wire.MarshalBytes(buffer, 9, self.BytesSlice) - - wire.MarshalBoolSlice(buffer, 10, self.BoolSlice) - - wire.MarshalInt32Slice(buffer, 11, self.Int32Slice) - - wire.MarshalUInt32Slice(buffer, 12, self.UInt32Slice) - - wire.MarshalInt64Slice(buffer, 13, self.Int64Slice) - - wire.MarshalUInt64Slice(buffer, 14, self.UInt64Slice) - - wire.MarshalFloat32Slice(buffer, 15, self.Float32Slice) - - wire.MarshalFloat64Slice(buffer, 16, self.Float64Slice) - - wire.MarshalStringSlice(buffer, 17, self.StrSlice) - - wire.MarshalInt32(buffer, 18, int32(self.Enum)) - - wire.MarshalInt32Slice(buffer, 19, *(*[]int32)(unsafe.Pointer(&self.EnumSlice))) - - return nil -} - -func (self *MySubType) Unmarshal(buffer *wire.Buffer, fieldIndex uint64, wt wire.WireType) error { - switch fieldIndex { - case 1: - v, err := wire.UnmarshalBool(buffer, wt) - self.Bool = v - return err - case 2: - v, err := wire.UnmarshalInt32(buffer, wt) - self.Int32 = v - return err - case 3: - v, err := wire.UnmarshalUInt32(buffer, wt) - self.UInt32 = v - return err - case 4: - v, err := wire.UnmarshalInt64(buffer, wt) - self.Int64 = v - return err - case 5: - v, err := wire.UnmarshalUInt64(buffer, wt) - self.UInt64 = v - return err - case 6: - v, err := wire.UnmarshalFloat32(buffer, wt) - self.Float32 = v - return err - case 7: - v, err := wire.UnmarshalFloat64(buffer, wt) - self.Float64 = v - return err - case 8: - v, err := wire.UnmarshalString(buffer, wt) - self.Str = v - return err - case 9: - v, err := wire.UnmarshalBytes(buffer, wt) - self.BytesSlice = v - return err - case 10: - v, err := wire.UnmarshalBoolSlice(buffer, wt) - self.BoolSlice = append(self.BoolSlice, v...) - return err - case 11: - v, err := wire.UnmarshalInt32Slice(buffer, wt) - self.Int32Slice = append(self.Int32Slice, v...) - return err - case 12: - v, err := wire.UnmarshalUInt32Slice(buffer, wt) - self.UInt32Slice = append(self.UInt32Slice, v...) - return err - case 13: - v, err := wire.UnmarshalInt64Slice(buffer, wt) - self.Int64Slice = append(self.Int64Slice, v...) - return err - case 14: - v, err := wire.UnmarshalUInt64Slice(buffer, wt) - self.UInt64Slice = append(self.UInt64Slice, v...) - return err - case 15: - v, err := wire.UnmarshalFloat32Slice(buffer, wt) - self.Float32Slice = append(self.Float32Slice, v...) - return err - case 16: - v, err := wire.UnmarshalFloat64Slice(buffer, wt) - self.Float64Slice = append(self.Float64Slice, v...) - return err - case 17: - v, err := wire.UnmarshalStringSlice(buffer, wt) - self.StrSlice = append(self.StrSlice, v...) - return err - case 18: - v, err := wire.UnmarshalInt32(buffer, wt) - self.Enum = MyEnum(v) - return err - case 19: - v, err := wire.UnmarshalInt32Slice(buffer, wt) - for _, vv := range v { - self.EnumSlice = append(self.EnumSlice, MyEnum(vv)) - } - return err - - } - - return wire.ErrUnknownField -} - -type MyType struct { - Bool bool - Int32 int32 - UInt32 uint32 - Int64 int64 - UInt64 uint64 - Float32 float32 - Float64 float64 - Str string - Struct *MySubType - BytesSlice []byte - BoolSlice []bool - Int32Slice []int32 - UInt32Slice []uint32 - Int64Slice []int64 - UInt64Slice []uint64 - Float32Slice []float32 - Float64Slice []float64 - StrSlice []string - StructSlice []*MySubType - Enum MyEnum - EnumSlice []MyEnum -} - -func (self *MyType) String() string { return ppgo.CompactTextString(self) } - -func (self *MyType) Size() (ret int) { - - ret += wire.SizeBool(1, self.Bool) - - ret += wire.SizeInt32(2, self.Int32) - - ret += wire.SizeUInt32(3, self.UInt32) - - ret += wire.SizeInt64(4, self.Int64) - - ret += wire.SizeUInt64(5, self.UInt64) - - ret += wire.SizeFloat32(6, self.Float32) - - ret += wire.SizeFloat64(7, self.Float64) - - ret += wire.SizeString(8, self.Str) - - ret += wire.SizeStruct(9, self.Struct) - - ret += wire.SizeBytes(10, self.BytesSlice) - - ret += wire.SizeBoolSlice(11, self.BoolSlice) - - ret += wire.SizeInt32Slice(12, self.Int32Slice) - - ret += wire.SizeUInt32Slice(13, self.UInt32Slice) - - ret += wire.SizeInt64Slice(14, self.Int64Slice) - - ret += wire.SizeUInt64Slice(15, self.UInt64Slice) - - ret += wire.SizeFloat32Slice(16, self.Float32Slice) - - ret += wire.SizeFloat64Slice(17, self.Float64Slice) - - ret += wire.SizeStringSlice(18, self.StrSlice) - - if len(self.StructSlice) > 0 { - for _, elm := range self.StructSlice { - ret += wire.SizeStruct(19, elm) - } - } - - ret += wire.SizeInt32(20, int32(self.Enum)) - - ret += wire.SizeInt32Slice(21, *(*[]int32)(unsafe.Pointer(&self.EnumSlice))) - - return -} - -func (self *MyType) Marshal(buffer *wire.Buffer) error { - - wire.MarshalBool(buffer, 1, self.Bool) - - wire.MarshalInt32(buffer, 2, self.Int32) - - wire.MarshalUInt32(buffer, 3, self.UInt32) - - wire.MarshalInt64(buffer, 4, self.Int64) - - wire.MarshalUInt64(buffer, 5, self.UInt64) - - wire.MarshalFloat32(buffer, 6, self.Float32) - - wire.MarshalFloat64(buffer, 7, self.Float64) - - wire.MarshalString(buffer, 8, self.Str) - - wire.MarshalStruct(buffer, 9, self.Struct) - - wire.MarshalBytes(buffer, 10, self.BytesSlice) - - wire.MarshalBoolSlice(buffer, 11, self.BoolSlice) - - wire.MarshalInt32Slice(buffer, 12, self.Int32Slice) - - wire.MarshalUInt32Slice(buffer, 13, self.UInt32Slice) - - wire.MarshalInt64Slice(buffer, 14, self.Int64Slice) - - wire.MarshalUInt64Slice(buffer, 15, self.UInt64Slice) - - wire.MarshalFloat32Slice(buffer, 16, self.Float32Slice) - - wire.MarshalFloat64Slice(buffer, 17, self.Float64Slice) - - wire.MarshalStringSlice(buffer, 18, self.StrSlice) - - for _, elm := range self.StructSlice { - wire.MarshalStruct(buffer, 19, elm) - } - - wire.MarshalInt32(buffer, 20, int32(self.Enum)) - - wire.MarshalInt32Slice(buffer, 21, *(*[]int32)(unsafe.Pointer(&self.EnumSlice))) - - return nil -} - -func (self *MyType) Unmarshal(buffer *wire.Buffer, fieldIndex uint64, wt wire.WireType) error { - switch fieldIndex { - case 1: - v, err := wire.UnmarshalBool(buffer, wt) - self.Bool = v - return err - case 2: - v, err := wire.UnmarshalInt32(buffer, wt) - self.Int32 = v - return err - case 3: - v, err := wire.UnmarshalUInt32(buffer, wt) - self.UInt32 = v - return err - case 4: - v, err := wire.UnmarshalInt64(buffer, wt) - self.Int64 = v - return err - case 5: - v, err := wire.UnmarshalUInt64(buffer, wt) - self.UInt64 = v - return err - case 6: - v, err := wire.UnmarshalFloat32(buffer, wt) - self.Float32 = v - return err - case 7: - v, err := wire.UnmarshalFloat64(buffer, wt) - self.Float64 = v - return err - case 8: - v, err := wire.UnmarshalString(buffer, wt) - self.Str = v - return err - case 9: - var elm MySubType - self.Struct = &elm - return wire.UnmarshalStruct(buffer, wt, self.Struct) - case 10: - v, err := wire.UnmarshalBytes(buffer, wt) - self.BytesSlice = v - return err - case 11: - v, err := wire.UnmarshalBoolSlice(buffer, wt) - self.BoolSlice = append(self.BoolSlice, v...) - return err - case 12: - v, err := wire.UnmarshalInt32Slice(buffer, wt) - self.Int32Slice = append(self.Int32Slice, v...) - return err - case 13: - v, err := wire.UnmarshalUInt32Slice(buffer, wt) - self.UInt32Slice = append(self.UInt32Slice, v...) - return err - case 14: - v, err := wire.UnmarshalInt64Slice(buffer, wt) - self.Int64Slice = append(self.Int64Slice, v...) - return err - case 15: - v, err := wire.UnmarshalUInt64Slice(buffer, wt) - self.UInt64Slice = append(self.UInt64Slice, v...) - return err - case 16: - v, err := wire.UnmarshalFloat32Slice(buffer, wt) - self.Float32Slice = append(self.Float32Slice, v...) - return err - case 17: - v, err := wire.UnmarshalFloat64Slice(buffer, wt) - self.Float64Slice = append(self.Float64Slice, v...) - return err - case 18: - v, err := wire.UnmarshalStringSlice(buffer, wt) - self.StrSlice = append(self.StrSlice, v...) - return err - case 19: - var elm MySubType - if err := wire.UnmarshalStruct(buffer, wt, &elm); err != nil { - return err - } else { - self.StructSlice = append(self.StructSlice, &elm) - return nil - } - case 20: - v, err := wire.UnmarshalInt32(buffer, wt) - self.Enum = MyEnum(v) - return err - case 21: - v, err := wire.UnmarshalInt32Slice(buffer, wt) - for _, vv := range v { - self.EnumSlice = append(self.EnumSlice, MyEnum(vv)) - } - return err - - } - - return wire.ErrUnknownField -} - type LoginREQ struct { } diff --git a/tests/pb_gen.proto b/tests/pb_gen.proto index 454e333..ecdc822 100644 --- a/tests/pb_gen.proto +++ b/tests/pb_gen.proto @@ -72,6 +72,7 @@ message MyType { repeated MySubType StructSlice = 19; MyEnum Enum = 20; repeated MyEnum EnumSlice = 21; + map Map = 22; } diff --git a/tests/type.proto b/tests/type.proto index 3f0ff54..f3c0ef5 100644 --- a/tests/type.proto +++ b/tests/type.proto @@ -1,6 +1,6 @@ struct MyTypeMini { - Bool bool + Bool bool Int32 int32 UInt32 uint32 @@ -44,15 +44,15 @@ struct MySubType UInt64Slice []uint64 - Float32Slice []float32 + Float32Slice []float32 - Float64Slice []float64 + Float64Slice []float64 StrSlice []string - Enum MyEnum + Enum MyEnum - EnumSlice []MyEnum + EnumSlice []MyEnum } @@ -82,14 +82,16 @@ struct MyType UInt64Slice []uint64 - Float32Slice []float32 + Float32Slice []float32 - Float64Slice []float64 + Float64Slice []float64 StrSlice []string - StructSlice []MySubType - Enum MyEnum + StructSlice []MySubType + Enum MyEnum + + EnumSlice []MyEnum - EnumSlice []MyEnum + Map map[int32]string }