forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.go
109 lines (92 loc) · 5.05 KB
/
proto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package pgs
import "github.com/golang/protobuf/protoc-gen-go/descriptor"
// Syntax describes the proto syntax used to encode the proto file
type Syntax string
const (
// Proto2 syntax permits the use of "optional" and "required" prefixes on
// fields. Most of the field types in the generated go structs are pointers.
// See: https://developers.google.com/protocol-buffers/docs/proto
Proto2 Syntax = "proto2"
// Proto3 syntax only allows for optional fields, but defaults to the zero
// value of that particular type. Most of the field types in the generated go
// structs are value types.
// See: https://developers.google.com/protocol-buffers/docs/proto3
Proto3 Syntax = "proto3"
)
// SupportsRequiredPrefix returns true if s supports "optional" and
// "required" identifiers on message fields. Only Proto2 syntax supports this
// feature.
func (s Syntax) SupportsRequiredPrefix() bool { return s == Proto2 }
// ProtoLabel wraps the FieldDescriptorProto_Label enum for better readability.
// It is a 1-to-1 conversion.
type ProtoLabel descriptor.FieldDescriptorProto_Label
const (
// Optional (in the context of Proto2 syntax) identifies that the field may
// be unset in the proto message. In Proto3 syntax, all fields are considered
// Optional and default to their zero value.
Optional ProtoLabel = ProtoLabel(descriptor.FieldDescriptorProto_LABEL_OPTIONAL)
// Required (in the context of Proto2 syntax) identifies that the field must
// be set in the proto message. In Proto3 syntax, no fields can be identified
// as Required.
Required ProtoLabel = ProtoLabel(descriptor.FieldDescriptorProto_LABEL_REQUIRED)
// Repeated identifies that the field either permits multiple entries
// (repeated) or is a map (map<key,val>). Determining which requires further
// evaluation of the descriptor and whether or not the embedded message is
// identified as a MapEntry (see IsMap on FieldType).
Repeated ProtoLabel = ProtoLabel(descriptor.FieldDescriptorProto_LABEL_REPEATED)
)
// Proto returns the FieldDescriptorProto_Label for this ProtoLabel. This
// method is exclusively used to improve readability without having to switch
// the types.
func (pl ProtoLabel) Proto() descriptor.FieldDescriptorProto_Label {
return descriptor.FieldDescriptorProto_Label(pl)
}
// ProtoType wraps the FieldDescriptorProto_Type enum for better readability
// and utility methods. It is a 1-to-1 conversion.
type ProtoType descriptor.FieldDescriptorProto_Type
// 1-to-1 mapping of FieldDescriptorProto_Type enum to ProtoType. While all are
// listed here, group types are not supported by this library.
const (
DoubleT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_DOUBLE)
FloatT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_FLOAT)
Int64T ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_INT64)
UInt64T ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_UINT64)
Int32T ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_INT32)
Fixed64T ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_FIXED64)
Fixed32T ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_FIXED32)
BoolT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_BOOL)
StringT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_STRING)
GroupT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_GROUP)
MessageT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_MESSAGE)
BytesT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_BYTES)
UInt32T ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_UINT32)
EnumT ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_ENUM)
SFixed32 ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_SFIXED32)
SFixed64 ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_SFIXED64)
SInt32 ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_SINT32)
SInt64 ProtoType = ProtoType(descriptor.FieldDescriptorProto_TYPE_SINT64)
)
// IsInt returns true if pt maps to an integer-like type. While EnumT types in
// Go are aliases of uint32, to correctly accomodate other languages with
// non-numeric enums, IsInt returns false for EnumT.
func (pt ProtoType) IsInt() bool {
switch pt {
case Int64T, UInt64T, SFixed64, SInt64, Fixed64T,
Int32T, UInt32T, SFixed32, SInt32, Fixed32T:
return true
}
return false
}
// IsNumeric returns true if pt maps to a numeric type. While EnumT types in Go
// are aliases of uint32, to correctly accomodate other languages with non-numeric
// enums, IsNumeric returns false for EnumT.
func (pt ProtoType) IsNumeric() bool { return pt == DoubleT || pt == FloatT || pt.IsInt() }
// IsSlice returns true if the type is represented as a slice/array. At this
// time, only BytesT satisfies this condition.
func (pt ProtoType) IsSlice() bool { return pt == BytesT }
// Proto returns the FieldDescriptorProto_Type for this ProtoType. This
// method is exclusively used to improve readability without having to switch
// the types.
func (pt ProtoType) Proto() descriptor.FieldDescriptorProto_Type {
return descriptor.FieldDescriptorProto_Type(pt)
}