Skip to content

Commit

Permalink
all: Release the Opaque API
Browse files Browse the repository at this point in the history
For golang/protobuf#1657

Change-Id: I7b2b0c30506706015ce278e6054439c9ad9ef727
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/634815
TryBot-Bypass: Michael Stapelberg <[email protected]>
Reviewed-by: Joseph Tsai <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
stapelberg committed Dec 11, 2024
1 parent 5f5de33 commit eb7b468
Show file tree
Hide file tree
Showing 101 changed files with 17,785 additions and 142 deletions.
744 changes: 744 additions & 0 deletions cmd/protoc-gen-go/builder_test/builder_test.go

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions cmd/protoc-gen-go/descriptor_test/descriptor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package descriptor_test

import (
"testing"

testopenpb "google.golang.org/protobuf/internal/testprotos/test"
testhybridpb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_hybrid"
testopaquepb "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_opaque"
)

func TestFileModeEnum(t *testing.T) {
var e any = testopenpb.ForeignEnum_FOREIGN_FOO
if _, ok := e.(interface{ EnumDescriptor() ([]byte, []int) }); !ok {
t.Errorf("Open V1 proto did not have deprecated method EnumDescriptor")
}
var oe any = testopaquepb.ForeignEnum_FOREIGN_FOO
if _, ok := oe.(interface{ EnumDescriptor() ([]byte, []int) }); ok {
t.Errorf("Opaque V0 proto did have deprecated method EnumDescriptor")
}
var he any = testhybridpb.ForeignEnum_FOREIGN_FOO
if _, ok := he.(interface{ EnumDescriptor() ([]byte, []int) }); ok {
t.Errorf("Hybrid proto did have deprecated method EnumDescriptor")
}
}

func TestFileModeMessage(t *testing.T) {
var p any = &testopenpb.TestAllTypes{}
if _, ok := p.(interface{ Descriptor() ([]byte, []int) }); !ok {
t.Errorf("Open V1 proto did not have deprecated method Descriptor")
}
var op any = &testopaquepb.TestAllTypes{}
if _, ok := op.(interface{ Descriptor() ([]byte, []int) }); ok {
t.Errorf("Opaque V0 mode proto unexpectedly has deprecated Descriptor() method")
}
var hp any = &testhybridpb.TestAllTypes{}
if _, ok := hp.(interface{ EnumDescriptor() ([]byte, []int) }); ok {
t.Errorf("Hybrid proto did have deprecated method EnumDescriptor")
}
}
7 changes: 5 additions & 2 deletions cmd/protoc-gen-go/internal_gengo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func newEnumInfo(f *fileInfo, enum *protogen.Enum) *enumInfo {
e := &enumInfo{Enum: enum}
e.genJSONMethod = true
e.genRawDescMethod = true
opaqueNewEnumInfoHook(f, e)
return e
}

Expand All @@ -123,8 +124,9 @@ type messageInfo struct {
genRawDescMethod bool
genExtRangeMethod bool

isTracked bool
hasWeak bool
isTracked bool
noInterface bool
hasWeak bool
}

func newMessageInfo(f *fileInfo, message *protogen.Message) *messageInfo {
Expand All @@ -135,6 +137,7 @@ func newMessageInfo(f *fileInfo, message *protogen.Message) *messageInfo {
for _, field := range m.Fields {
m.hasWeak = m.hasWeak || field.Desc.IsWeak()
}
opaqueNewMessageInfoHook(f, m)
return m
}

Expand Down
33 changes: 33 additions & 0 deletions cmd/protoc-gen-go/internal_gengo/init_opaque.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package internal_gengo

import "google.golang.org/protobuf/types/gofeaturespb"

func (m *messageInfo) isOpen() bool {
return m.Message.APILevel == gofeaturespb.GoFeatures_API_OPEN
}

func (m *messageInfo) isHybrid() bool {
return m.Message.APILevel == gofeaturespb.GoFeatures_API_HYBRID
}

func (m *messageInfo) isOpaque() bool {
return m.Message.APILevel == gofeaturespb.GoFeatures_API_OPAQUE
}

func opaqueNewEnumInfoHook(f *fileInfo, e *enumInfo) {
if f.File.APILevel != gofeaturespb.GoFeatures_API_OPEN {
e.genJSONMethod = false
e.genRawDescMethod = false
}
}

func opaqueNewMessageInfoHook(f *fileInfo, m *messageInfo) {
if !m.isOpen() {
m.genRawDescMethod = false
m.genExtRangeMethod = false
}
}
5 changes: 4 additions & 1 deletion cmd/protoc-gen-go/internal_gengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ func genMessage(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo) {
if m.Desc.IsMapEntry() {
return
}
if opaqueGenMessageHook(g, f, m) {
return
}

// Message type declaration.
g.AnnotateSymbol(m.GoIdent.GoName, protogen.Annotation{Location: m.Location})
Expand Down Expand Up @@ -657,7 +660,7 @@ func genMessageSetterMethods(g *protogen.GeneratedFile, f *fileInfo, m *messageI
continue
}

genNoInterfacePragma(g, m.isTracked)
genNoInterfacePragma(g, m.noInterface)

g.AnnotateSymbol(m.GoIdent.GoName+".Set"+field.GoName, protogen.Annotation{
Location: field.Location,
Expand Down
Loading

0 comments on commit eb7b468

Please sign in to comment.