github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/proto/descriptor.go (about)

     1  package proto
     2  
     3  type TypeDescriptor struct {
     4  	baseId FieldNumber // for LIST/MAP to write field tag by baseId
     5  	typ    Type
     6  	name   string
     7  	key    *TypeDescriptor
     8  	elem   *TypeDescriptor
     9  	msg    *MessageDescriptor // for message, list+message element and map key-value entry
    10  }
    11  
    12  func (t *TypeDescriptor) Type() Type {
    13  	return t.typ
    14  }
    15  
    16  func (t *TypeDescriptor) Key() *TypeDescriptor {
    17  	return t.key
    18  }
    19  
    20  func (t *TypeDescriptor) Elem() *TypeDescriptor {
    21  	return t.elem
    22  }
    23  
    24  func (t *TypeDescriptor) Message() *MessageDescriptor {
    25  	return t.msg
    26  }
    27  
    28  func (t *TypeDescriptor) BaseId() FieldNumber {
    29  	return t.baseId
    30  }
    31  
    32  func (t *TypeDescriptor) IsPacked() bool {
    33  	if t.typ != LIST {
    34  		return false // if not list, return false forever
    35  	}
    36  	return t.elem.typ.IsPacked()
    37  }
    38  
    39  func (f *TypeDescriptor) IsMap() bool {
    40  	return f.typ == MAP
    41  }
    42  
    43  func (f *TypeDescriptor) IsList() bool {
    44  	return f.typ == LIST
    45  }
    46  
    47  func (f *TypeDescriptor) WireType() WireType {
    48  	kind := f.typ.TypeToKind()
    49  	return Kind2Wire[kind]
    50  }
    51  
    52  func (f *TypeDescriptor) Name() string {
    53  	return f.name
    54  }
    55  
    56  type FieldDescriptor struct {
    57  	kind     ProtoKind // the same value with protobuf descriptor
    58  	id       FieldNumber
    59  	name     string
    60  	jsonName string
    61  	typ      *TypeDescriptor
    62  }
    63  
    64  func (f *FieldDescriptor) Number() FieldNumber {
    65  	return f.id
    66  }
    67  
    68  func (f *FieldDescriptor) Kind() ProtoKind {
    69  	return f.kind
    70  }
    71  
    72  func (f *FieldDescriptor) Name() string {
    73  	return f.name
    74  }
    75  
    76  func (f *FieldDescriptor) JSONName() string {
    77  	return f.jsonName
    78  }
    79  
    80  func (f *FieldDescriptor) Type() *TypeDescriptor {
    81  	return f.typ
    82  }
    83  
    84  // when List+Message it can get message element descriptor
    85  // when Map it can get map key-value entry massage descriptor
    86  // when Message it can get sub message descriptor
    87  func (f *FieldDescriptor) Message() *MessageDescriptor {
    88  	return f.typ.Message()
    89  }
    90  
    91  func (f *FieldDescriptor) MapKey() *TypeDescriptor {
    92  	if f.typ.Type() != MAP {
    93  		panic("not map")
    94  	}
    95  	return f.typ.Key()
    96  }
    97  
    98  func (f *FieldDescriptor) MapValue() *TypeDescriptor {
    99  	if f.typ.Type() != MAP {
   100  		panic("not map")
   101  	}
   102  	return f.typ.Elem()
   103  }
   104  
   105  func (f *FieldDescriptor) IsMap() bool {
   106  	return f.typ.IsMap()
   107  }
   108  
   109  func (f *FieldDescriptor) IsList() bool {
   110  	return f.typ.IsList()
   111  }
   112  
   113  type MessageDescriptor struct {
   114  	baseId FieldNumber
   115  	name   string
   116  	ids    FieldNumberMap
   117  	names  FieldNameMap // store name and jsonName for FieldDescriptor
   118  }
   119  
   120  func (m *MessageDescriptor) Name() string {
   121  	return m.name
   122  }
   123  
   124  func (m *MessageDescriptor) ByJSONName(name string) *FieldDescriptor {
   125  	return m.names.Get(name)
   126  }
   127  
   128  func (m *MessageDescriptor) ByName(name string) *FieldDescriptor {
   129  	return m.names.Get(name)
   130  }
   131  
   132  func (m *MessageDescriptor) ByNumber(id FieldNumber) *FieldDescriptor {
   133  	return m.ids.Get(id)
   134  }
   135  
   136  func (m *MessageDescriptor) FieldsCount() int {
   137  	return m.ids.Size() - 1
   138  }
   139  
   140  type MethodDescriptor struct {
   141  	name   string
   142  	input  *TypeDescriptor
   143  	output *TypeDescriptor
   144  }
   145  
   146  func (m *MethodDescriptor) Name() string {
   147  	return m.name
   148  }
   149  
   150  func (m *MethodDescriptor) Input() *TypeDescriptor {
   151  	return m.input
   152  }
   153  
   154  func (m *MethodDescriptor) Output() *TypeDescriptor {
   155  	return m.output
   156  }
   157  
   158  type ServiceDescriptor struct {
   159  	serviceName string
   160  	methods     map[string]*MethodDescriptor
   161  }
   162  
   163  func (s *ServiceDescriptor) Name() string {
   164  	return s.serviceName
   165  }
   166  
   167  func (s *ServiceDescriptor) Methods() map[string]*MethodDescriptor {
   168  	return s.methods
   169  }
   170  
   171  func (s *ServiceDescriptor) LookupMethodByName(name string) *MethodDescriptor {
   172  	return s.methods[name]
   173  }