...

Source file src/google.golang.org/protobuf/reflect/protodesc/desc.go

Documentation: google.golang.org/protobuf/reflect/protodesc

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package protodesc provides functionality for converting
     6  // FileDescriptorProto messages to/from [protoreflect.FileDescriptor] values.
     7  //
     8  // The google.protobuf.FileDescriptorProto is a protobuf message that describes
     9  // the type information for a .proto file in a form that is easily serializable.
    10  // The [protoreflect.FileDescriptor] is a more structured representation of
    11  // the FileDescriptorProto message where references and remote dependencies
    12  // can be directly followed.
    13  package protodesc
    14  
    15  import (
    16  	"google.golang.org/protobuf/internal/errors"
    17  	"google.golang.org/protobuf/internal/filedesc"
    18  	"google.golang.org/protobuf/internal/pragma"
    19  	"google.golang.org/protobuf/internal/strs"
    20  	"google.golang.org/protobuf/proto"
    21  	"google.golang.org/protobuf/reflect/protoreflect"
    22  	"google.golang.org/protobuf/reflect/protoregistry"
    23  
    24  	"google.golang.org/protobuf/types/descriptorpb"
    25  )
    26  
    27  // Resolver is the resolver used by [NewFile] to resolve dependencies.
    28  // The enums and messages provided must belong to some parent file,
    29  // which is also registered.
    30  //
    31  // It is implemented by [protoregistry.Files].
    32  type Resolver interface {
    33  	FindFileByPath(string) (protoreflect.FileDescriptor, error)
    34  	FindDescriptorByName(protoreflect.FullName) (protoreflect.Descriptor, error)
    35  }
    36  
    37  // FileOptions configures the construction of file descriptors.
    38  type FileOptions struct {
    39  	pragma.NoUnkeyedLiterals
    40  
    41  	// AllowUnresolvable configures New to permissively allow unresolvable
    42  	// file, enum, or message dependencies. Unresolved dependencies are replaced
    43  	// by placeholder equivalents.
    44  	//
    45  	// The following dependencies may be left unresolved:
    46  	//	• Resolving an imported file.
    47  	//	• Resolving the type for a message field or extension field.
    48  	//	If the kind of the field is unknown, then a placeholder is used for both
    49  	//	the Enum and Message accessors on the protoreflect.FieldDescriptor.
    50  	//	• Resolving an enum value set as the default for an optional enum field.
    51  	//	If unresolvable, the protoreflect.FieldDescriptor.Default is set to the
    52  	//	first value in the associated enum (or zero if the also enum dependency
    53  	//	is also unresolvable). The protoreflect.FieldDescriptor.DefaultEnumValue
    54  	//	is populated with a placeholder.
    55  	//	• Resolving the extended message type for an extension field.
    56  	//	• Resolving the input or output message type for a service method.
    57  	//
    58  	// If the unresolved dependency uses a relative name,
    59  	// then the placeholder will contain an invalid FullName with a "*." prefix,
    60  	// indicating that the starting prefix of the full name is unknown.
    61  	AllowUnresolvable bool
    62  }
    63  
    64  // NewFile creates a new [protoreflect.FileDescriptor] from the provided
    65  // file descriptor message. See [FileOptions.New] for more information.
    66  func NewFile(fd *descriptorpb.FileDescriptorProto, r Resolver) (protoreflect.FileDescriptor, error) {
    67  	return FileOptions{}.New(fd, r)
    68  }
    69  
    70  // NewFiles creates a new [protoregistry.Files] from the provided
    71  // FileDescriptorSet message. See [FileOptions.NewFiles] for more information.
    72  func NewFiles(fd *descriptorpb.FileDescriptorSet) (*protoregistry.Files, error) {
    73  	return FileOptions{}.NewFiles(fd)
    74  }
    75  
    76  // New creates a new [protoreflect.FileDescriptor] from the provided
    77  // file descriptor message. The file must represent a valid proto file according
    78  // to protobuf semantics. The returned descriptor is a deep copy of the input.
    79  //
    80  // Any imported files, enum types, or message types referenced in the file are
    81  // resolved using the provided registry. When looking up an import file path,
    82  // the path must be unique. The newly created file descriptor is not registered
    83  // back into the provided file registry.
    84  func (o FileOptions) New(fd *descriptorpb.FileDescriptorProto, r Resolver) (protoreflect.FileDescriptor, error) {
    85  	if r == nil {
    86  		r = (*protoregistry.Files)(nil) // empty resolver
    87  	}
    88  
    89  	// Handle the file descriptor content.
    90  	f := &filedesc.File{L2: &filedesc.FileL2{}}
    91  	switch fd.GetSyntax() {
    92  	case "proto2", "":
    93  		f.L1.Syntax = protoreflect.Proto2
    94  	case "proto3":
    95  		f.L1.Syntax = protoreflect.Proto3
    96  	case "editions":
    97  		f.L1.Syntax = protoreflect.Editions
    98  		f.L1.Edition = fromEditionProto(fd.GetEdition())
    99  	default:
   100  		return nil, errors.New("invalid syntax: %q", fd.GetSyntax())
   101  	}
   102  	if f.L1.Syntax == protoreflect.Editions && (fd.GetEdition() < SupportedEditionsMinimum || fd.GetEdition() > SupportedEditionsMaximum) {
   103  		return nil, errors.New("use of edition %v not yet supported by the Go Protobuf runtime", fd.GetEdition())
   104  	}
   105  	f.L1.Path = fd.GetName()
   106  	if f.L1.Path == "" {
   107  		return nil, errors.New("file path must be populated")
   108  	}
   109  	f.L1.Package = protoreflect.FullName(fd.GetPackage())
   110  	if !f.L1.Package.IsValid() && f.L1.Package != "" {
   111  		return nil, errors.New("invalid package: %q", f.L1.Package)
   112  	}
   113  	if opts := fd.GetOptions(); opts != nil {
   114  		opts = proto.Clone(opts).(*descriptorpb.FileOptions)
   115  		f.L2.Options = func() protoreflect.ProtoMessage { return opts }
   116  	}
   117  	if f.L1.Syntax == protoreflect.Editions {
   118  		initFileDescFromFeatureSet(f, fd.GetOptions().GetFeatures())
   119  	}
   120  
   121  	f.L2.Imports = make(filedesc.FileImports, len(fd.GetDependency()))
   122  	for _, i := range fd.GetPublicDependency() {
   123  		if !(0 <= i && int(i) < len(f.L2.Imports)) || f.L2.Imports[i].IsPublic {
   124  			return nil, errors.New("invalid or duplicate public import index: %d", i)
   125  		}
   126  		f.L2.Imports[i].IsPublic = true
   127  	}
   128  	for _, i := range fd.GetWeakDependency() {
   129  		if !(0 <= i && int(i) < len(f.L2.Imports)) || f.L2.Imports[i].IsWeak {
   130  			return nil, errors.New("invalid or duplicate weak import index: %d", i)
   131  		}
   132  		f.L2.Imports[i].IsWeak = true
   133  	}
   134  	imps := importSet{f.Path(): true}
   135  	for i, path := range fd.GetDependency() {
   136  		imp := &f.L2.Imports[i]
   137  		f, err := r.FindFileByPath(path)
   138  		if err == protoregistry.NotFound && (o.AllowUnresolvable || imp.IsWeak) {
   139  			f = filedesc.PlaceholderFile(path)
   140  		} else if err != nil {
   141  			return nil, errors.New("could not resolve import %q: %v", path, err)
   142  		}
   143  		imp.FileDescriptor = f
   144  
   145  		if imps[imp.Path()] {
   146  			return nil, errors.New("already imported %q", path)
   147  		}
   148  		imps[imp.Path()] = true
   149  	}
   150  	for i := range fd.GetDependency() {
   151  		imp := &f.L2.Imports[i]
   152  		imps.importPublic(imp.Imports())
   153  	}
   154  
   155  	// Handle source locations.
   156  	f.L2.Locations.File = f
   157  	for _, loc := range fd.GetSourceCodeInfo().GetLocation() {
   158  		var l protoreflect.SourceLocation
   159  		// TODO: Validate that the path points to an actual declaration?
   160  		l.Path = protoreflect.SourcePath(loc.GetPath())
   161  		s := loc.GetSpan()
   162  		switch len(s) {
   163  		case 3:
   164  			l.StartLine, l.StartColumn, l.EndLine, l.EndColumn = int(s[0]), int(s[1]), int(s[0]), int(s[2])
   165  		case 4:
   166  			l.StartLine, l.StartColumn, l.EndLine, l.EndColumn = int(s[0]), int(s[1]), int(s[2]), int(s[3])
   167  		default:
   168  			return nil, errors.New("invalid span: %v", s)
   169  		}
   170  		// TODO: Validate that the span information is sensible?
   171  		// See https://github.com/protocolbuffers/protobuf/issues/6378.
   172  		if false && (l.EndLine < l.StartLine || l.StartLine < 0 || l.StartColumn < 0 || l.EndColumn < 0 ||
   173  			(l.StartLine == l.EndLine && l.EndColumn <= l.StartColumn)) {
   174  			return nil, errors.New("invalid span: %v", s)
   175  		}
   176  		l.LeadingDetachedComments = loc.GetLeadingDetachedComments()
   177  		l.LeadingComments = loc.GetLeadingComments()
   178  		l.TrailingComments = loc.GetTrailingComments()
   179  		f.L2.Locations.List = append(f.L2.Locations.List, l)
   180  	}
   181  
   182  	// Step 1: Allocate and derive the names for all declarations.
   183  	// This copies all fields from the descriptor proto except:
   184  	//	google.protobuf.FieldDescriptorProto.type_name
   185  	//	google.protobuf.FieldDescriptorProto.default_value
   186  	//	google.protobuf.FieldDescriptorProto.oneof_index
   187  	//	google.protobuf.FieldDescriptorProto.extendee
   188  	//	google.protobuf.MethodDescriptorProto.input
   189  	//	google.protobuf.MethodDescriptorProto.output
   190  	var err error
   191  	sb := new(strs.Builder)
   192  	r1 := make(descsByName)
   193  	if f.L1.Enums.List, err = r1.initEnumDeclarations(fd.GetEnumType(), f, sb); err != nil {
   194  		return nil, err
   195  	}
   196  	if f.L1.Messages.List, err = r1.initMessagesDeclarations(fd.GetMessageType(), f, sb); err != nil {
   197  		return nil, err
   198  	}
   199  	if f.L1.Extensions.List, err = r1.initExtensionDeclarations(fd.GetExtension(), f, sb); err != nil {
   200  		return nil, err
   201  	}
   202  	if f.L1.Services.List, err = r1.initServiceDeclarations(fd.GetService(), f, sb); err != nil {
   203  		return nil, err
   204  	}
   205  
   206  	// Step 2: Resolve every dependency reference not handled by step 1.
   207  	r2 := &resolver{local: r1, remote: r, imports: imps, allowUnresolvable: o.AllowUnresolvable}
   208  	if err := r2.resolveMessageDependencies(f.L1.Messages.List, fd.GetMessageType()); err != nil {
   209  		return nil, err
   210  	}
   211  	if err := r2.resolveExtensionDependencies(f.L1.Extensions.List, fd.GetExtension()); err != nil {
   212  		return nil, err
   213  	}
   214  	if err := r2.resolveServiceDependencies(f.L1.Services.List, fd.GetService()); err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	// Step 3: Validate every enum, message, and extension declaration.
   219  	if err := validateEnumDeclarations(f.L1.Enums.List, fd.GetEnumType()); err != nil {
   220  		return nil, err
   221  	}
   222  	if err := validateMessageDeclarations(f.L1.Messages.List, fd.GetMessageType()); err != nil {
   223  		return nil, err
   224  	}
   225  	if err := validateExtensionDeclarations(f.L1.Extensions.List, fd.GetExtension()); err != nil {
   226  		return nil, err
   227  	}
   228  
   229  	return f, nil
   230  }
   231  
   232  type importSet map[string]bool
   233  
   234  func (is importSet) importPublic(imps protoreflect.FileImports) {
   235  	for i := 0; i < imps.Len(); i++ {
   236  		if imp := imps.Get(i); imp.IsPublic {
   237  			is[imp.Path()] = true
   238  			is.importPublic(imp.Imports())
   239  		}
   240  	}
   241  }
   242  
   243  // NewFiles creates a new [protoregistry.Files] from the provided
   244  // FileDescriptorSet message. The descriptor set must include only
   245  // valid files according to protobuf semantics. The returned descriptors
   246  // are a deep copy of the input.
   247  func (o FileOptions) NewFiles(fds *descriptorpb.FileDescriptorSet) (*protoregistry.Files, error) {
   248  	files := make(map[string]*descriptorpb.FileDescriptorProto)
   249  	for _, fd := range fds.File {
   250  		if _, ok := files[fd.GetName()]; ok {
   251  			return nil, errors.New("file appears multiple times: %q", fd.GetName())
   252  		}
   253  		files[fd.GetName()] = fd
   254  	}
   255  	r := &protoregistry.Files{}
   256  	for _, fd := range files {
   257  		if err := o.addFileDeps(r, fd, files); err != nil {
   258  			return nil, err
   259  		}
   260  	}
   261  	return r, nil
   262  }
   263  func (o FileOptions) addFileDeps(r *protoregistry.Files, fd *descriptorpb.FileDescriptorProto, files map[string]*descriptorpb.FileDescriptorProto) error {
   264  	// Set the entry to nil while descending into a file's dependencies to detect cycles.
   265  	files[fd.GetName()] = nil
   266  	for _, dep := range fd.Dependency {
   267  		depfd, ok := files[dep]
   268  		if depfd == nil {
   269  			if ok {
   270  				return errors.New("import cycle in file: %q", dep)
   271  			}
   272  			continue
   273  		}
   274  		if err := o.addFileDeps(r, depfd, files); err != nil {
   275  			return err
   276  		}
   277  	}
   278  	// Delete the entry once dependencies are processed.
   279  	delete(files, fd.GetName())
   280  	f, err := o.New(fd, r)
   281  	if err != nil {
   282  		return err
   283  	}
   284  	return r.RegisterFile(f)
   285  }
   286  

View as plain text