Skip to content

Commit

Permalink
reflect/protodesc: support editions feature resolution
Browse files Browse the repository at this point in the history
This is the first of multiple changes to add protobuf editions support
to the Go implementation.
This change includes the feature resolutions for protobuf editions.
The plugin does not yet report editions support and protoc would fail
if it invoked this plugin with a proto that uses editions.

Change-Id: I7d31909366c3433b21abab74ec92263e08145434
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/547315
Auto-Submit: Lasse Folger <lassefolger@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Lasse Folger <lassefolger@google.com>
Reviewed-by: Lasse Folger <lassefolger@google.com>
  • Loading branch information
lfolger authored and gopherbot committed Dec 8, 2023
1 parent 3bfc0b0 commit e8baad6
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 3 deletions.
23 changes: 23 additions & 0 deletions internal/cmd/generate-protos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/internal/detrand"
"google.golang.org/protobuf/reflect/protodesc"
)

func init() {
Expand Down Expand Up @@ -89,6 +90,28 @@ func main() {

generateLocalProtos()
generateRemoteProtos()
generateEditionsDefaults()
}

func generateEditionsDefaults() {
dest := filepath.Join(repoRoot, "reflect", "protodesc", "editions_defaults.binpb")
// The enum in Go string formats to "EDITION_${EDITION}" but protoc expects
// the flag in the form "${EDITION}". To work around this, we trim the
// "EDITION_" prefix.
minEdition := strings.TrimPrefix(fmt.Sprint(protodesc.SupportedEditionsMinimum), "EDITION_")
maxEdition := strings.TrimPrefix(fmt.Sprint(protodesc.SupportedEditionsMaximum), "EDITION_")
cmd := exec.Command(
"protoc",
"--experimental_edition_defaults_out", dest,
"--experimental_edition_defaults_minimum", minEdition,
"--experimental_edition_defaults_maximum", maxEdition,
"--proto_path", protoRoot, "src/google/protobuf/descriptor.proto",
)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("executing: %v\n%s\n", strings.Join(cmd.Args, " "), out)
}
check(err)
}

func generateLocalProtos() {
Expand Down
47 changes: 46 additions & 1 deletion internal/filedesc/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@ import (
"google.golang.org/protobuf/reflect/protoregistry"
)

// Edition is an Enum for proto2.Edition
type Edition int32

// These values align with the value of Enum in descriptor.proto which allows
// direct conversion between the proto enum and this enum.
const (
EditionUnknown Edition = 0
EditionProto2 Edition = 998
EditionProto3 Edition = 999
Edition2023 Edition = 1000
EditionUnsupported Edition = 100000
)

// The types in this file may have a suffix:
// • L0: Contains fields common to all descriptors (except File) and
// must be initialized up front.
// • L1: Contains fields specific to a descriptor and
// must be initialized up front.
// must be initialized up front. If the associated proto uses Editions, the
// Editions features must always be resolved. If not explicitly set, the
// appropriate default must be resolved and set.
// • L2: Contains fields that are lazily initialized when constructing
// from the raw file descriptor. When constructing as a literal, the L2
// fields must be initialized up front.
Expand All @@ -44,19 +59,43 @@ type (
}
FileL1 struct {
Syntax protoreflect.Syntax
Edition Edition // Only used if Syntax == Editions
Path string
Package protoreflect.FullName

Enums Enums
Messages Messages
Extensions Extensions
Services Services

EditionFeatures FileEditionFeatures
}
FileL2 struct {
Options func() protoreflect.ProtoMessage
Imports FileImports
Locations SourceLocations
}

FileEditionFeatures struct {
// IsFieldPresence is true if field_presence is EXPLICIT
// https://protobuf.dev/editions/features/#field_presence
IsFieldPresence bool
// IsOpenEnum is true if enum_type is OPEN
// https://protobuf.dev/editions/features/#enum_type
IsOpenEnum bool
// IsPacked is true if repeated_field_encoding is PACKED
// https://protobuf.dev/editions/features/#repeated_field_encoding
IsPacked bool
// IsUTF8Validated is true if utf_validation is VERIFY
// https://protobuf.dev/editions/features/#utf8_validation
IsUTF8Validated bool
// IsDelimitedEncoded is true if message_encoding is DELIMITED
// https://protobuf.dev/editions/features/#message_encoding
IsDelimitedEncoded bool
// IsJSONCompliant is true if json_format is ALLOW
// https://protobuf.dev/editions/features/#json_format
IsJSONCompliant bool
}
)

