github.com/jhump/protocompile@v0.0.0-20221021153901-4f6f732835e8/linker/descriptors.go (about)

     1  package linker
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"google.golang.org/protobuf/proto"
     9  	"google.golang.org/protobuf/reflect/protoreflect"
    10  	"google.golang.org/protobuf/types/descriptorpb"
    11  	"google.golang.org/protobuf/types/dynamicpb"
    12  
    13  	"github.com/jhump/protocompile/internal"
    14  	"github.com/jhump/protocompile/parser"
    15  )
    16  
    17  // This file contains implementations of protoreflect.Descriptor. Note that
    18  // this is a hack since those interfaces have a "doNotImplement" tag
    19  // interface therein. We do just enough to make dynamicpb happy; constructing
    20  // a regular descriptor would fail because we haven't yet interpreted options
    21  // at the point we need these, and some validations will fail if the options
    22  // aren't present.
    23  
    24  type result struct {
    25  	protoreflect.FileDescriptor
    26  	parser.Result
    27  	prefix         string
    28  	descriptorPool map[string]proto.Message
    29  	deps           Files
    30  	descriptors    map[proto.Message]protoreflect.Descriptor
    31  	usedImports    map[string]struct{}
    32  	optionBytes    map[proto.Message][]byte
    33  	srcLocs        []protoreflect.SourceLocation
    34  	srcLocIndex    map[interface{}]protoreflect.SourceLocation
    35  }
    36  
    37  var _ protoreflect.FileDescriptor = (*result)(nil)
    38  
    39  func (r *result) ParentFile() protoreflect.FileDescriptor {
    40  	return r
    41  }
    42  
    43  func (r *result) Parent() protoreflect.Descriptor {
    44  	return nil
    45  }
    46  
    47  func (r *result) Index() int {
    48  	return 0
    49  }
    50  
    51  func (r *result) Syntax() protoreflect.Syntax {
    52  	switch r.Proto().GetSyntax() {
    53  	case "proto2", "":
    54  		return protoreflect.Proto2
    55  	case "proto3":
    56  		return protoreflect.Proto3
    57  	default:
    58  		return 0 // ???
    59  	}
    60  }
    61  
    62  func (r *result) Name() protoreflect.Name {
    63  	return ""
    64  }
    65  
    66  func (r *result) FullName() protoreflect.FullName {
    67  	return r.Package()
    68  }
    69  
    70  func (r *result) IsPlaceholder() bool {
    71  	return false
    72  }
    73  
    74  func (r *result) Options() protoreflect.ProtoMessage {
    75  	return r.Proto().Options
    76  }
    77  
    78  func (r *result) Path() string {
    79  	return r.Proto().GetName()
    80  }
    81  
    82  func (r *result) Package() protoreflect.FullName {
    83  	return protoreflect.FullName(r.Proto().GetPackage())
    84  }
    85  
    86  func (r *result) Imports() protoreflect.FileImports {
    87  	return &fileImports{parent: r}
    88  }
    89  
    90  func (r *result) Enums() protoreflect.EnumDescriptors {
    91  	return &enumDescriptors{file: r, parent: r, enums: r.Proto().GetEnumType(), prefix: r.prefix}
    92  }
    93  
    94  func (r *result) Messages() protoreflect.MessageDescriptors {
    95  	return &msgDescriptors{file: r, parent: r, msgs: r.Proto().GetMessageType(), prefix: r.prefix}
    96  }
    97  
    98  func (r *result) Extensions() protoreflect.ExtensionDescriptors {
    99  	return &extDescriptors{file: r, parent: r, exts: r.Proto().GetExtension(), prefix: r.prefix}
   100  }
   101  
   102  func (r *result) Services() protoreflect.ServiceDescriptors {
   103  	return &svcDescriptors{file: r, svcs: r.Proto().GetService(), prefix: r.prefix}
   104  }
   105  
   106  func (r *result) SourceLocations() protoreflect.SourceLocations {
   107  	srcInfoProtos := r.Proto().GetSourceCodeInfo().GetLocation()
   108  	if r.srcLocs == nil && len(srcInfoProtos) > 0 {
   109  		r.srcLocs = asSourceLocations(srcInfoProtos)
   110  		r.srcLocIndex = computeSourceLocIndex(r.srcLocs)
   111  	}
   112  	return srcLocs{file: r, locs: r.srcLocs, index: r.srcLocIndex}
   113  }
   114  
   115  func computeSourceLocIndex(locs []protoreflect.SourceLocation) map[interface{}]protoreflect.SourceLocation {
   116  	index := map[interface{}]protoreflect.SourceLocation{}
   117  	for _, loc := range locs {
   118  		if loc.Next == 0 {
   119  			index[pathKey(loc.Path)] = loc
   120  		}
   121  	}
   122  	return index
   123  }
   124  
   125  func asSourceLocations(srcInfoProtos []*descriptorpb.SourceCodeInfo_Location) []protoreflect.SourceLocation {
   126  	locs := make([]protoreflect.SourceLocation, len(srcInfoProtos))
   127  	prev := map[string]*protoreflect.SourceLocation{}
   128  	for i, loc := range srcInfoProtos {
   129  		var stLin, stCol, enLin, enCol int
   130  		if len(loc.Span) == 3 {
   131  			stLin, stCol, enCol = int(loc.Span[0]), int(loc.Span[1]), int(loc.Span[2])
   132  			enLin = stLin
   133  		} else {
   134  			stLin, stCol, enLin, enCol = int(loc.Span[0]), int(loc.Span[1]), int(loc.Span[2]), int(loc.Span[3])
   135  		}
   136  		locs[i] = protoreflect.SourceLocation{
   137  			Path:                    loc.Path,
   138  			LeadingComments:         loc.GetLeadingComments(),
   139  			LeadingDetachedComments: loc.GetLeadingDetachedComments(),
   140  			TrailingComments:        loc.GetTrailingComments(),
   141  			StartLine:               stLin,
   142  			StartColumn:             stCol,
   143  			EndLine:                 enLin,
   144  			EndColumn:               enCol,
   145  		}
   146  		str := pathStr(loc.Path)
   147  		pr := prev[str]
   148  		if pr != nil {
   149  			pr.Next = i
   150  		}
   151  		prev[str] = &locs[i]
   152  	}
   153  	return locs
   154  }
   155  
   156  func pathStr(p protoreflect.SourcePath) string {
   157  	var buf bytes.Buffer
   158  	for _, v := range p {
   159  		fmt.Fprintf(&buf, "%x:", v)
   160  	}
   161  	return buf.String()
   162  }
   163  
   164  func (r *result) AddOptionBytes(opts []byte) {
   165  	pm := r.Proto().Options
   166  	if pm == nil {
   167  		panic(fmt.Sprintf("options is nil for %q", r.Path()))
   168  	}
   169  	r.optionBytes[pm] = append(r.optionBytes[pm], opts...)
   170  }
   171  
   172  type fileImports struct {
   173  	protoreflect.FileImports
   174  	parent *result
   175  }
   176  
   177  func (f *fileImports) Len() int {
   178  	return len(f.parent.Proto().Dependency)
   179  }
   180  
   181  func (f *fileImports) Get(i int) protoreflect.FileImport {
   182  	dep := f.parent.Proto().Dependency[i]
   183  	desc := f.parent.deps.FindFileByPath(dep)
   184  	isPublic := false
   185  	for _, d := range f.parent.Proto().PublicDependency {
   186  		if d == int32(i) {
   187  			isPublic = true
   188  			break
   189  		}
   190  	}
   191  	isWeak := false
   192  	for _, d := range f.parent.Proto().WeakDependency {
   193  		if d == int32(i) {
   194  			isWeak = true
   195  			break
   196  		}
   197  	}
   198  	return protoreflect.FileImport{FileDescriptor: desc, IsPublic: isPublic, IsWeak: isWeak}
   199  }
   200  
   201  type srcLocs struct {
   202  	protoreflect.SourceLocations
   203  	file  *result
   204  	locs  []protoreflect.SourceLocation
   205  	index map[interface{}]protoreflect.SourceLocation
   206  }
   207  
   208  func (s srcLocs) Len() int {
   209  	return len(s.locs)
   210  }
   211  
   212  func (s srcLocs) Get(i int) protoreflect.SourceLocation {
   213  	return s.locs[i]
   214  }
   215  
   216  func (s srcLocs) ByPath(p protoreflect.SourcePath) protoreflect.SourceLocation {
   217  	return s.index[pathKey(p)]
   218  }
   219  
   220  func (s srcLocs) ByDescriptor(d protoreflect.Descriptor) protoreflect.SourceLocation {
   221  	if d.ParentFile() != s.file {
   222  		return protoreflect.SourceLocation{}
   223  	}
   224  	path, ok := computePath(d)
   225  	if !ok {
   226  		return protoreflect.SourceLocation{}
   227  	}
   228  	return s.ByPath(path)
   229  }
   230  
   231  func computePath(d protoreflect.Descriptor) (protoreflect.SourcePath, bool) {
   232  	_, ok := d.(protoreflect.FileDescriptor)
   233  	if ok {
   234  		return nil, true
   235  	}
   236  	var path protoreflect.SourcePath
   237  	for {
   238  		p := d.Parent()
   239  		switch d := d.(type) {
   240  		case protoreflect.FileDescriptor:
   241  			return reverse(path), true
   242  		case protoreflect.MessageDescriptor:
   243  			path = append(path, int32(d.Index()))
   244  			switch p.(type) {
   245  			case protoreflect.FileDescriptor:
   246  				path = append(path, internal.File_messagesTag)
   247  			case protoreflect.MessageDescriptor:
   248  				path = append(path, internal.Message_nestedMessagesTag)
   249  			default:
   250  				return nil, false
   251  			}
   252  		case protoreflect.FieldDescriptor:
   253  			path = append(path, int32(d.Index()))
   254  			switch p.(type) {
   255  			case protoreflect.FileDescriptor:
   256  				if d.IsExtension() {
   257  					path = append(path, internal.File_extensionsTag)
   258  				} else {
   259  					return nil, false
   260  				}
   261  			case protoreflect.MessageDescriptor:
   262  				if d.IsExtension() {
   263  					path = append(path, internal.Message_extensionsTag)
   264  				} else {
   265  					path = append(path, internal.Message_fieldsTag)
   266  				}
   267  			default:
   268  				return nil, false
   269  			}
   270  		case protoreflect.OneofDescriptor:
   271  			path = append(path, int32(d.Index()))
   272  			if _, ok := p.(protoreflect.MessageDescriptor); ok {
   273  				path = append(path, internal.Message_oneOfsTag)
   274  			} else {
   275  				return nil, false
   276  			}
   277  		case protoreflect.EnumDescriptor:
   278  			path = append(path, int32(d.Index()))
   279  			switch p.(type) {
   280  			case protoreflect.FileDescriptor:
   281  				path = append(path, internal.File_enumsTag)
   282  			case protoreflect.MessageDescriptor:
   283  				path = append(path, internal.Message_enumsTag)
   284  			default:
   285  				return nil, false
   286  			}
   287  		case protoreflect.EnumValueDescriptor:
   288  			path = append(path, int32(d.Index()))
   289  			if _, ok := p.(protoreflect.EnumDescriptor); ok {
   290  				path = append(path, internal.Enum_valuesTag)
   291  			} else {
   292  				return nil, false
   293  			}
   294  		case protoreflect.ServiceDescriptor:
   295  			path = append(path, int32(d.Index()))
   296  			if _, ok := p.(protoreflect.FileDescriptor); ok {
   297  				path = append(path, internal.File_servicesTag)
   298  			} else {
   299  				return nil, false
   300  			}
   301  		case protoreflect.MethodDescriptor:
   302  			path = append(path, int32(d.Index()))
   303  			if _, ok := p.(protoreflect.ServiceDescriptor); ok {
   304  				path = append(path, internal.Service_methodsTag)
   305  			} else {
   306  				return nil, false
   307  			}
   308  		}
   309  		d = p
   310  	}
   311  }
   312  
   313  func reverse(p protoreflect.SourcePath) protoreflect.SourcePath {
   314  	for i, j := 0, len(p)-1; i < j; i, j = i+1, j-1 {
   315  		p[i], p[j] = p[j], p[i]
   316  	}
   317  	return p
   318  }
   319  
   320  type msgDescriptors struct {
   321  	protoreflect.MessageDescriptors
   322  	file   *result
   323  	parent protoreflect.Descriptor
   324  	msgs   []*descriptorpb.DescriptorProto
   325  	prefix string
   326  }
   327  
   328  func (m *msgDescriptors) Len() int {
   329  	return len(m.msgs)
   330  }
   331  
   332  func (m *msgDescriptors) Get(i int) protoreflect.MessageDescriptor {
   333  	msg := m.msgs[i]
   334  	return m.file.asMessageDescriptor(msg, m.file, m.parent, i, m.prefix+msg.GetName())
   335  }
   336  
   337  func (m *msgDescriptors) ByName(s protoreflect.Name) protoreflect.MessageDescriptor {
   338  	for i, msg := range m.msgs {
   339  		if msg.GetName() == string(s) {
   340  			return m.Get(i)
   341  		}
   342  	}
   343  	return nil
   344  }
   345  
   346  type msgDescriptor struct {
   347  	protoreflect.MessageDescriptor
   348  	file   *result
   349  	parent protoreflect.Descriptor
   350  	index  int
   351  	proto  *descriptorpb.DescriptorProto
   352  	fqn    string
   353  }
   354  
   355  func (r *result) asMessageDescriptor(md *descriptorpb.DescriptorProto, file *result, parent protoreflect.Descriptor, index int, fqn string) *msgDescriptor {
   356  	if ret := r.descriptors[md]; ret != nil {
   357  		return ret.(*msgDescriptor)
   358  	}
   359  	ret := &msgDescriptor{file: file, parent: parent, index: index, proto: md, fqn: fqn}
   360  	r.descriptors[md] = ret
   361  	return ret
   362  }
   363  
   364  func (m *msgDescriptor) ParentFile() protoreflect.FileDescriptor {
   365  	return m.file
   366  }
   367  
   368  func (m *msgDescriptor) Parent() protoreflect.Descriptor {
   369  	return m.parent
   370  }
   371  
   372  func (m *msgDescriptor) Index() int {
   373  	return m.index
   374  }
   375  
   376  func (m *msgDescriptor) Syntax() protoreflect.Syntax {
   377  	return m.file.Syntax()
   378  }
   379  
   380  func (m *msgDescriptor) Name() protoreflect.Name {
   381  	return protoreflect.Name(m.proto.GetName())
   382  }
   383  
   384  func (m *msgDescriptor) FullName() protoreflect.FullName {
   385  	return protoreflect.FullName(m.fqn)
   386  }
   387  
   388  func (m *msgDescriptor) IsPlaceholder() bool {
   389  	return false
   390  }
   391  
   392  func (m *msgDescriptor) Options() protoreflect.ProtoMessage {
   393  	return m.proto.Options
   394  }
   395  
   396  func (m *msgDescriptor) IsMapEntry() bool {
   397  	return m.proto.Options.GetMapEntry()
   398  }
   399  
   400  func (m *msgDescriptor) Fields() protoreflect.FieldDescriptors {
   401  	return &fldDescriptors{file: m.file, parent: m, fields: m.proto.GetField(), prefix: m.fqn + "."}
   402  }
   403  
   404  func (m *msgDescriptor) Oneofs() protoreflect.OneofDescriptors {
   405  	return &oneofDescriptors{file: m.file, parent: m, oneofs: m.proto.GetOneofDecl(), prefix: m.fqn + "."}
   406  }
   407  
   408  func (m *msgDescriptor) ReservedNames() protoreflect.Names {
   409  	return names{s: m.proto.ReservedName}
   410  }
   411  
   412  func (m *msgDescriptor) ReservedRanges() protoreflect.FieldRanges {
   413  	return fieldRanges{s: m.proto.ReservedRange}
   414  }
   415  
   416  func (m *msgDescriptor) RequiredNumbers() protoreflect.FieldNumbers {
   417  	var indexes fieldNums
   418  	for _, fld := range m.proto.Field {
   419  		if fld.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REQUIRED {
   420  			indexes.s = append(indexes.s, fld.GetNumber())
   421  		}
   422  	}
   423  	return indexes
   424  }
   425  
   426  func (m *msgDescriptor) ExtensionRanges() protoreflect.FieldRanges {
   427  	return extRanges{s: m.proto.ExtensionRange}
   428  }
   429  
   430  func (m *msgDescriptor) ExtensionRangeOptions(i int) protoreflect.ProtoMessage {
   431  	return m.proto.ExtensionRange[i].Options
   432  }
   433  
   434  func (m *msgDescriptor) Enums() protoreflect.EnumDescriptors {
   435  	return &enumDescriptors{file: m.file, parent: m, enums: m.proto.GetEnumType(), prefix: m.fqn + "."}
   436  }
   437  
   438  func (m *msgDescriptor) Messages() protoreflect.MessageDescriptors {
   439  	return &msgDescriptors{file: m.file, parent: m, msgs: m.proto.GetNestedType(), prefix: m.fqn + "."}
   440  }
   441  
   442  func (m *msgDescriptor) Extensions() protoreflect.ExtensionDescriptors {
   443  	return &extDescriptors{file: m.file, parent: m, exts: m.proto.GetExtension(), prefix: m.fqn + "."}
   444  }
   445  
   446  func (m *msgDescriptor) AddOptionBytes(opts []byte) {
   447  	pm := m.proto.Options
   448  	if pm == nil {
   449  		panic(fmt.Sprintf("options is nil for %s", m.FullName()))
   450  	}
   451  	m.file.optionBytes[pm] = append(m.file.optionBytes[pm], opts...)
   452  }
   453  
   454  func (m *msgDescriptor) AddExtensionRangeOptionBytes(i int, opts []byte) {
   455  	pm := m.proto.ExtensionRange[i].Options
   456  	if pm == nil {
   457  		panic(fmt.Sprintf("options is nil for %s:extension_range[%d]", m.FullName(), i))
   458  	}
   459  	m.file.optionBytes[pm] = append(m.file.optionBytes[pm], opts...)
   460  }
   461  
   462  type names struct {
   463  	protoreflect.Names
   464  	s []string
   465  }
   466  
   467  func (n names) Len() int {
   468  	return len(n.s)
   469  }
   470  
   471  func (n names) Get(i int) protoreflect.Name {
   472  	return protoreflect.Name(n.s[i])
   473  }
   474  
   475  func (n names) Has(s protoreflect.Name) bool {
   476  	for _, name := range n.s {
   477  		if name == string(s) {
   478  			return true
   479  		}
   480  	}
   481  	return false
   482  }
   483  
   484  type fieldNums struct {
   485  	protoreflect.FieldNumbers
   486  	s []int32
   487  }
   488  
   489  func (n fieldNums) Len() int {
   490  	return len(n.s)
   491  }
   492  
   493  func (n fieldNums) Get(i int) protoreflect.FieldNumber {
   494  	return protoreflect.FieldNumber(n.s[i])
   495  }
   496  
   497  func (n fieldNums) Has(s protoreflect.FieldNumber) bool {
   498  	for _, num := range n.s {
   499  		if num == int32(s) {
   500  			return true
   501  		}
   502  	}
   503  	return false
   504  }
   505  
   506  type fieldRanges struct {
   507  	protoreflect.FieldRanges
   508  	s []*descriptorpb.DescriptorProto_ReservedRange
   509  }
   510  
   511  func (f fieldRanges) Len() int {
   512  	return len(f.s)
   513  }
   514  
   515  func (f fieldRanges) Get(i int) [2]protoreflect.FieldNumber {
   516  	r := f.s[i]
   517  	return [2]protoreflect.FieldNumber{
   518  		protoreflect.FieldNumber(r.GetStart()),
   519  		protoreflect.FieldNumber(r.GetEnd()),
   520  	}
   521  }
   522  
   523  func (f fieldRanges) Has(n protoreflect.FieldNumber) bool {
   524  	for _, r := range f.s {
   525  		if r.GetStart() <= int32(n) && r.GetEnd() > int32(n) {
   526  			return true
   527  		}
   528  	}
   529  	return false
   530  }
   531  
   532  type extRanges struct {
   533  	protoreflect.FieldRanges
   534  	s []*descriptorpb.DescriptorProto_ExtensionRange
   535  }
   536  
   537  func (e extRanges) Len() int {
   538  	return len(e.s)
   539  }
   540  
   541  func (e extRanges) Get(i int) [2]protoreflect.FieldNumber {
   542  	r := e.s[i]
   543  	return [2]protoreflect.FieldNumber{
   544  		protoreflect.FieldNumber(r.GetStart()),
   545  		protoreflect.FieldNumber(r.GetEnd()),
   546  	}
   547  }
   548  
   549  func (e extRanges) Has(n protoreflect.FieldNumber) bool {
   550  	for _, r := range e.s {
   551  		if r.GetStart() <= int32(n) && r.GetEnd() > int32(n) {
   552  			return true
   553  		}
   554  	}
   555  	return false
   556  }
   557  
   558  type enumDescriptors struct {
   559  	protoreflect.EnumDescriptors
   560  	file   *result
   561  	parent protoreflect.Descriptor
   562  	enums  []*descriptorpb.EnumDescriptorProto
   563  	prefix string
   564  }
   565  
   566  func (e *enumDescriptors) Len() int {
   567  	return len(e.enums)
   568  }
   569  
   570  func (e *enumDescriptors) Get(i int) protoreflect.EnumDescriptor {
   571  	en := e.enums[i]
   572  	return e.file.asEnumDescriptor(en, e.file, e.parent, i, e.prefix+en.GetName())
   573  }
   574  
   575  func (e *enumDescriptors) ByName(s protoreflect.Name) protoreflect.EnumDescriptor {
   576  	for i, en := range e.enums {
   577  		if en.GetName() == string(s) {
   578  			return e.Get(i)
   579  		}
   580  	}
   581  	return nil
   582  }
   583  
   584  type enumDescriptor struct {
   585  	protoreflect.EnumDescriptor
   586  	file   *result
   587  	parent protoreflect.Descriptor
   588  	index  int
   589  	proto  *descriptorpb.EnumDescriptorProto
   590  	fqn    string
   591  }
   592  
   593  func (r *result) asEnumDescriptor(ed *descriptorpb.EnumDescriptorProto, file *result, parent protoreflect.Descriptor, index int, fqn string) *enumDescriptor {
   594  	if ret := r.descriptors[ed]; ret != nil {
   595  		return ret.(*enumDescriptor)
   596  	}
   597  	ret := &enumDescriptor{file: file, parent: parent, index: index, proto: ed, fqn: fqn}
   598  	r.descriptors[ed] = ret
   599  	return ret
   600  }
   601  
   602  func (e *enumDescriptor) ParentFile() protoreflect.FileDescriptor {
   603  	return e.file
   604  }
   605  
   606  func (e *enumDescriptor) Parent() protoreflect.Descriptor {
   607  	return e.parent
   608  }
   609  
   610  func (e *enumDescriptor) Index() int {
   611  	return e.index
   612  }
   613  
   614  func (e *enumDescriptor) Syntax() protoreflect.Syntax {
   615  	return e.file.Syntax()
   616  }
   617  
   618  func (e *enumDescriptor) Name() protoreflect.Name {
   619  	return protoreflect.Name(e.proto.GetName())
   620  }
   621  
   622  func (e *enumDescriptor) FullName() protoreflect.FullName {
   623  	return protoreflect.FullName(e.fqn)
   624  }
   625  
   626  func (e *enumDescriptor) IsPlaceholder() bool {
   627  	return false
   628  }
   629  
   630  func (e *enumDescriptor) Options() protoreflect.ProtoMessage {
   631  	return e.proto.Options
   632  }
   633  
   634  func (e *enumDescriptor) Values() protoreflect.EnumValueDescriptors {
   635  	// Unlike all other elements, the fully-qualified name of enum values
   636  	// is NOT scoped to their parent element (the enum), but rather to
   637  	// the enum's parent element. This follows C++ scoping rules for
   638  	// enum values.
   639  	prefix := strings.TrimSuffix(e.fqn, e.proto.GetName())
   640  	return &enValDescriptors{file: e.file, parent: e, vals: e.proto.GetValue(), prefix: prefix}
   641  }
   642  
   643  func (e *enumDescriptor) ReservedNames() protoreflect.Names {
   644  	return names{s: e.proto.ReservedName}
   645  }
   646  
   647  func (e *enumDescriptor) ReservedRanges() protoreflect.EnumRanges {
   648  	return enumRanges{s: e.proto.ReservedRange}
   649  }
   650  
   651  func (e *enumDescriptor) AddOptionBytes(opts []byte) {
   652  	pm := e.proto.Options
   653  	if pm == nil {
   654  		panic(fmt.Sprintf("options is nil for %s", e.FullName()))
   655  	}
   656  	e.file.optionBytes[pm] = append(e.file.optionBytes[pm], opts...)
   657  }
   658  
   659  type enumRanges struct {
   660  	protoreflect.EnumRanges
   661  	s []*descriptorpb.EnumDescriptorProto_EnumReservedRange
   662  }
   663  
   664  func (e enumRanges) Len() int {
   665  	return len(e.s)
   666  }
   667  
   668  func (e enumRanges) Get(i int) [2]protoreflect.EnumNumber {
   669  	r := e.s[i]
   670  	return [2]protoreflect.EnumNumber{
   671  		protoreflect.EnumNumber(r.GetStart()),
   672  		protoreflect.EnumNumber(r.GetEnd()),
   673  	}
   674  }
   675  
   676  func (e enumRanges) Has(n protoreflect.EnumNumber) bool {
   677  	for _, r := range e.s {
   678  		if r.GetStart() <= int32(n) && r.GetEnd() >= int32(n) {
   679  			return true
   680  		}
   681  	}
   682  	return false
   683  }
   684  
   685  type enValDescriptors struct {
   686  	protoreflect.EnumValueDescriptors
   687  	file   *result
   688  	parent *enumDescriptor
   689  	vals   []*descriptorpb.EnumValueDescriptorProto
   690  	prefix string
   691  }
   692  
   693  func (e *enValDescriptors) Len() int {
   694  	return len(e.vals)
   695  }
   696  
   697  func (e *enValDescriptors) Get(i int) protoreflect.EnumValueDescriptor {
   698  	val := e.vals[i]
   699  	return e.file.asEnumValueDescriptor(val, e.file, e.parent, i, e.prefix+val.GetName())
   700  }
   701  
   702  func (e *enValDescriptors) ByName(s protoreflect.Name) protoreflect.EnumValueDescriptor {
   703  	for i, en := range e.vals {
   704  		if en.GetName() == string(s) {
   705  			return e.Get(i)
   706  		}
   707  	}
   708  	return nil
   709  }
   710  
   711  func (e *enValDescriptors) ByNumber(n protoreflect.EnumNumber) protoreflect.EnumValueDescriptor {
   712  	for i, en := range e.vals {
   713  		if en.GetNumber() == int32(n) {
   714  			return e.Get(i)
   715  		}
   716  	}
   717  	return nil
   718  }
   719  
   720  type enValDescriptor struct {
   721  	protoreflect.EnumValueDescriptor
   722  	file   *result
   723  	parent *enumDescriptor
   724  	index  int
   725  	proto  *descriptorpb.EnumValueDescriptorProto
   726  	fqn    string
   727  }
   728  
   729  func (r *result) asEnumValueDescriptor(ed *descriptorpb.EnumValueDescriptorProto, file *result, parent *enumDescriptor, index int, fqn string) *enValDescriptor {
   730  	if ret := r.descriptors[ed]; ret != nil {
   731  		return ret.(*enValDescriptor)
   732  	}
   733  	ret := &enValDescriptor{file: file, parent: parent, index: index, proto: ed, fqn: fqn}
   734  	r.descriptors[ed] = ret
   735  	return ret
   736  }
   737  
   738  func (e *enValDescriptor) ParentFile() protoreflect.FileDescriptor {
   739  	return e.file
   740  }
   741  
   742  func (e *enValDescriptor) Parent() protoreflect.Descriptor {
   743  	return e.parent
   744  }
   745  
   746  func (e *enValDescriptor) Index() int {
   747  	return e.index
   748  }
   749  
   750  func (e *enValDescriptor) Syntax() protoreflect.Syntax {
   751  	return e.file.Syntax()
   752  }
   753  
   754  func (e *enValDescriptor) Name() protoreflect.Name {
   755  	return protoreflect.Name(e.proto.GetName())
   756  }
   757  
   758  func (e *enValDescriptor) FullName() protoreflect.FullName {
   759  	return protoreflect.FullName(e.fqn)
   760  }
   761  
   762  func (e *enValDescriptor) IsPlaceholder() bool {
   763  	return false
   764  }
   765  
   766  func (e *enValDescriptor) Options() protoreflect.ProtoMessage {
   767  	return e.proto.Options
   768  }
   769  
   770  func (e *enValDescriptor) Number() protoreflect.EnumNumber {
   771  	return protoreflect.EnumNumber(e.proto.GetNumber())
   772  }
   773  
   774  func (e *enValDescriptor) AddOptionBytes(opts []byte) {
   775  	pm := e.proto.Options
   776  	if pm == nil {
   777  		panic(fmt.Sprintf("options is nil for %s", e.FullName()))
   778  	}
   779  	e.file.optionBytes[pm] = append(e.file.optionBytes[pm], opts...)
   780  }
   781  
   782  type extDescriptors struct {
   783  	protoreflect.ExtensionDescriptors
   784  	file   *result
   785  	parent protoreflect.Descriptor
   786  	exts   []*descriptorpb.FieldDescriptorProto
   787  	prefix string
   788  }
   789  
   790  func (e *extDescriptors) Len() int {
   791  	return len(e.exts)
   792  }
   793  
   794  func (e *extDescriptors) Get(i int) protoreflect.ExtensionDescriptor {
   795  	fld := e.exts[i]
   796  	fd := e.file.asFieldDescriptor(fld, e.file, e.parent, i, e.prefix+fld.GetName())
   797  	// extensions are expected to implement ExtensionTypeDescriptor, not just ExtensionDescriptor
   798  	return dynamicpb.NewExtensionType(fd).TypeDescriptor()
   799  }
   800  
   801  func (e *extDescriptors) ByName(s protoreflect.Name) protoreflect.ExtensionDescriptor {
   802  	for i, ext := range e.exts {
   803  		if ext.GetName() == string(s) {
   804  			return e.Get(i)
   805  		}
   806  	}
   807  	return nil
   808  }
   809  
   810  type fldDescriptors struct {
   811  	protoreflect.FieldDescriptors
   812  	file   *result
   813  	parent protoreflect.Descriptor
   814  	fields []*descriptorpb.FieldDescriptorProto
   815  	prefix string
   816  }
   817  
   818  func (f *fldDescriptors) Len() int {
   819  	return len(f.fields)
   820  }
   821  
   822  func (f *fldDescriptors) Get(i int) protoreflect.FieldDescriptor {
   823  	fld := f.fields[i]
   824  	return f.file.asFieldDescriptor(fld, f.file, f.parent, i, f.prefix+fld.GetName())
   825  }
   826  
   827  func (f *fldDescriptors) ByName(s protoreflect.Name) protoreflect.FieldDescriptor {
   828  	for i, fld := range f.fields {
   829  		if fld.GetName() == string(s) {
   830  			return f.Get(i)
   831  		}
   832  	}
   833  	return nil
   834  }
   835  
   836  func (f *fldDescriptors) ByJSONName(s string) protoreflect.FieldDescriptor {
   837  	for i, fld := range f.fields {
   838  		if fld.GetJsonName() == s {
   839  			return f.Get(i)
   840  		}
   841  	}
   842  	return nil
   843  }
   844  
   845  func (f *fldDescriptors) ByTextName(s string) protoreflect.FieldDescriptor {
   846  	return f.ByName(protoreflect.Name(s))
   847  }
   848  
   849  func (f *fldDescriptors) ByNumber(n protoreflect.FieldNumber) protoreflect.FieldDescriptor {
   850  	for i, fld := range f.fields {
   851  		if fld.GetNumber() == int32(n) {
   852  			return f.Get(i)
   853  		}
   854  	}
   855  	return nil
   856  }
   857  
   858  type fldDescriptor struct {
   859  	protoreflect.FieldDescriptor
   860  	file   *result
   861  	parent protoreflect.Descriptor
   862  	index  int
   863  	proto  *descriptorpb.FieldDescriptorProto
   864  	fqn    string
   865  }
   866  
   867  func (r *result) asFieldDescriptor(fd *descriptorpb.FieldDescriptorProto, file *result, parent protoreflect.Descriptor, index int, fqn string) *fldDescriptor {
   868  	if ret := r.descriptors[fd]; ret != nil {
   869  		return ret.(*fldDescriptor)
   870  	}
   871  	ret := &fldDescriptor{file: file, parent: parent, index: index, proto: fd, fqn: fqn}
   872  	r.descriptors[fd] = ret
   873  	return ret
   874  }
   875  
   876  func (f *fldDescriptor) ParentFile() protoreflect.FileDescriptor {
   877  	return f.file
   878  }
   879  
   880  func (f *fldDescriptor) Parent() protoreflect.Descriptor {
   881  	return f.parent
   882  }
   883  
   884  func (f *fldDescriptor) Index() int {
   885  	return f.index
   886  }
   887  
   888  func (f *fldDescriptor) Syntax() protoreflect.Syntax {
   889  	return f.file.Syntax()
   890  }
   891  
   892  func (f *fldDescriptor) Name() protoreflect.Name {
   893  	return protoreflect.Name(f.proto.GetName())
   894  }
   895  
   896  func (f *fldDescriptor) FullName() protoreflect.FullName {
   897  	return protoreflect.FullName(f.fqn)
   898  }
   899  
   900  func (f *fldDescriptor) IsPlaceholder() bool {
   901  	return false
   902  }
   903  
   904  func (f *fldDescriptor) Options() protoreflect.ProtoMessage {
   905  	return f.proto.Options
   906  }
   907  
   908  func (f *fldDescriptor) Number() protoreflect.FieldNumber {
   909  	return protoreflect.FieldNumber(f.proto.GetNumber())
   910  }
   911  
   912  func (f *fldDescriptor) Cardinality() protoreflect.Cardinality {
   913  	switch f.proto.GetLabel() {
   914  	case descriptorpb.FieldDescriptorProto_LABEL_REPEATED:
   915  		return protoreflect.Repeated
   916  	case descriptorpb.FieldDescriptorProto_LABEL_REQUIRED:
   917  		return protoreflect.Required
   918  	case descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL:
   919  		return protoreflect.Optional
   920  	default:
   921  		return 0
   922  	}
   923  }
   924  
   925  func (f *fldDescriptor) Kind() protoreflect.Kind {
   926  	return protoreflect.Kind(f.proto.GetType())
   927  }
   928  
   929  func (f *fldDescriptor) HasJSONName() bool {
   930  	return f.proto.JsonName != nil
   931  }
   932  
   933  func (f *fldDescriptor) JSONName() string {
   934  	if f.IsExtension() {
   935  		return f.TextName()
   936  	} else {
   937  		return f.proto.GetJsonName()
   938  	}
   939  }
   940  
   941  func (f *fldDescriptor) TextName() string {
   942  	if f.IsExtension() {
   943  		return fmt.Sprintf("[%s]", f.FullName())
   944  	} else {
   945  		return string(f.Name())
   946  	}
   947  }
   948  
   949  func (f *fldDescriptor) HasPresence() bool {
   950  	if f.Syntax() == protoreflect.Proto2 {
   951  		return true
   952  	}
   953  	if f.Kind() == protoreflect.MessageKind || f.Kind() == protoreflect.GroupKind {
   954  		return true
   955  	}
   956  	if f.proto.OneofIndex != nil {
   957  		return true
   958  	}
   959  	return false
   960  }
   961  
   962  func (f *fldDescriptor) IsExtension() bool {
   963  	return f.proto.GetExtendee() != ""
   964  }
   965  
   966  func (f *fldDescriptor) HasOptionalKeyword() bool {
   967  	return f.proto.Label != nil && f.proto.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL
   968  }
   969  
   970  func (f *fldDescriptor) IsWeak() bool {
   971  	return f.proto.Options.GetWeak()
   972  }
   973  
   974  func (f *fldDescriptor) IsPacked() bool {
   975  	return f.proto.Options.GetPacked()
   976  }
   977  
   978  func (f *fldDescriptor) IsList() bool {
   979  	if f.proto.GetLabel() != descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
   980  		return false
   981  	}
   982  	return !f.isMapEntry()
   983  }
   984  
   985  func (f *fldDescriptor) IsMap() bool {
   986  	if f.proto.GetLabel() != descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
   987  		return false
   988  	}
   989  	if f.IsExtension() {
   990  		return false
   991  	}
   992  	return f.isMapEntry()
   993  }
   994  
   995  func (f *fldDescriptor) isMapEntry() bool {
   996  	if f.proto.GetType() != descriptorpb.FieldDescriptorProto_TYPE_MESSAGE {
   997  		return false
   998  	}
   999  	return f.Message().IsMapEntry()
  1000  }
  1001  
  1002  func (f *fldDescriptor) MapKey() protoreflect.FieldDescriptor {
  1003  	if !f.IsMap() {
  1004  		return nil
  1005  	}
  1006  	return f.Message().Fields().ByNumber(1)
  1007  }
  1008  
  1009  func (f *fldDescriptor) MapValue() protoreflect.FieldDescriptor {
  1010  	if !f.IsMap() {
  1011  		return nil
  1012  	}
  1013  	return f.Message().Fields().ByNumber(2)
  1014  }
  1015  
  1016  func (f *fldDescriptor) HasDefault() bool {
  1017  	// TODO
  1018  	return false
  1019  }
  1020  
  1021  func (f *fldDescriptor) Default() protoreflect.Value {
  1022  	switch f.Kind() {
  1023  	case protoreflect.Int32Kind, protoreflect.Sint32Kind,
  1024  		protoreflect.Sfixed32Kind:
  1025  		return protoreflect.ValueOfInt32(0)
  1026  	case protoreflect.Int64Kind, protoreflect.Sint64Kind,
  1027  		protoreflect.Sfixed64Kind:
  1028  		return protoreflect.ValueOfInt64(0)
  1029  	case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
  1030  		return protoreflect.ValueOfUint32(0)
  1031  	case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
  1032  		return protoreflect.ValueOfUint64(0)
  1033  	case protoreflect.FloatKind:
  1034  		return protoreflect.ValueOfFloat32(0)
  1035  	case protoreflect.DoubleKind:
  1036  		return protoreflect.ValueOfFloat32(0)
  1037  	case protoreflect.BoolKind:
  1038  		return protoreflect.ValueOfBool(false)
  1039  	case protoreflect.BytesKind:
  1040  		return protoreflect.ValueOfBytes(nil)
  1041  	case protoreflect.StringKind:
  1042  		return protoreflect.ValueOfString("")
  1043  	case protoreflect.EnumKind:
  1044  		return protoreflect.ValueOfEnum(f.Enum().Values().Get(0).Number())
  1045  	case protoreflect.GroupKind, protoreflect.MessageKind:
  1046  		return protoreflect.ValueOfMessage(dynamicpb.NewMessage(f.Message()))
  1047  	default:
  1048  		panic(fmt.Sprintf("unknown kind: %v", f.Kind()))
  1049  	}
  1050  }
  1051  
  1052  func (f *fldDescriptor) DefaultEnumValue() protoreflect.EnumValueDescriptor {
  1053  	ed := f.Enum()
  1054  	if ed == nil {
  1055  		return nil
  1056  	}
  1057  	return ed.Values().Get(0)
  1058  }
  1059  
  1060  func (f *fldDescriptor) ContainingOneof() protoreflect.OneofDescriptor {
  1061  	if f.IsExtension() {
  1062  		return nil
  1063  	}
  1064  	if f.proto.OneofIndex == nil {
  1065  		return nil
  1066  	}
  1067  	parent := f.parent.(*msgDescriptor)
  1068  	index := int(f.proto.GetOneofIndex())
  1069  	ood := parent.proto.OneofDecl[index]
  1070  	fqn := parent.fqn + "." + ood.GetName()
  1071  	return f.file.asOneOfDescriptor(ood, f.file, parent, index, fqn)
  1072  }
  1073  
  1074  func (f *fldDescriptor) ContainingMessage() protoreflect.MessageDescriptor {
  1075  	if !f.IsExtension() {
  1076  		return f.parent.(*msgDescriptor)
  1077  	}
  1078  	return f.file.ResolveMessageType(protoreflect.FullName(f.proto.GetExtendee()))
  1079  }
  1080  
  1081  func (f *fldDescriptor) Enum() protoreflect.EnumDescriptor {
  1082  	if f.proto.GetType() != descriptorpb.FieldDescriptorProto_TYPE_ENUM {
  1083  		return nil
  1084  	}
  1085  	return f.file.ResolveEnumType(protoreflect.FullName(f.proto.GetTypeName()))
  1086  }
  1087  
  1088  func (f *fldDescriptor) Message() protoreflect.MessageDescriptor {
  1089  	if f.proto.GetType() != descriptorpb.FieldDescriptorProto_TYPE_MESSAGE &&
  1090  		f.proto.GetType() != descriptorpb.FieldDescriptorProto_TYPE_GROUP {
  1091  		return nil
  1092  	}
  1093  	return f.file.ResolveMessageType(protoreflect.FullName(f.proto.GetTypeName()))
  1094  }
  1095  
  1096  func (f *fldDescriptor) AddOptionBytes(opts []byte) {
  1097  	pm := f.proto.Options
  1098  	if pm == nil {
  1099  		panic(fmt.Sprintf("options is nil for %s", f.FullName()))
  1100  	}
  1101  	f.file.optionBytes[pm] = append(f.file.optionBytes[pm], opts...)
  1102  }
  1103  
  1104  type oneofDescriptors struct {
  1105  	protoreflect.OneofDescriptors
  1106  	file   *result
  1107  	parent *msgDescriptor
  1108  	oneofs []*descriptorpb.OneofDescriptorProto
  1109  	prefix string
  1110  }
  1111  
  1112  func (o *oneofDescriptors) Len() int {
  1113  	return len(o.oneofs)
  1114  }
  1115  
  1116  func (o *oneofDescriptors) Get(i int) protoreflect.OneofDescriptor {
  1117  	oo := o.oneofs[i]
  1118  	return o.file.asOneOfDescriptor(oo, o.file, o.parent, i, o.prefix+oo.GetName())
  1119  }
  1120  
  1121  func (o *oneofDescriptors) ByName(s protoreflect.Name) protoreflect.OneofDescriptor {
  1122  	for i, oo := range o.oneofs {
  1123  		if oo.GetName() == string(s) {
  1124  			return o.Get(i)
  1125  		}
  1126  	}
  1127  	return nil
  1128  }
  1129  
  1130  type oneofDescriptor struct {
  1131  	protoreflect.OneofDescriptor
  1132  	file   *result
  1133  	parent *msgDescriptor
  1134  	index  int
  1135  	proto  *descriptorpb.OneofDescriptorProto
  1136  	fqn    string
  1137  }
  1138  
  1139  func (r *result) asOneOfDescriptor(ood *descriptorpb.OneofDescriptorProto, file *result, parent *msgDescriptor, index int, fqn string) *oneofDescriptor {
  1140  	if ret := r.descriptors[ood]; ret != nil {
  1141  		return ret.(*oneofDescriptor)
  1142  	}
  1143  	ret := &oneofDescriptor{file: file, parent: parent, index: index, proto: ood, fqn: fqn}
  1144  	r.descriptors[ood] = ret
  1145  	return ret
  1146  }
  1147  
  1148  func (o *oneofDescriptor) ParentFile() protoreflect.FileDescriptor {
  1149  	return o.file
  1150  }
  1151  
  1152  func (o *oneofDescriptor) Parent() protoreflect.Descriptor {
  1153  	return o.parent
  1154  }
  1155  
  1156  func (o *oneofDescriptor) Index() int {
  1157  	return o.index
  1158  }
  1159  
  1160  func (o *oneofDescriptor) Syntax() protoreflect.Syntax {
  1161  	return o.file.Syntax()
  1162  }
  1163  
  1164  func (o *oneofDescriptor) Name() protoreflect.Name {
  1165  	return protoreflect.Name(o.proto.GetName())
  1166  }
  1167  
  1168  func (o *oneofDescriptor) FullName() protoreflect.FullName {
  1169  	return protoreflect.FullName(o.fqn)
  1170  }
  1171  
  1172  func (o *oneofDescriptor) IsPlaceholder() bool {
  1173  	return false
  1174  }
  1175  
  1176  func (o *oneofDescriptor) Options() protoreflect.ProtoMessage {
  1177  	return o.proto.Options
  1178  }
  1179  
  1180  func (o *oneofDescriptor) IsSynthetic() bool {
  1181  	// TODO
  1182  	return false
  1183  }
  1184  
  1185  func (o *oneofDescriptor) Fields() protoreflect.FieldDescriptors {
  1186  	var fields []*descriptorpb.FieldDescriptorProto
  1187  	for _, fld := range o.parent.proto.GetField() {
  1188  		if fld.OneofIndex != nil && int(fld.GetOneofIndex()) == o.index {
  1189  			fields = append(fields, fld)
  1190  		}
  1191  	}
  1192  	return &fldDescriptors{file: o.file, parent: o.parent, fields: fields, prefix: o.parent.fqn + "."}
  1193  }
  1194  
  1195  func (o *oneofDescriptor) AddOptionBytes(opts []byte) {
  1196  	pm := o.proto.Options
  1197  	if pm == nil {
  1198  		panic(fmt.Sprintf("options is nil for %s", o.FullName()))
  1199  	}
  1200  	o.file.optionBytes[pm] = append(o.file.optionBytes[pm], opts...)
  1201  }
  1202  
  1203  type svcDescriptors struct {
  1204  	protoreflect.ServiceDescriptors
  1205  	file   *result
  1206  	svcs   []*descriptorpb.ServiceDescriptorProto
  1207  	prefix string
  1208  }
  1209  
  1210  func (s *svcDescriptors) Len() int {
  1211  	return len(s.svcs)
  1212  }
  1213  
  1214  func (s *svcDescriptors) Get(i int) protoreflect.ServiceDescriptor {
  1215  	svc := s.svcs[i]
  1216  	return s.file.asServiceDescriptor(svc, s.file, i, s.prefix+svc.GetName())
  1217  }
  1218  
  1219  func (s *svcDescriptors) ByName(n protoreflect.Name) protoreflect.ServiceDescriptor {
  1220  	for i, svc := range s.svcs {
  1221  		if svc.GetName() == string(n) {
  1222  			return s.Get(i)
  1223  		}
  1224  	}
  1225  	return nil
  1226  }
  1227  
  1228  type svcDescriptor struct {
  1229  	protoreflect.ServiceDescriptor
  1230  	file  *result
  1231  	index int
  1232  	proto *descriptorpb.ServiceDescriptorProto
  1233  	fqn   string
  1234  }
  1235  
  1236  func (r *result) asServiceDescriptor(sd *descriptorpb.ServiceDescriptorProto, file *result, index int, fqn string) *svcDescriptor {
  1237  	if ret := r.descriptors[sd]; ret != nil {
  1238  		return ret.(*svcDescriptor)
  1239  	}
  1240  	ret := &svcDescriptor{file: file, index: index, proto: sd, fqn: fqn}
  1241  	r.descriptors[sd] = ret
  1242  	return ret
  1243  }
  1244  
  1245  func (s *svcDescriptor) ParentFile() protoreflect.FileDescriptor {
  1246  	return s.file
  1247  }
  1248  
  1249  func (s *svcDescriptor) Parent() protoreflect.Descriptor {
  1250  	return s.file
  1251  }
  1252  
  1253  func (s *svcDescriptor) Index() int {
  1254  	return s.index
  1255  }
  1256  
  1257  func (s *svcDescriptor) Syntax() protoreflect.Syntax {
  1258  	return s.file.Syntax()
  1259  }
  1260  
  1261  func (s *svcDescriptor) Name() protoreflect.Name {
  1262  	return protoreflect.Name(s.proto.GetName())
  1263  }
  1264  
  1265  func (s *svcDescriptor) FullName() protoreflect.FullName {
  1266  	return protoreflect.FullName(s.fqn)
  1267  }
  1268  
  1269  func (s *svcDescriptor) IsPlaceholder() bool {
  1270  	return false
  1271  }
  1272  
  1273  func (s *svcDescriptor) Options() protoreflect.ProtoMessage {
  1274  	return s.proto.Options
  1275  }
  1276  
  1277  func (s *svcDescriptor) Methods() protoreflect.MethodDescriptors {
  1278  	return &mtdDescriptors{file: s.file, parent: s, mtds: s.proto.GetMethod(), prefix: s.fqn + "."}
  1279  }
  1280  
  1281  func (s *svcDescriptor) AddOptionBytes(opts []byte) {
  1282  	pm := s.proto.Options
  1283  	if pm == nil {
  1284  		panic(fmt.Sprintf("options is nil for %s", s.FullName()))
  1285  	}
  1286  	s.file.optionBytes[pm] = append(s.file.optionBytes[pm], opts...)
  1287  }
  1288  
  1289  type mtdDescriptors struct {
  1290  	protoreflect.MethodDescriptors
  1291  	file   *result
  1292  	parent *svcDescriptor
  1293  	mtds   []*descriptorpb.MethodDescriptorProto
  1294  	prefix string
  1295  }
  1296  
  1297  func (m *mtdDescriptors) Len() int {
  1298  	return len(m.mtds)
  1299  }
  1300  
  1301  func (m *mtdDescriptors) Get(i int) protoreflect.MethodDescriptor {
  1302  	mtd := m.mtds[i]
  1303  	return m.file.asMethodDescriptor(mtd, m.file, m.parent, i, m.prefix+mtd.GetName())
  1304  }
  1305  
  1306  func (m *mtdDescriptors) ByName(n protoreflect.Name) protoreflect.MethodDescriptor {
  1307  	for i, svc := range m.mtds {
  1308  		if svc.GetName() == string(n) {
  1309  			return m.Get(i)
  1310  		}
  1311  	}
  1312  	return nil
  1313  }
  1314  
  1315  type mtdDescriptor struct {
  1316  	protoreflect.MethodDescriptor
  1317  	file   *result
  1318  	parent *svcDescriptor
  1319  	index  int
  1320  	proto  *descriptorpb.MethodDescriptorProto
  1321  	fqn    string
  1322  }
  1323  
  1324  func (r *result) asMethodDescriptor(mtd *descriptorpb.MethodDescriptorProto, file *result, parent *svcDescriptor, index int, fqn string) *mtdDescriptor {
  1325  	if ret := r.descriptors[mtd]; ret != nil {
  1326  		return ret.(*mtdDescriptor)
  1327  	}
  1328  	ret := &mtdDescriptor{file: file, parent: parent, index: index, proto: mtd, fqn: fqn}
  1329  	r.descriptors[mtd] = ret
  1330  	return ret
  1331  }
  1332  
  1333  func (m *mtdDescriptor) ParentFile() protoreflect.FileDescriptor {
  1334  	return m.file
  1335  }
  1336  
  1337  func (m *mtdDescriptor) Parent() protoreflect.Descriptor {
  1338  	return m.parent
  1339  }
  1340  
  1341  func (m *mtdDescriptor) Index() int {
  1342  	return m.index
  1343  }
  1344  
  1345  func (m *mtdDescriptor) Syntax() protoreflect.Syntax {
  1346  	return m.file.Syntax()
  1347  }
  1348  
  1349  func (m *mtdDescriptor) Name() protoreflect.Name {
  1350  	return protoreflect.Name(m.proto.GetName())
  1351  }
  1352  
  1353  func (m *mtdDescriptor) FullName() protoreflect.FullName {
  1354  	return protoreflect.FullName(m.fqn)
  1355  }
  1356  
  1357  func (m *mtdDescriptor) IsPlaceholder() bool {
  1358  	return false
  1359  }
  1360  
  1361  func (m *mtdDescriptor) Options() protoreflect.ProtoMessage {
  1362  	return m.proto.Options
  1363  }
  1364  
  1365  func (m *mtdDescriptor) Input() protoreflect.MessageDescriptor {
  1366  	return m.file.ResolveMessageType(protoreflect.FullName(m.proto.GetInputType()))
  1367  }
  1368  
  1369  func (m *mtdDescriptor) Output() protoreflect.MessageDescriptor {
  1370  	return m.file.ResolveMessageType(protoreflect.FullName(m.proto.GetOutputType()))
  1371  }
  1372  
  1373  func (m *mtdDescriptor) IsStreamingClient() bool {
  1374  	return m.proto.GetClientStreaming()
  1375  }
  1376  
  1377  func (m *mtdDescriptor) IsStreamingServer() bool {
  1378  	return m.proto.GetServerStreaming()
  1379  }
  1380  
  1381  func (m *mtdDescriptor) AddOptionBytes(opts []byte) {
  1382  	pm := m.proto.Options
  1383  	if pm == nil {
  1384  		panic(fmt.Sprintf("options is nil for %s", m.FullName()))
  1385  	}
  1386  	m.file.optionBytes[pm] = append(m.file.optionBytes[pm], opts...)
  1387  }
  1388  
  1389  func (r *result) FindImportByPath(path string) File {
  1390  	return r.deps.FindFileByPath(path)
  1391  }
  1392  
  1393  func (r *result) FindExtensionByNumber(msg protoreflect.FullName, tag protoreflect.FieldNumber) protoreflect.ExtensionTypeDescriptor {
  1394  	return findExtension(r, msg, tag)
  1395  }
  1396  
  1397  func (r *result) FindDescriptorByName(name protoreflect.FullName) protoreflect.Descriptor {
  1398  	fqn := strings.TrimPrefix(string(name), ".")
  1399  	d := r.descriptorPool[string(name)]
  1400  	if d == nil {
  1401  		return nil
  1402  	}
  1403  	return r.toDescriptor(fqn, d)
  1404  }
  1405  
  1406  func (r *result) importsAsFiles() Files {
  1407  	return r.deps
  1408  }