Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: update google/protobuf/descriptor.proto to proto3 #39248

Closed
crytaljin opened this issue May 26, 2020 · 2 comments
Closed

proposal: update google/protobuf/descriptor.proto to proto3 #39248

crytaljin opened this issue May 26, 2020 · 2 comments

Comments

@crytaljin
Copy link

crytaljin commented May 26, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/Crystal/Library/Caches/go-build"
GOENV="/Users/Crystal/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/Crystal/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bp/5wf5wz6n2r57410661mrply40000gn/T/go-build285114793=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I wanted to include type info of a proto message by creating a wrapper message that includes a google.protobuf.DescriptorProto field. However, when I call .ProtoReflect().Descriptor() on the *descriptorpb.DescriptorProto field, all the info is lost. I believe this is due to the descriptor.proto file having outdated "proto2" syntax. Here is how I tested this:

  1. Create a test .proto file.
syntax = "proto3";
package project;

option go_package = ".;project";

message Test {
    string data = 1; 
}
  1. Generate the .pb.go file with protoc. (I used protoc -I=. --go_out=. ./test.proto)
  2. Write a test file:
import (
	"fmt"
	reflect "reflect"
	"testing"

	"google.golang.org/protobuf/reflect/protodesc"
)

func TestDesc(t *testing.T) {
	data := &Test{
		Data: "hi",
	}

	prMessage := data.ProtoReflect() //protoreflect.Message
	msgDesc := prMessage.Descriptor() //protoreflect MessageDescriptor
	fmt.Println("msgDesc ", msgDesc)

	descProto := protodesc.ToDescriptorProto(msgDesc) //converts from protoreflect.MessageDescriptor to *descriptorpb.DescriptorProto

	prMessage2 := descProto.ProtoReflect() //protoreflect.Message 
	msgDesc2 := prMessage2.Descriptor() //protoreflect MessageDescriptor
	fmt.Println("msgDesc2 ", msgDesc2)

	if !reflect.DeepEqual(msgDesc, msgDesc2) {
		t.Errorf("(%s) does not match (%s)", msgDesc, msgDesc2)
	}
}

What did you expect to see?

I expected to get back the MessageDescriptor (so msgDesc and msgDesc2 should match)

What did you see instead?

