Skip to content

Commit

Permalink
reflect/protodesc: restore edition in protodesc.ToDescriptorProto
Browse files Browse the repository at this point in the history
I have choosen to implement this via interace assertion so that
other implementers of protoreflect.FileDescriptor can implement
this as well.

Change-Id: Ib907895044e89bdba1009cc09129d3dd1224561f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/573055
Reviewed-by: Michael Stapelberg <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
lfolger committed Mar 21, 2024
1 parent 3039476 commit 59034d8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
9 changes: 6 additions & 3 deletions internal/filedesc/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ func (fd *File) ParentFile() protoreflect.FileDescriptor { return fd }
func (fd *File) Parent() protoreflect.Descriptor { return nil }
func (fd *File) Index() int { return 0 }
func (fd *File) Syntax() protoreflect.Syntax { return fd.L1.Syntax }
func (fd *File) Name() protoreflect.Name { return fd.L1.Package.Name() }
func (fd *File) FullName() protoreflect.FullName { return fd.L1.Package }
func (fd *File) IsPlaceholder() bool { return false }

// Not exported and just used to reconstruct the original FileDescriptor proto
func (fd *File) Edition() int32 { return int32(fd.L1.Edition) }
func (fd *File) Name() protoreflect.Name { return fd.L1.Package.Name() }
func (fd *File) FullName() protoreflect.FullName { return fd.L1.Package }
func (fd *File) IsPlaceholder() bool { return false }
func (fd *File) Options() protoreflect.ProtoMessage {
if f := fd.lazyInit().Options; f != nil {
return f()
Expand Down
21 changes: 18 additions & 3 deletions reflect/protodesc/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
`)
protoEdition2023Message = mustParseFile(`
syntax: "editions"
edition: 2023
edition: EDITION_2023
name: "proto_editions_2023_message.proto"
package: "test.editions2023"
options: {
Expand All @@ -70,7 +70,7 @@ var (
`)
protoEdition2024Message = mustParseFile(`
syntax: "editions"
edition: 2024
edition: EDITION_2024
name: "proto_editions_2024_message.proto"
package: "test.editions2024"
message_type: [{
Expand Down Expand Up @@ -420,6 +420,20 @@ func TestNewFile(t *testing.T) {
field: [{name:"F" number:1 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".fizz.M.M"}]
}]
`),
}, {
label: "basic editions tests",
inDesc: mustParseFile(`
syntax: "editions"
edition: EDITION_2023
name: "test.proto"
package: "fizz"
`),
wantDesc: mustParseFile(`
syntax: "editions"
edition: EDITION_2023
name: "test.proto"
package: "fizz"
`),
}, {
label: "namespace conflict on enum value",
inDesc: mustParseFile(`
Expand Down Expand Up @@ -842,7 +856,8 @@ func TestNewFile(t *testing.T) {
}, {
label: "proto editions implicit presence field with defaults",
inDesc: mustParseFile(`
syntax: "proto3"
syntax: "editions"
edition: EDITION_2023
name: "test.proto"
message_type: [{name:"M" nested_type:[{
name: "M"
Expand Down
10 changes: 10 additions & 0 deletions reflect/protodesc/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func ToFileDescriptorProto(file protoreflect.FileDescriptor) *descriptorpb.FileD
if syntax := file.Syntax(); syntax != protoreflect.Proto2 && syntax.IsValid() {
p.Syntax = proto.String(file.Syntax().String())
}
if file.Syntax() == protoreflect.Editions {
desc := file
if fileImportDesc, ok := file.(protoreflect.FileImport); ok {
desc = fileImportDesc.FileDescriptor
}

if editionsInterface, ok := desc.(interface{ Edition() int32 }); ok {
p.Edition = descriptorpb.Edition(editionsInterface.Edition()).Enum()
}
}
return p
}

Expand Down
2 changes: 1 addition & 1 deletion reflect/protoreflect/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const (
// IsValid reports whether the syntax is valid.
func (s Syntax) IsValid() bool {
switch s {
case Proto2, Proto3:
case Proto2, Proto3, Editions:
return true
default:
return false
Expand Down

0 comments on commit 59034d8

Please sign in to comment.