github.com/jhump/protoreflect@v1.16.0/desc/sourceinfo/wrappers.go (about)

     1  package sourceinfo
     2  
     3  import (
     4  	"fmt"
     5  	"google.golang.org/protobuf/reflect/protoreflect"
     6  )
     7  
     8  // These are wrappers around the various interfaces in the
     9  // google.golang.org/protobuf/reflect/protoreflect that all
    10  // make sure to return a FileDescriptor that includes source
    11  // code info.
    12  
    13  type fileDescriptor struct {
    14  	protoreflect.FileDescriptor
    15  	locs protoreflect.SourceLocations
    16  }
    17  
    18  func (f fileDescriptor) Edition() int32 {
    19  	ed, ok := f.FileDescriptor.(interface{ Edition() int32 })
    20  	if ok {
    21  		return ed.Edition()
    22  	}
    23  	return 0
    24  }
    25  
    26  func (f fileDescriptor) ParentFile() protoreflect.FileDescriptor {
    27  	return f
    28  }
    29  
    30  func (f fileDescriptor) Parent() protoreflect.Descriptor {
    31  	return nil
    32  }
    33  
    34  func (f fileDescriptor) Imports() protoreflect.FileImports {
    35  	return imports{f.FileDescriptor.Imports()}
    36  }
    37  
    38  func (f fileDescriptor) Messages() protoreflect.MessageDescriptors {
    39  	return messages{f.FileDescriptor.Messages()}
    40  }
    41  
    42  func (f fileDescriptor) Enums() protoreflect.EnumDescriptors {
    43  	return enums{f.FileDescriptor.Enums()}
    44  }
    45  
    46  func (f fileDescriptor) Extensions() protoreflect.ExtensionDescriptors {
    47  	return extensions{f.FileDescriptor.Extensions()}
    48  }
    49  
    50  func (f fileDescriptor) Services() protoreflect.ServiceDescriptors {
    51  	return services{f.FileDescriptor.Services()}
    52  }
    53  
    54  func (f fileDescriptor) SourceLocations() protoreflect.SourceLocations {
    55  	return f.locs
    56  }
    57  
    58  type imports struct {
    59  	protoreflect.FileImports
    60  }
    61  
    62  func (im imports) Get(i int) protoreflect.FileImport {
    63  	fi := im.FileImports.Get(i)
    64  	return protoreflect.FileImport{
    65  		FileDescriptor: getFile(fi.FileDescriptor),
    66  		IsPublic:       fi.IsPublic,
    67  		IsWeak:         fi.IsWeak,
    68  	}
    69  }
    70  
    71  type messages struct {
    72  	protoreflect.MessageDescriptors
    73  }
    74  
    75  func (m messages) Get(i int) protoreflect.MessageDescriptor {
    76  	return messageDescriptor{m.MessageDescriptors.Get(i)}
    77  }
    78  
    79  func (m messages) ByName(n protoreflect.Name) protoreflect.MessageDescriptor {
    80  	return messageDescriptor{m.MessageDescriptors.ByName(n)}
    81  }
    82  
    83  type enums struct {
    84  	protoreflect.EnumDescriptors
    85  }
    86  
    87  func (e enums) Get(i int) protoreflect.EnumDescriptor {
    88  	return enumDescriptor{e.EnumDescriptors.Get(i)}
    89  }
    90  
    91  func (e enums) ByName(n protoreflect.Name) protoreflect.EnumDescriptor {
    92  	return enumDescriptor{e.EnumDescriptors.ByName(n)}
    93  }
    94  
    95  type extensions struct {
    96  	protoreflect.ExtensionDescriptors
    97  }
    98  
    99  func (e extensions) Get(i int) protoreflect.ExtensionDescriptor {
   100  	d := e.ExtensionDescriptors.Get(i)
   101  	if ed, ok := d.(protoreflect.ExtensionTypeDescriptor); ok {
   102  		return extensionDescriptor{ed}
   103  	}
   104  	return fieldDescriptor{d}
   105  }
   106  
   107  func (e extensions) ByName(n protoreflect.Name) protoreflect.ExtensionDescriptor {
   108  	d := e.ExtensionDescriptors.ByName(n)
   109  	if ed, ok := d.(protoreflect.ExtensionTypeDescriptor); ok {
   110  		return extensionDescriptor{ed}
   111  	}
   112  	return fieldDescriptor{d}
   113  }
   114  
   115  type services struct {
   116  	protoreflect.ServiceDescriptors
   117  }
   118  
   119  func (s services) Get(i int) protoreflect.ServiceDescriptor {
   120  	return serviceDescriptor{s.ServiceDescriptors.Get(i)}
   121  }
   122  
   123  func (s services) ByName(n protoreflect.Name) protoreflect.ServiceDescriptor {
   124  	return serviceDescriptor{s.ServiceDescriptors.ByName(n)}
   125  }
   126  
   127  type messageDescriptor struct {
   128  	protoreflect.MessageDescriptor
   129  }
   130  
   131  func (m messageDescriptor) ParentFile() protoreflect.FileDescriptor {
   132  	return getFile(m.MessageDescriptor.ParentFile())
   133  }
   134  
   135  func (m messageDescriptor) Parent() protoreflect.Descriptor {
   136  	d := m.MessageDescriptor.Parent()
   137  	switch d := d.(type) {
   138  	case protoreflect.MessageDescriptor:
   139  		return messageDescriptor{d}
   140  	case protoreflect.FileDescriptor:
   141  		return getFile(d)
   142  	case nil:
   143  		return nil
   144  	default:
   145  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   146  	}
   147  }
   148  
   149  func (m messageDescriptor) Fields() protoreflect.FieldDescriptors {
   150  	return fields{m.MessageDescriptor.Fields()}
   151  }
   152  
   153  func (m messageDescriptor) Oneofs() protoreflect.OneofDescriptors {
   154  	return oneOfs{m.MessageDescriptor.Oneofs()}
   155  }
   156  
   157  func (m messageDescriptor) Enums() protoreflect.EnumDescriptors {
   158  	return enums{m.MessageDescriptor.Enums()}
   159  }
   160  
   161  func (m messageDescriptor) Messages() protoreflect.MessageDescriptors {
   162  	return messages{m.MessageDescriptor.Messages()}
   163  }
   164  
   165  func (m messageDescriptor) Extensions() protoreflect.ExtensionDescriptors {
   166  	return extensions{m.MessageDescriptor.Extensions()}
   167  }
   168  
   169  type fields struct {
   170  	protoreflect.FieldDescriptors
   171  }
   172  
   173  func (f fields) Get(i int) protoreflect.FieldDescriptor {
   174  	return fieldDescriptor{f.FieldDescriptors.Get(i)}
   175  }
   176  
   177  func (f fields) ByName(n protoreflect.Name) protoreflect.FieldDescriptor {
   178  	return fieldDescriptor{f.FieldDescriptors.ByName(n)}
   179  }
   180  
   181  func (f fields) ByJSONName(n string) protoreflect.FieldDescriptor {
   182  	return fieldDescriptor{f.FieldDescriptors.ByJSONName(n)}
   183  }
   184  
   185  func (f fields) ByTextName(n string) protoreflect.FieldDescriptor {
   186  	return fieldDescriptor{f.FieldDescriptors.ByTextName(n)}
   187  }
   188  
   189  func (f fields) ByNumber(n protoreflect.FieldNumber) protoreflect.FieldDescriptor {
   190  	return fieldDescriptor{f.FieldDescriptors.ByNumber(n)}
   191  }
   192  
   193  type oneOfs struct {
   194  	protoreflect.OneofDescriptors
   195  }
   196  
   197  func (o oneOfs) Get(i int) protoreflect.OneofDescriptor {
   198  	return oneOfDescriptor{o.OneofDescriptors.Get(i)}
   199  }
   200  
   201  func (o oneOfs) ByName(n protoreflect.Name) protoreflect.OneofDescriptor {
   202  	return oneOfDescriptor{o.OneofDescriptors.ByName(n)}
   203  }
   204  
   205  type fieldDescriptor struct {
   206  	protoreflect.FieldDescriptor
   207  }
   208  
   209  func (f fieldDescriptor) ParentFile() protoreflect.FileDescriptor {
   210  	return getFile(f.FieldDescriptor.ParentFile())
   211  }
   212  
   213  func (f fieldDescriptor) Parent() protoreflect.Descriptor {
   214  	d := f.FieldDescriptor.Parent()
   215  	switch d := d.(type) {
   216  	case protoreflect.MessageDescriptor:
   217  		return messageDescriptor{d}
   218  	case protoreflect.FileDescriptor:
   219  		return getFile(d)
   220  	case nil:
   221  		return nil
   222  	default:
   223  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   224  	}
   225  }
   226  
   227  func (f fieldDescriptor) MapKey() protoreflect.FieldDescriptor {
   228  	fd := f.FieldDescriptor.MapKey()
   229  	if fd == nil {
   230  		return nil
   231  	}
   232  	return fieldDescriptor{fd}
   233  }
   234  
   235  func (f fieldDescriptor) MapValue() protoreflect.FieldDescriptor {
   236  	fd := f.FieldDescriptor.MapValue()
   237  	if fd == nil {
   238  		return nil
   239  	}
   240  	return fieldDescriptor{fd}
   241  }
   242  
   243  func (f fieldDescriptor) DefaultEnumValue() protoreflect.EnumValueDescriptor {
   244  	ed := f.FieldDescriptor.DefaultEnumValue()
   245  	if ed == nil {
   246  		return nil
   247  	}
   248  	return enumValueDescriptor{ed}
   249  }
   250  
   251  func (f fieldDescriptor) ContainingOneof() protoreflect.OneofDescriptor {
   252  	od := f.FieldDescriptor.ContainingOneof()
   253  	if od == nil {
   254  		return nil
   255  	}
   256  	return oneOfDescriptor{od}
   257  }
   258  
   259  func (f fieldDescriptor) ContainingMessage() protoreflect.MessageDescriptor {
   260  	return messageDescriptor{f.FieldDescriptor.ContainingMessage()}
   261  }
   262  
   263  func (f fieldDescriptor) Enum() protoreflect.EnumDescriptor {
   264  	ed := f.FieldDescriptor.Enum()
   265  	if ed == nil {
   266  		return nil
   267  	}
   268  	return enumDescriptor{ed}
   269  }
   270  
   271  func (f fieldDescriptor) Message() protoreflect.MessageDescriptor {
   272  	md := f.FieldDescriptor.Message()
   273  	if md == nil {
   274  		return nil
   275  	}
   276  	return messageDescriptor{md}
   277  }
   278  
   279  type oneOfDescriptor struct {
   280  	protoreflect.OneofDescriptor
   281  }
   282  
   283  func (o oneOfDescriptor) ParentFile() protoreflect.FileDescriptor {
   284  	return getFile(o.OneofDescriptor.ParentFile())
   285  }
   286  
   287  func (o oneOfDescriptor) Parent() protoreflect.Descriptor {
   288  	d := o.OneofDescriptor.Parent()
   289  	switch d := d.(type) {
   290  	case protoreflect.MessageDescriptor:
   291  		return messageDescriptor{d}
   292  	case nil:
   293  		return nil
   294  	default:
   295  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   296  	}
   297  }
   298  
   299  func (o oneOfDescriptor) Fields() protoreflect.FieldDescriptors {
   300  	return fields{o.OneofDescriptor.Fields()}
   301  }
   302  
   303  type enumDescriptor struct {
   304  	protoreflect.EnumDescriptor
   305  }
   306  
   307  func (e enumDescriptor) ParentFile() protoreflect.FileDescriptor {
   308  	return getFile(e.EnumDescriptor.ParentFile())
   309  }
   310  
   311  func (e enumDescriptor) Parent() protoreflect.Descriptor {
   312  	d := e.EnumDescriptor.Parent()
   313  	switch d := d.(type) {
   314  	case protoreflect.MessageDescriptor:
   315  		return messageDescriptor{d}
   316  	case protoreflect.FileDescriptor:
   317  		return getFile(d)
   318  	case nil:
   319  		return nil
   320  	default:
   321  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   322  	}
   323  }
   324  
   325  func (e enumDescriptor) Values() protoreflect.EnumValueDescriptors {
   326  	return enumValues{e.EnumDescriptor.Values()}
   327  }
   328  
   329  type enumValues struct {
   330  	protoreflect.EnumValueDescriptors
   331  }
   332  
   333  func (e enumValues) Get(i int) protoreflect.EnumValueDescriptor {
   334  	return enumValueDescriptor{e.EnumValueDescriptors.Get(i)}
   335  }
   336  
   337  func (e enumValues) ByName(n protoreflect.Name) protoreflect.EnumValueDescriptor {
   338  	return enumValueDescriptor{e.EnumValueDescriptors.ByName(n)}
   339  }
   340  
   341  func (e enumValues) ByNumber(n protoreflect.EnumNumber) protoreflect.EnumValueDescriptor {
   342  	return enumValueDescriptor{e.EnumValueDescriptors.ByNumber(n)}
   343  }
   344  
   345  type enumValueDescriptor struct {
   346  	protoreflect.EnumValueDescriptor
   347  }
   348  
   349  func (e enumValueDescriptor) ParentFile() protoreflect.FileDescriptor {
   350  	return getFile(e.EnumValueDescriptor.ParentFile())
   351  }
   352  
   353  func (e enumValueDescriptor) Parent() protoreflect.Descriptor {
   354  	d := e.EnumValueDescriptor.Parent()
   355  	switch d := d.(type) {
   356  	case protoreflect.EnumDescriptor:
   357  		return enumDescriptor{d}
   358  	case nil:
   359  		return nil
   360  	default:
   361  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   362  	}
   363  }
   364  
   365  type extensionDescriptor struct {
   366  	protoreflect.ExtensionTypeDescriptor
   367  }
   368  
   369  func (e extensionDescriptor) ParentFile() protoreflect.FileDescriptor {
   370  	return getFile(e.ExtensionTypeDescriptor.ParentFile())
   371  }
   372  
   373  func (e extensionDescriptor) Parent() protoreflect.Descriptor {
   374  	d := e.ExtensionTypeDescriptor.Parent()
   375  	switch d := d.(type) {
   376  	case protoreflect.MessageDescriptor:
   377  		return messageDescriptor{d}
   378  	case protoreflect.FileDescriptor:
   379  		return getFile(d)
   380  	case nil:
   381  		return nil
   382  	default:
   383  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   384  	}
   385  }
   386  
   387  func (e extensionDescriptor) MapKey() protoreflect.FieldDescriptor {
   388  	fd := e.ExtensionTypeDescriptor.MapKey()
   389  	if fd == nil {
   390  		return nil
   391  	}
   392  	return fieldDescriptor{fd}
   393  }
   394  
   395  func (e extensionDescriptor) MapValue() protoreflect.FieldDescriptor {
   396  	fd := e.ExtensionTypeDescriptor.MapValue()
   397  	if fd == nil {
   398  		return nil
   399  	}
   400  	return fieldDescriptor{fd}
   401  }
   402  
   403  func (e extensionDescriptor) DefaultEnumValue() protoreflect.EnumValueDescriptor {
   404  	ed := e.ExtensionTypeDescriptor.DefaultEnumValue()
   405  	if ed == nil {
   406  		return nil
   407  	}
   408  	return enumValueDescriptor{ed}
   409  }
   410  
   411  func (e extensionDescriptor) ContainingOneof() protoreflect.OneofDescriptor {
   412  	od := e.ExtensionTypeDescriptor.ContainingOneof()
   413  	if od == nil {
   414  		return nil
   415  	}
   416  	return oneOfDescriptor{od}
   417  }
   418  
   419  func (e extensionDescriptor) ContainingMessage() protoreflect.MessageDescriptor {
   420  	return messageDescriptor{e.ExtensionTypeDescriptor.ContainingMessage()}
   421  }
   422  
   423  func (e extensionDescriptor) Enum() protoreflect.EnumDescriptor {
   424  	ed := e.ExtensionTypeDescriptor.Enum()
   425  	if ed == nil {
   426  		return nil
   427  	}
   428  	return enumDescriptor{ed}
   429  }
   430  
   431  func (e extensionDescriptor) Message() protoreflect.MessageDescriptor {
   432  	md := e.ExtensionTypeDescriptor.Message()
   433  	if md == nil {
   434  		return nil
   435  	}
   436  	return messageDescriptor{md}
   437  }
   438  
   439  func (e extensionDescriptor) Descriptor() protoreflect.ExtensionDescriptor {
   440  	return e
   441  }
   442  
   443  var _ protoreflect.ExtensionTypeDescriptor = extensionDescriptor{}
   444  
   445  type serviceDescriptor struct {
   446  	protoreflect.ServiceDescriptor
   447  }
   448  
   449  func (s serviceDescriptor) ParentFile() protoreflect.FileDescriptor {
   450  	return getFile(s.ServiceDescriptor.ParentFile())
   451  }
   452  
   453  func (s serviceDescriptor) Parent() protoreflect.Descriptor {
   454  	d := s.ServiceDescriptor.Parent()
   455  	switch d := d.(type) {
   456  	case protoreflect.FileDescriptor:
   457  		return getFile(d)
   458  	case nil:
   459  		return nil
   460  	default:
   461  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   462  	}
   463  }
   464  
   465  func (s serviceDescriptor) Methods() protoreflect.MethodDescriptors {
   466  	return methods{s.ServiceDescriptor.Methods()}
   467  }
   468  
   469  type methods struct {
   470  	protoreflect.MethodDescriptors
   471  }
   472  
   473  func (m methods) Get(i int) protoreflect.MethodDescriptor {
   474  	return methodDescriptor{m.MethodDescriptors.Get(i)}
   475  }
   476  
   477  func (m methods) ByName(n protoreflect.Name) protoreflect.MethodDescriptor {
   478  	return methodDescriptor{m.MethodDescriptors.ByName(n)}
   479  }
   480  
   481  type methodDescriptor struct {
   482  	protoreflect.MethodDescriptor
   483  }
   484  
   485  func (m methodDescriptor) ParentFile() protoreflect.FileDescriptor {
   486  	return getFile(m.MethodDescriptor.ParentFile())
   487  }
   488  
   489  func (m methodDescriptor) Parent() protoreflect.Descriptor {
   490  	d := m.MethodDescriptor.Parent()
   491  	switch d := d.(type) {
   492  	case protoreflect.ServiceDescriptor:
   493  		return serviceDescriptor{d}
   494  	case nil:
   495  		return nil
   496  	default:
   497  		panic(fmt.Sprintf("unexpected descriptor type %T", d))
   498  	}
   499  }
   500  
   501  func (m methodDescriptor) Input() protoreflect.MessageDescriptor {
   502  	return messageDescriptor{m.MethodDescriptor.Input()}
   503  }
   504  
   505  func (m methodDescriptor) Output() protoreflect.MessageDescriptor {
   506  	return messageDescriptor{m.MethodDescriptor.Output()}
   507  }
   508  
   509  type extensionType struct {
   510  	protoreflect.ExtensionType
   511  }
   512  
   513  func (e extensionType) TypeDescriptor() protoreflect.ExtensionTypeDescriptor {
   514  	return extensionDescriptor{e.ExtensionType.TypeDescriptor()}
   515  }
   516  
   517  type messageType struct {
   518  	protoreflect.MessageType
   519  }
   520  
   521  func (m messageType) Descriptor() protoreflect.MessageDescriptor {
   522  	return messageDescriptor{m.MessageType.Descriptor()}
   523  }
   524  
   525  // WrapFile wraps the given file descriptor so that it will include source
   526  // code info that was registered with this package if the given file was
   527  // processed with protoc-gen-gosrcinfo. Returns fd without wrapping if fd
   528  // already contains source code info.
   529  func WrapFile(fd protoreflect.FileDescriptor) protoreflect.FileDescriptor {
   530  	if wrapper, ok := fd.(fileDescriptor); ok {
   531  		// already wrapped
   532  		return wrapper
   533  	}
   534  	if fd.SourceLocations().Len() > 0 {
   535  		// no need to wrap since it includes source info already
   536  		return fd
   537  	}
   538  	return getFile(fd)
   539  }
   540  
   541  // WrapMessage wraps the given message descriptor so that it will include source
   542  // code info that was registered with this package if the file it is defined in
   543  // was processed with protoc-gen-gosrcinfo. Returns md without wrapping if md's
   544  // parent file already contains source code info.
   545  func WrapMessage(md protoreflect.MessageDescriptor) protoreflect.MessageDescriptor {
   546  	if wrapper, ok := md.(messageDescriptor); ok {
   547  		// already wrapped
   548  		return wrapper
   549  	}
   550  	if md.ParentFile().SourceLocations().Len() > 0 {
   551  		// no need to wrap since it includes source info already
   552  		return md
   553  	}
   554  	if !canWrap(md) {
   555  		return md
   556  	}
   557  	return messageDescriptor{md}
   558  }
   559  
   560  // WrapEnum wraps the given enum descriptor so that it will include source
   561  // code info that was registered with this package if the file it is defined in
   562  // was processed with protoc-gen-gosrcinfo. Returns ed without wrapping if ed's
   563  // parent file already contains source code info.
   564  func WrapEnum(ed protoreflect.EnumDescriptor) protoreflect.EnumDescriptor {
   565  	if wrapper, ok := ed.(enumDescriptor); ok {
   566  		// already wrapped
   567  		return wrapper
   568  	}
   569  	if ed.ParentFile().SourceLocations().Len() > 0 {
   570  		// no need to wrap since it includes source info already
   571  		return ed
   572  	}
   573  	if !canWrap(ed) {
   574  		return ed
   575  	}
   576  	return enumDescriptor{ed}
   577  }
   578  
   579  // WrapService wraps the given service descriptor so that it will include source
   580  // code info that was registered with this package if the file it is defined in
   581  // was processed with protoc-gen-gosrcinfo. Returns sd without wrapping if sd's
   582  // parent file already contains source code info.
   583  func WrapService(sd protoreflect.ServiceDescriptor) protoreflect.ServiceDescriptor {
   584  	if wrapper, ok := sd.(serviceDescriptor); ok {
   585  		// already wrapped
   586  		return wrapper
   587  	}
   588  	if sd.ParentFile().SourceLocations().Len() > 0 {
   589  		// no need to wrap since it includes source info already
   590  		return sd
   591  	}
   592  	if !canWrap(sd) {
   593  		return sd
   594  	}
   595  	return serviceDescriptor{sd}
   596  }
   597  
   598  // WrapExtensionType wraps the given extension type so that its associated
   599  // descriptor will include source code info that was registered with this package
   600  // if the file it is defined in was processed with protoc-gen-gosrcinfo. Returns
   601  // xt without wrapping if the parent file of xt's descriptor already contains
   602  // source code info.
   603  func WrapExtensionType(xt protoreflect.ExtensionType) protoreflect.ExtensionType {
   604  	if wrapper, ok := xt.(extensionType); ok {
   605  		// already wrapped
   606  		return wrapper
   607  	}
   608  	if xt.TypeDescriptor().ParentFile().SourceLocations().Len() > 0 {
   609  		// no need to wrap since it includes source info already
   610  		return xt
   611  	}
   612  	if !canWrap(xt.TypeDescriptor()) {
   613  		return xt
   614  	}
   615  	return extensionType{xt}
   616  }
   617  
   618  // WrapMessageType wraps the given message type so that its associated
   619  // descriptor will include source code info that was registered with this package
   620  // if the file it is defined in was processed with protoc-gen-gosrcinfo. Returns
   621  // mt without wrapping if the parent file of mt's descriptor already contains
   622  // source code info.
   623  func WrapMessageType(mt protoreflect.MessageType) protoreflect.MessageType {
   624  	if wrapper, ok := mt.(messageType); ok {
   625  		// already wrapped
   626  		return wrapper
   627  	}
   628  	if mt.Descriptor().ParentFile().SourceLocations().Len() > 0 {
   629  		// no need to wrap since it includes source info already
   630  		return mt
   631  	}
   632  	if !canWrap(mt.Descriptor()) {
   633  		return mt
   634  	}
   635  	return messageType{mt}
   636  }