msgDesc  MessageDescriptor{Syntax: proto3, FullName: project.Test, Fields: [{Name: data, Number: 1, Cardinality: optional, Kind: string, HasJSONName: true, JSONName: "data"}]}
msgDesc2  MessageDescriptor{Syntax: proto2, FullName: google.protobuf.DescriptorProto, Fields: [{Name: name, Number: 1, Cardinality: optional, Kind: string, HasJSONName: true, JSONName: "name", HasPresence: true}, {Name: field, Number: 2, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "field", IsList: true, Message: google.protobuf.FieldDescriptorProto}, {Name: extension, Number: 6, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "extension", IsList: true, Message: google.protobuf.FieldDescriptorProto}, {Name: nested_type, Number: 3, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "nestedType", IsList: true, Message: google.protobuf.DescriptorProto}, {Name: enum_type, Number: 4, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "enumType", IsList: true, Message: google.protobuf.EnumDescriptorProto}, {Name: extension_range, Number: 5, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "extensionRange", IsList: true, Message: google.protobuf.DescriptorProto.ExtensionRange}, {Name: oneof_decl, Number: 8, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "oneofDecl", IsList: true, Message: google.protobuf.OneofDescriptorProto}, {Name: options, Number: 7, Cardinality: optional, Kind: message, HasJSONName: true, JSONName: "options", HasPresence: true, Message: google.protobuf.MessageOptions}, {Name: reserved_range, Number: 9, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "reservedRange", IsList: true, Message: google.protobuf.DescriptorProto.ReservedRange}, {Name: reserved_name, Number: 10, Cardinality: repeated, Kind: string, HasJSONName: true, JSONName: "reservedName", IsList: true}], Messages: [{Name: ExtensionRange, Fields: [{Name: start, Number: 1, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "start", HasPresence: true}, {Name: end, Number: 2, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "end", HasPresence: true}, {Name: options, Number: 3, Cardinality: optional, Kind: message, HasJSONName: true, JSONName: "options", HasPresence: true, Message: google.protobuf.ExtensionRangeOptions}]}, {Name: ReservedRange, Fields: [{Name: start, Number: 1, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "start", HasPresence: true}, {Name: end, Number: 2, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "end", HasPresence: true}]}]}
--- FAIL: TestDesc (0.00s)
    /Users/Crystal/go/src/project/test_test.go:30: (MessageDescriptor{Syntax: proto3, FullName: project.Test, Fields: [{Name: data, Number: 1, Cardinality: optional, Kind: string, HasJSONName: true, JSONName: "data"}]}) does not match (MessageDescriptor{Syntax: proto2, FullName: google.protobuf.DescriptorProto, Fields: [{Name: name, Number: 1, Cardinality: optional, Kind: string, HasJSONName: true, JSONName: "name", HasPresence: true}, {Name: field, Number: 2, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "field", IsList: true, Message: google.protobuf.FieldDescriptorProto}, {Name: extension, Number: 6, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "extension", IsList: true, Message: google.protobuf.FieldDescriptorProto}, {Name: nested_type, Number: 3, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "nestedType", IsList: true, Message: google.protobuf.DescriptorProto}, {Name: enum_type, Number: 4, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "enumType", IsList: true, Message: google.protobuf.EnumDescriptorProto}, {Name: extension_range, Number: 5, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "extensionRange", IsList: true, Message: google.protobuf.DescriptorProto.ExtensionRange}, {Name: oneof_decl, Number: 8, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "oneofDecl", IsList: true, Message: google.protobuf.OneofDescriptorProto}, {Name: options, Number: 7, Cardinality: optional, Kind: message, HasJSONName: true, JSONName: "options", HasPresence: true, Message: google.protobuf.MessageOptions}, {Name: reserved_range, Number: 9, Cardinality: repeated, Kind: message, HasJSONName: true, JSONName: "reservedRange", IsList: true, Message: google.protobuf.DescriptorProto.ReservedRange}, {Name: reserved_name, Number: 10, Cardinality: repeated, Kind: string, HasJSONName: true, JSONName: "reservedName", IsList: true}], Messages: [{Name: ExtensionRange, Fields: [{Name: start, Number: 1, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "start", HasPresence: true}, {Name: end, Number: 2, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "end", HasPresence: true}, {Name: options, Number: 3, Cardinality: optional, Kind: message, HasJSONName: true, JSONName: "options", HasPresence: true, Message: google.protobuf.ExtensionRangeOptions}]}, {Name: ReservedRange, Fields: [{Name: start, Number: 1, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "start", HasPresence: true}, {Name: end, Number: 2, Cardinality: optional, Kind: int32, HasJSONName: true, JSONName: "end", HasPresence: true}]}]})

msgDesc2 has syntax proto2 instead of proto3 and loses all the info from the MessageDescriptor.

@gopherbot gopherbot added this to the Proposal milestone May 26, 2020
@neild
Copy link
Contributor

neild commented May 26, 2020

This is a protocol buffer question, and therefore more appropriate to github.com/golang/protobuf. Or actually, since it's about descriptor.proto, github.com/protocolbuffers/protobuf.

This also has nothing to do with proto2 vs. proto3.

The result you're seeing is because msgDesc is the descriptor of the Test message type, and msgDesc2 is the descriptor of the google.protobuf.DescriptorProto message type. You can easily see this if you compare the FullName of each descriptor.

@crytaljin
Copy link
Author

Ah that makes sense, my bad!

@golang golang locked and limited conversation to collaborators May 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants