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

     1  package proto
     2  
     3  import (
     4  	"google.golang.org/protobuf/encoding/protowire"
     5  	"google.golang.org/protobuf/reflect/protoreflect"
     6  	"google.golang.org/protobuf/types/descriptorpb"
     7  )
     8  
     9  // protobuf encoding wire type
    10  type WireType int8
    11  
    12  const (
    13  	VarintType     WireType = 0
    14  	Fixed32Type    WireType = 5
    15  	Fixed64Type    WireType = 1
    16  	BytesType      WireType = 2
    17  	StartGroupType WireType = 3 // deprecated
    18  	EndGroupType   WireType = 4 // deprecated
    19  )
    20  
    21  func (p WireType) String() string {
    22  	switch p {
    23  	case VarintType:
    24  		return "VarintType"
    25  	case Fixed32Type:
    26  		return "Fixed32Type"
    27  	case Fixed64Type:
    28  		return "Fixed64Type"
    29  	case BytesType:
    30  		return "BytesType"
    31  	case StartGroupType:
    32  		return "StartGroupType"
    33  	case EndGroupType:
    34  		return "EndGroupType"
    35  	default:
    36  		return "UnknownWireType"
    37  	}
    38  }
    39  
    40  // define ProtoKind = protoreflect.Kind (int8)
    41  type ProtoKind = protoreflect.Kind
    42  
    43  const (
    44  	DoubleKind ProtoKind = iota + 1
    45  	FloatKind
    46  	Int64Kind
    47  	Uint64Kind
    48  	Int32Kind
    49  	Fixed64Kind
    50  	Fixed32Kind
    51  	BoolKind
    52  	StringKind
    53  	GroupKind
    54  	MessageKind
    55  	BytesKind
    56  	Uint32Kind
    57  	EnumKind
    58  	Sfixed32Kind
    59  	Sfixed64Kind
    60  	Sint32Kind
    61  	Sint64Kind
    62  )
    63  
    64  // map from proto.ProtoKind to proto.WireType
    65  var Kind2Wire = map[ProtoKind]WireType{
    66  	BoolKind:     VarintType,
    67  	EnumKind:     VarintType,
    68  	Int32Kind:    VarintType,
    69  	Sint32Kind:   VarintType,
    70  	Uint32Kind:   VarintType,
    71  	Int64Kind:    VarintType,
    72  	Sint64Kind:   VarintType,
    73  	Uint64Kind:   VarintType,
    74  	Sfixed32Kind: Fixed32Type,
    75  	Fixed32Kind:  Fixed32Type,
    76  	FloatKind:    Fixed32Type,
    77  	Sfixed64Kind: Fixed64Type,
    78  	Fixed64Kind:  Fixed64Type,
    79  	DoubleKind:   Fixed64Type,
    80  	StringKind:   BytesType,
    81  	BytesKind:    BytesType,
    82  	MessageKind:  BytesType,
    83  	GroupKind:    StartGroupType,
    84  }
    85  
    86  // Node type (uint8) mapping ProtoKind the same value, except for UNKNOWN, LIST, MAP, ERROR
    87  type Type uint8
    88  
    89  const (
    90  	UNKNOWN Type = 0 // unknown field type
    91  	DOUBLE  Type = 1
    92  	FLOAT   Type = 2
    93  	INT64   Type = 3
    94  	UINT64  Type = 4
    95  	INT32   Type = 5
    96  	FIX64   Type = 6
    97  	FIX32   Type = 7
    98  	BOOL    Type = 8
    99  	STRING  Type = 9
   100  	GROUP   Type = 10 // deprecated
   101  	MESSAGE Type = 11
   102  	BYTE    Type = 12
   103  	UINT32  Type = 13
   104  	ENUM    Type = 14
   105  	SFIX32  Type = 15
   106  	SFIX64  Type = 16
   107  	SINT32  Type = 17
   108  	SINT64  Type = 18
   109  	LIST    Type = 19
   110  	MAP     Type = 20
   111  	ERROR   Type = 255
   112  )
   113  
   114  func (p Type) Valid() bool {
   115  	switch p {
   116  	case UNKNOWN, BOOL, ENUM, BYTE, INT32, SINT32, UINT32, SFIX32, FIX32, INT64, SINT64, UINT64, SFIX64, FIX64, FLOAT,
   117  		DOUBLE, STRING, MESSAGE, LIST, MAP:
   118  		return true
   119  	default:
   120  		return false
   121  	}
   122  }
   123  
   124  func (p Type) TypeToKind() ProtoKind {
   125  	switch p {
   126  	case UNKNOWN, ERROR:
   127  		return 0
   128  	case MAP:
   129  		return MessageKind
   130  	case LIST:
   131  		panic("LIST type has no kind, only list element type has kind")
   132  	}
   133  	return ProtoKind(p)
   134  }
   135  
   136  // FromProtoKindTType converts ProtoKind to Type
   137  func FromProtoKindToType(kind ProtoKind, isList bool, isMap bool) Type {
   138  	t := Type(kind)
   139  	if isList {
   140  		t = LIST
   141  	} else if isMap {
   142  		t = MAP
   143  	}
   144  	return t
   145  }
   146  
   147  // check if the type need Varint encoding
   148  func (p Type) NeedVarint() bool {
   149  	return p == BOOL || p == ENUM || p == INT32 || p == SINT32 || p == UINT32 || p == INT64 || p == SINT64 || p == UINT64
   150  }
   151  
   152  func (p Type) IsPacked() bool {
   153  	if p == LIST || p == MAP {
   154  		panic("error type")
   155  	}
   156  	return p != STRING && p != MESSAGE && p != BYTE
   157  }
   158  
   159  // IsInt containing isUint
   160  func (p Type) IsInt() bool {
   161  	return p == INT32 || p == INT64 || p == SFIX32 || p == SFIX64 || p == SINT64 || p == SINT32 || p == UINT32 || p == UINT64 || p == FIX32 || p == FIX64
   162  }
   163  
   164  func (p Type) IsUint() bool {
   165  	return p == UINT32 || p == UINT64 || p == FIX32 || p == FIX64
   166  }
   167  
   168  // IsComplex tells if the type is one of STRUCT, MAP, SET, LIST
   169  func (p Type) IsComplex() bool {
   170  	return p == MESSAGE || p == MAP || p == LIST
   171  }
   172  
   173  // String for format and print
   174  func (p Type) String() string {
   175  	switch p {
   176  	case UNKNOWN:
   177  		return "UNKNOWN"
   178  	case BOOL:
   179  		return "BOOL"
   180  	case ENUM:
   181  		return "ENUM"
   182  	case BYTE:
   183  		return "BYTE"
   184  	case INT32:
   185  		return "INT32"
   186  	case SINT32:
   187  		return "SINT32"
   188  	case UINT32:
   189  		return "UINT32"
   190  	case SFIX32:
   191  		return "SFIX32"
   192  	case FIX32:
   193  		return "FIX32"
   194  	case INT64:
   195  		return "INT64"
   196  	case SINT64:
   197  		return "SINT64"
   198  	case UINT64:
   199  		return "UINT64"
   200  	case SFIX64:
   201  		return "SFIX64"
   202  	case FIX64:
   203  		return "FIX64"
   204  	case FLOAT:
   205  		return "FLOAT"
   206  	case DOUBLE:
   207  		return "DOUBLE"
   208  	case STRING:
   209  		return "STRING"
   210  	case MESSAGE:
   211  		return "MESSAGE"
   212  	case LIST:
   213  		return "LIST"
   214  	case MAP:
   215  		return "MAP"
   216  	default:
   217  		return "ERROR"
   218  	}
   219  }
   220  
   221  // define Number = protowire.Number (int32)
   222  type Number = protowire.Number
   223  
   224  type FieldNumber int32
   225  type EnumNumber int32
   226  
   227  // reserved field number min-max ranges in a proto message
   228  const (
   229  	MinValidNumber        FieldNumber = 1
   230  	FirstReservedNumber   FieldNumber = 19000
   231  	LastReservedNumber    FieldNumber = 19999
   232  	MaxValidNumber        FieldNumber = 1<<29 - 1
   233  	DefaultRecursionLimit        = 10000
   234  )
   235  
   236  // builtinTypes from descriptorProto to TypeDescriptor
   237  var builtinTypes = map[descriptorpb.FieldDescriptorProto_Type]*TypeDescriptor{
   238  	descriptorpb.FieldDescriptorProto_TYPE_DOUBLE:   {name:"DOUBLE", typ: DOUBLE},
   239  	descriptorpb.FieldDescriptorProto_TYPE_FLOAT:   {name:"FLOAT",typ: FLOAT},
   240  	descriptorpb.FieldDescriptorProto_TYPE_INT64:   {name:"INT64",typ: INT64},
   241  	descriptorpb.FieldDescriptorProto_TYPE_UINT64:  {name:"UINT64", typ: UINT64},
   242  	descriptorpb.FieldDescriptorProto_TYPE_INT32:   {name:"INT32", typ: INT32},
   243  	descriptorpb.FieldDescriptorProto_TYPE_FIXED64: {name:"FIX64", typ: FIX64},
   244  	descriptorpb.FieldDescriptorProto_TYPE_FIXED32: {name:"FIX32", typ: FIX32},
   245  	descriptorpb.FieldDescriptorProto_TYPE_BOOL:    {name:"BOOL", typ: BOOL},
   246  	descriptorpb.FieldDescriptorProto_TYPE_STRING:  {name:"STRING", typ: STRING},
   247  	descriptorpb.FieldDescriptorProto_TYPE_MESSAGE: {name:"MESSAGE", typ: MESSAGE},
   248  	descriptorpb.FieldDescriptorProto_TYPE_GROUP:   {name:"GROUP", typ: GROUP}, // deprecated
   249  	descriptorpb.FieldDescriptorProto_TYPE_BYTES:   {name:"BYTE", typ: BYTE},
   250  	descriptorpb.FieldDescriptorProto_TYPE_UINT32:  {name:"UINT32", typ: UINT32},
   251  	descriptorpb.FieldDescriptorProto_TYPE_ENUM:    {name:"ENUM", typ: ENUM},
   252  	descriptorpb.FieldDescriptorProto_TYPE_SFIXED32: {name:"SFIX32", typ: SFIX32},
   253  	descriptorpb.FieldDescriptorProto_TYPE_SFIXED64: {name:"SFIX64", typ: SFIX64},
   254  	descriptorpb.FieldDescriptorProto_TYPE_SINT32:   {name:"SINT32", typ: SINT32},
   255  	descriptorpb.FieldDescriptorProto_TYPE_SINT64:   {name:"SINT64", typ: SINT64},
   256  
   257  }
   258