func (fd *File) ParentFile() protoreflect.FileDescriptor { return fd }
Expand Down Expand Up @@ -210,6 +249,9 @@ type (
ContainingOneof protoreflect.OneofDescriptor // must be consistent with Message.Oneofs.Fields
Enum protoreflect.EnumDescriptor
Message protoreflect.MessageDescriptor

// Edition features.
Presence bool
}

Oneof struct {
Expand Down Expand Up @@ -273,6 +315,9 @@ func (fd *Field) HasJSONName() bool { return fd.L1.StringNam
func (fd *Field) JSONName() string { return fd.L1.StringName.getJSON(fd) }
func (fd *Field) TextName() string { return fd.L1.StringName.getText(fd) }
func (fd *Field) HasPresence() bool {
if fd.L0.ParentFile.L1.Syntax == protoreflect.Editions {
return fd.L1.Presence || fd.L1.Message != nil || fd.L1.ContainingOneof != nil
}
return fd.L1.Cardinality != protoreflect.Repeated && (fd.L0.ParentFile.L1.Syntax == protoreflect.Proto2 || fd.L1.Message != nil || fd.L1.ContainingOneof != nil)
}
func (fd *Field) HasOptionalKeyword() bool {
Expand Down
9 changes: 9 additions & 0 deletions reflect/protodesc/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ func (o FileOptions) New(fd *descriptorpb.FileDescriptorProto, r Resolver) (prot
f.L1.Syntax = protoreflect.Proto2
case "proto3":
f.L1.Syntax = protoreflect.Proto3
case "editions":
f.L1.Syntax = protoreflect.Editions
f.L1.Edition = fromEditionProto(fd.GetEdition())
default:
return nil, errors.New("invalid syntax: %q", fd.GetSyntax())
}
if f.L1.Syntax == protoreflect.Editions && (fd.GetEdition() < SupportedEditionsMinimum || fd.GetEdition() > SupportedEditionsMaximum) {
return nil, errors.New("use of edition %v not yet supported by the Go Protobuf runtime", fd.GetEdition())
}
f.L1.Path = fd.GetName()
if f.L1.Path == "" {
return nil, errors.New("file path must be populated")
Expand All @@ -108,6 +114,9 @@ func (o FileOptions) New(fd *descriptorpb.FileDescriptorProto, r Resolver) (prot
opts = proto.Clone(opts).(*descriptorpb.FileOptions)
f.L2.Options = func() protoreflect.ProtoMessage { return opts }
}
if f.L1.Syntax == protoreflect.Editions {
initFileDescFromFeatureSet(f, fd.GetOptions().GetFeatures())
}

f.L2.Imports = make(filedesc.FileImports, len(fd.GetDependency()))
for _, i := range fd.GetPublicDependency() {
Expand Down
24 changes: 24 additions & 0 deletions reflect/protodesc/desc_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ func (r descsByName) initFieldsFromDescriptorProto(fds []*descriptorpb.FieldDesc
if fd.JsonName != nil {
f.L1.StringName.InitJSON(fd.GetJsonName())
}

if f.Base.L0.ParentFile.Syntax() == protoreflect.Editions {
f.L1.Presence = resolveFeatureHasFieldPresence(f.Base.L0.ParentFile, fd)
// We reuse the existing field because the old option `[packed =
// true]` is mutually exclusive with the editions feature.
if fd.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
f.L1.HasPacked = true
f.L1.IsPacked = resolveFeatureRepeatedFieldEncodingPacked(f.Base.L0.ParentFile, fd)
}

// We pretend this option is always explicitly set because the only
// use of HasEnforceUTF8 is to determine whether to use EnforceUTF8
// or to return the appropriate default.
// When using editions we either parse the option or resolve the
// appropriate default here (instead of later when this option is
// requested from the descriptor).
// In proto2/proto3 syntax HasEnforceUTF8 might be false.
f.L1.HasEnforceUTF8 = true
f.L1.EnforceUTF8 = resolveFeatureEnforceUTF8(f.Base.L0.ParentFile, fd)

if f.L1.Kind == protoreflect.MessageKind && resolveFeatureDelimitedEncoding(f.Base.L0.ParentFile, fd) {
f.L1.Kind = protoreflect.GroupKind
}
}
}
return fs, nil
}
Expand Down
177 changes: 177 additions & 0 deletions reflect/protodesc/editions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright 2019 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 protodesc

import (
_ "embed"
"fmt"
"os"
"sync"

"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)

const (
SupportedEditionsMinimum = descriptorpb.Edition_EDITION_PROTO2
SupportedEditionsMaximum = descriptorpb.Edition_EDITION_2023
)

//go:embed editions_defaults.binpb
var binaryEditionDefaults []byte
var defaults = &descriptorpb.FeatureSetDefaults{}
var defaultsCacheMu sync.Mutex
var defaultsCache = make(map[filedesc.Edition]*descriptorpb.FeatureSet)

func init() {
err := proto.Unmarshal(binaryEditionDefaults, defaults)
if err != nil {
fmt.Fprintf(os.Stderr, "unmarshal editions defaults: %v\n", err)
os.Exit(1)
}
}

func fromEditionProto(epb descriptorpb.Edition) filedesc.Edition {
return filedesc.Edition(epb)
}

func toEditionProto(ed filedesc.Edition) descriptorpb.Edition {
switch ed {
case filedesc.EditionUnknown:
return descriptorpb.Edition_EDITION_UNKNOWN
case filedesc.EditionProto2:
return descriptorpb.Edition_EDITION_PROTO2
case filedesc.EditionProto3:
return descriptorpb.Edition_EDITION_PROTO3
case filedesc.Edition2023:
return descriptorpb.Edition_EDITION_2023
default:
panic(fmt.Sprintf("unknown value for edition: %v", ed))
}
}

func getFeatureSetFor(ed filedesc.Edition) *descriptorpb.FeatureSet {
defaultsCacheMu.Lock()
defer defaultsCacheMu.Unlock()
if def, ok := defaultsCache[ed]; ok {
return def
}
edpb := toEditionProto(ed)
if defaults.GetMinimumEdition() > edpb || defaults.GetMaximumEdition() < edpb {
// This should never happen protodesc.(FileOptions).New would fail when
// initializing the file descriptor.
// This most likely means the embedded defaults were not updated.
fmt.Fprintf(os.Stderr, "internal error: unsupported edition %v (did you forget to update the embedded defaults (i.e. the bootstrap descriptor proto)?)\n", edpb)
os.Exit(1)
}
fs := defaults.GetDefaults()[0].GetFeatures()
// Using a linear search for now.
// Editions are guaranteed to be sorted and thus we could use a binary search.
// Given that there are only a handful of editions (with one more per year)
// there is not much reason to use a binary search.
for _, def := range defaults.GetDefaults() {
if def.GetEdition() <= edpb {
fs = def.GetFeatures()
} else {
break
}
}
defaultsCache[ed] = fs
return fs
}

func resolveFeatureHasFieldPresence(fileDesc *filedesc.File, fieldDesc *descriptorpb.FieldDescriptorProto) bool {
fs := fieldDesc.GetOptions().GetFeatures()
if fs == nil || fs.FieldPresence == nil {
return fileDesc.L1.EditionFeatures.IsFieldPresence
}
return fs.GetFieldPresence() == descriptorpb.FeatureSet_LEGACY_REQUIRED ||
fs.GetFieldPresence() == descriptorpb.FeatureSet_EXPLICIT
}

func resolveFeatureRepeatedFieldEncodingPacked(fileDesc *filedesc.File, fieldDesc *descriptorpb.FieldDescriptorProto) bool {
fs := fieldDesc.GetOptions().GetFeatures()
if fs == nil || fs.RepeatedFieldEncoding == nil {
return fileDesc.L1.EditionFeatures.IsPacked
}
return fs.GetRepeatedFieldEncoding() == descriptorpb.FeatureSet_PACKED
}

func resolveFeatureEnforceUTF8(fileDesc *filedesc.File, fieldDesc *descriptorpb.FieldDescriptorProto) bool {
fs := fieldDesc.GetOptions().GetFeatures()
if fs == nil || fs.Utf8Validation == nil {
return fileDesc.L1.EditionFeatures.IsUTF8Validated
}
return fs.GetUtf8Validation() == descriptorpb.FeatureSet_VERIFY
}

func resolveFeatureDelimitedEncoding(fileDesc *filedesc.File, fieldDesc *descriptorpb.FieldDescriptorProto) bool {
fs := fieldDesc.GetOptions().GetFeatures()
if fs == nil || fs.MessageEncoding == nil {
return fileDesc.L1.EditionFeatures.IsDelimitedEncoded
}
return fs.GetMessageEncoding() == descriptorpb.FeatureSet_DELIMITED
}

// initFileDescFromFeatureSet initializes editions related fields in fd based
// on fs. If fs is nil it is assumed to be an empty featureset and all fields
// will be initialized with the appropriate default. fd.L1.Edition must be set
// before calling this function.
func initFileDescFromFeatureSet(fd *filedesc.File, fs *descriptorpb.FeatureSet) {
dfs := getFeatureSetFor(fd.L1.Edition)
if fs == nil {
fs = &descriptorpb.FeatureSet{}
}

var fieldPresence descriptorpb.FeatureSet_FieldPresence
if fp := fs.FieldPresence; fp != nil {
fieldPresence = *fp
} else {
fieldPresence = *dfs.FieldPresence
}
fd.L1.EditionFeatures.IsFieldPresence = fieldPresence == descriptorpb.FeatureSet_LEGACY_REQUIRED ||
fieldPresence == descriptorpb.FeatureSet_EXPLICIT

var enumType descriptorpb.FeatureSet_EnumType
if et := fs.EnumType; et != nil {
enumType = *et
} else {
enumType = *dfs.EnumType
}
fd.L1.EditionFeatures.IsOpenEnum = enumType == descriptorpb.FeatureSet_OPEN

var respeatedFieldEncoding descriptorpb.FeatureSet_RepeatedFieldEncoding
if rfe := fs.RepeatedFieldEncoding; rfe != nil {
respeatedFieldEncoding = *rfe
} else {
respeatedFieldEncoding = *dfs.RepeatedFieldEncoding
}
fd.L1.EditionFeatures.IsPacked = respeatedFieldEncoding == descriptorpb.FeatureSet_PACKED

var isUTF8Validated descriptorpb.FeatureSet_Utf8Validation
if utf8val := fs.Utf8Validation; utf8val != nil {
isUTF8Validated = *utf8val
} else {
isUTF8Validated = *dfs.Utf8Validation
}
fd.L1.EditionFeatures.IsUTF8Validated = isUTF8Validated == descriptorpb.FeatureSet_VERIFY

var messageEncoding descriptorpb.FeatureSet_MessageEncoding
if me := fs.MessageEncoding; me != nil {
messageEncoding = *me
} else {
messageEncoding = *dfs.MessageEncoding
}
fd.L1.EditionFeatures.IsDelimitedEncoded = messageEncoding == descriptorpb.FeatureSet_DELIMITED

var jsonFormat descriptorpb.FeatureSet_JsonFormat
if jf := fs.JsonFormat; jf != nil {
jsonFormat = *jf
} else {
jsonFormat = *dfs.JsonFormat
}
fd.L1.EditionFeatures.IsJSONCompliant = jsonFormat == descriptorpb.FeatureSet_ALLOW
}
4 changes: 4 additions & 0 deletions reflect/protodesc/editions_defaults.binpb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

  (0�
  (0�
  (0� �(�
5 changes: 3 additions & 2 deletions reflect/protoreflect/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ type Syntax syntax
type syntax int8 // keep exact type opaque as the int type may change

const (
Proto2 Syntax = 2
Proto3 Syntax = 3
Proto2 Syntax = 2
Proto3 Syntax = 3
Editions Syntax = 4
)

// IsValid reports whether the syntax is valid.
Expand Down

0 comments on commit e8baad6

Please sign in to comment.