From 49de7c273a24ab7296c5bed89991c5a69bf69bc0 Mon Sep 17 00:00:00 2001 From: Arkadiy Tetelman Date: Sun, 1 Jan 2023 19:00:29 -0800 Subject: [PATCH] More bug fixes --- pkg/protodump/fixtures/test.proto | 11 ++++++----- pkg/protodump/proto.go | 14 +++++++++++--- pkg/protodump/scan.go | 13 ++++++++++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/pkg/protodump/fixtures/test.proto b/pkg/protodump/fixtures/test.proto index 283e037..255a253 100644 --- a/pkg/protodump/fixtures/test.proto +++ b/pkg/protodump/fixtures/test.proto @@ -3,6 +3,7 @@ syntax = "proto3"; package hello.world; option go_package = "./;helloworld"; +option php_namespace = "Google\\Cloud\\Iam\\V1"; service Greeter { rpc SayHello (.hello.world.HelloRequest) returns (.hello.world.HelloReply) {} @@ -30,14 +31,14 @@ message HelloRequest { } string name = 1; - hello.world.HelloRequest.Test blah = 2; - hello.world.HelloRequest.NestedEnum blahblah = 3; - hello.world.HelloReply blaha = 4; + .hello.world.HelloRequest.Test blah = 2; + .hello.world.HelloRequest.NestedEnum blahblah = 3; + .hello.world.HelloReply blaha = 4; } message HelloReply { - hello.world.HelloRequest.Test blah = 2; - hello.world.HelloRequest.NestedEnum blahblah = 3; + .hello.world.HelloRequest.Test blah = 2; + .hello.world.HelloRequest.NestedEnum blahblah = 3; string message = 1; } diff --git a/pkg/protodump/proto.go b/pkg/protodump/proto.go index 5100d7f..1642af2 100644 --- a/pkg/protodump/proto.go +++ b/pkg/protodump/proto.go @@ -2,6 +2,7 @@ package protodump import ( "fmt" + "path" "reflect" "strconv" "strings" @@ -44,7 +45,13 @@ func (pd *ProtoDefinition) String() string { } func (pd *ProtoDefinition) Filename() string { - return pd.filename + goPackage := pd.pb.GetOptions().GetGoPackage() + index := strings.Index(goPackage, ";") + if index == -1 { + return pd.descriptor.Path() + } + + return path.Join(goPackage[:index], path.Base(pd.descriptor.Path())) } func (pd *ProtoDefinition) writeMethod(method protoreflect.MethodDescriptor) { @@ -83,8 +90,10 @@ func (pd *ProtoDefinition) writeType(field protoreflect.FieldDescriptor) { kind := field.Kind().String() if kind == "message" { + pd.write(".") pd.write(string(field.Message().FullName())) } else if kind == "enum" { + pd.write(".") pd.write(string(field.Enum().FullName())) } else if kind == "map" { pd.write("map<") @@ -217,7 +226,7 @@ func (pd *ProtoDefinition) writeStringFileOptions(name string, value string) { pd.write("option ") pd.write(name) pd.write(" = \"") - pd.write(value) + pd.write(strings.ReplaceAll(value, "\\", "\\\\")) pd.write("\";\n") } @@ -333,7 +342,6 @@ func NewFromDescriptor(pb *descriptorpb.FileDescriptorProto) (*ProtoDefinition, pd := ProtoDefinition{ pb: pb, descriptor: descriptor, - filename: descriptor.Path(), } pd.writeFileDescriptor() diff --git a/pkg/protodump/scan.go b/pkg/protodump/scan.go index 42c819d..bda100f 100644 --- a/pkg/protodump/scan.go +++ b/pkg/protodump/scan.go @@ -13,13 +13,24 @@ const magicByte = 0xa func consumeBytes(data []byte, position int) int { start := position + consumedFieldOne := false for { - _, _, len := protowire.ConsumeField(data[position:]) + number, _, len := protowire.ConsumeField(data[position:]) if len < 0 { _ = protowire.ParseError(len) return position - start } + + // Only consume Field 1 once (to handle the case where protobuf definitions are adjacent + // in program memory) + if number == 1 { + if consumedFieldOne { + return position - start + } + consumedFieldOne = true + } + position += len } }