-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfield_descriptor_proto_ext.go
104 lines (81 loc) · 3.32 KB
/
field_descriptor_proto_ext.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
/*
Copyright 2015-2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"strings"
"github.com/gogo/protobuf/gogoproto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
)
const (
// protobufTimestampTypeName gogo protobuf timestamp type name
protobufTimestampTypeName = "google.protobuf.Timestamp"
// protobufDurationTypeName gogo protobuf duration type name
protobufDurationTypeName = "google.protobuf.Duration"
)
// FieldDescriptorProtoExt adds some useful extension methods to gogo protobuf field descriptor
type FieldDescriptorProtoExt struct {
*descriptor.FieldDescriptorProto
}
// IsTypeEq returns true if type equals current field descriptor type
func (f *FieldDescriptorProtoExt) IsTypeEq(t descriptor.FieldDescriptorProto_Type) bool {
return *f.Type == t
}
// IsTime returns true if field stores a time value (protobuf or standard library)
func (f *FieldDescriptorProtoExt) IsTime() bool {
t := f.TypeName
isStdTime := gogoproto.IsStdTime(f.FieldDescriptorProto)
isGoogleTime := (t != nil && strings.HasSuffix(*t, protobufTimestampTypeName))
isCastToTime := f.GetCastType() == "time.Time"
return isStdTime || isGoogleTime || isCastToTime
}
// IsDuration returns true if field stores a duration value (protobuf or cast to a standard library type)
func (f *FieldDescriptorProtoExt) IsDuration(durationCustomType string) bool {
ct := f.GetCastType()
t := f.TypeName
isStdDuration := gogoproto.IsStdDuration(f.FieldDescriptorProto)
isGoogleDuration := (t != nil && strings.HasSuffix(*t, protobufDurationTypeName))
isCastToCustomDuration := durationCustomType != "" && ct == durationCustomType
isCastToDuration := ct == "time.Duration"
return isStdDuration || isGoogleDuration || isCastToDuration || isCastToCustomDuration
}
// IsMessage returns true if field is a message
func (f *FieldDescriptorProtoExt) IsMessage() bool {
return f.IsTypeEq(descriptor.FieldDescriptorProto_TYPE_MESSAGE)
}
// IsCastType returns true if field has gogoproto.casttype flag
func (f *FieldDescriptorProtoExt) IsCastType() bool {
return gogoproto.IsCastType(f.FieldDescriptorProto)
}
// IsCustomType returns true if field has gogoproto.customtype flag
func (f *FieldDescriptorProtoExt) IsCustomType() bool {
return gogoproto.IsCustomType(f.FieldDescriptorProto)
}
// GetCastType returns field cast type name
func (f *FieldDescriptorProtoExt) GetCastType() string {
return gogoproto.GetCastType(f.FieldDescriptorProto)
}
// GetCustomType returns field custom type name
func (f *FieldDescriptorProtoExt) GetCustomType() string {
return gogoproto.GetCustomType(f.FieldDescriptorProto)
}
// GetJSONName returns JSON name set in JSON tag
func (f *FieldDescriptorProtoExt) GetJSONName() string {
t := gogoproto.GetJsonTag(f.FieldDescriptorProto)
if t != nil {
j := strings.Split(*t, ",")
if j[0] != "-" {
return j[0]
}
}
return ""
}