editions

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package editions contains helpers related to resolving features for Protobuf editions. These are lower-level helpers. Higher-level helpers (which use this package under the hood) can be found in the exported protoutil package.

Index

Constants

This section is empty.

Variables

View Source
var (
	// AllowEditions is set to true in tests to enable editions syntax for testing.
	// This will be removed and editions will be allowed by non-test code once the
	// implementation is complete.
	AllowEditions = false

	// SupportedEditions is the exhaustive set of editions that protocompile
	// can support. We don't allow it to compile future/unknown editions, to
	// make sure we don't generate incorrect descriptors, in the event that
	// a future edition introduces a change or new feature that requires
	// new logic in the compiler.
	SupportedEditions = map[string]descriptorpb.Edition{
		"2023": descriptorpb.Edition_EDITION_2023,
	}

	// FeatureSetDescriptor is the message descriptor for the compiled-in
	// version (in the descriptorpb package) of the google.protobuf.FeatureSet
	// message type.
	FeatureSetDescriptor = (*descriptorpb.FeatureSet)(nil).ProtoReflect().Descriptor()
	// FeatureSetType is the message type for the compiled-in version (in
	// the descriptorpb package) of google.protobuf.FeatureSet.
	FeatureSetType = (*descriptorpb.FeatureSet)(nil).ProtoReflect().Type()
)

Functions

func GetEdition

GetEdition returns the edition for a given element. It returns EDITION_PROTO2 or EDITION_PROTO3 if the element is in a file that uses proto2 or proto3 syntax, respectively. It returns EDITION_UNKNOWN if the syntax of the given element is not recognized or if the edition cannot be ascertained from the element's protoreflect.FileDescriptor.

func GetEditionDefaults

func GetEditionDefaults(edition descriptorpb.Edition) *descriptorpb.FeatureSet

GetEditionDefaults returns the default feature values for the given edition. It returns nil if the given edition is not known.

This only populates known features, those that are fields of *descriptorpb.FeatureSet. It does not populate any extension fields.

The returned value must not be mutated as it references shared package state.

func GetFeatureDefault

func GetFeatureDefault(edition descriptorpb.Edition, container protoreflect.MessageType, feature protoreflect.FieldDescriptor) (protoreflect.Value, error)

GetFeatureDefault computes the default value for a feature. The given container is the message type that contains the field. This should usually be the descriptor for google.protobuf.FeatureSet, but can be a different message for computing the default value of custom features.

Note that this always re-computes the default. For known fields of FeatureSet, it is more efficient to query from the statically computed default messages, like so:

editions.GetEditionDefaults(edition).ProtoReflect().Get(feature)

func ResolveFeature

func ResolveFeature(
	element protoreflect.Descriptor,
	fields ...protoreflect.FieldDescriptor,
) (protoreflect.Value, error)

ResolveFeature resolves a feature for the given descriptor. This simple helper examines the given element and its ancestors, searching for an override. If there is no overridden value, it returns a zero value.

Types

type HasEdition

type HasEdition interface {
	// Edition returns the numeric value of a google.protobuf.Edition enum
	// value that corresponds to the edition of this file. If the file does
	// not use editions, it should return the enum value that corresponds
	// to the syntax level, EDITION_PROTO2 or EDITION_PROTO3.
	Edition() int32
}

HasEdition should be implemented by values that implement protoreflect.FileDescriptor, to provide access to the file's edition when its syntax is protoreflect.Editions.

type HasFeatures

type HasFeatures interface {
	GetFeatures() *descriptorpb.FeatureSet
}

HasFeatures is implemented by all options messages and provides a nil-receiver-safe way of accessing the features explicitly configured in those options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL