-
Notifications
You must be signed in to change notification settings - Fork 0
/
aip.go
174 lines (155 loc) · 4.85 KB
/
aip.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package util
import (
"strings"
"github.com/einride/protoc-gen-go-aip-test/internal/aip/method"
"github.com/einride/protoc-gen-go-aip-test/internal/xrange"
"github.com/stoewer/go-strcase"
"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/aip/resourcename"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protopath"
"google.golang.org/protobuf/reflect/protoreflect"
)
func HasUserSettableIDField(r *annotations.ResourceDescriptor, m protoreflect.MessageDescriptor) bool {
idField := strcase.LowerCamelCase(r.GetSingular()) + "_id"
return m.Fields().ByName(protoreflect.Name(idField)) != nil
}
func HasParent(r *annotations.ResourceDescriptor) bool {
return len(resourceNameSegments(r.GetPattern()[0])) > 3
}
func WildcardResourceName(r *annotations.ResourceDescriptor) string {
patternSgments := resourceNameSegments(r.GetPattern()[0])
nameSegments := make([]string, 0, len(patternSgments))
for _, segment := range patternSgments {
if segment.IsVariable() {
nameSegments = append(nameSegments, resourcename.Wildcard)
} else {
nameSegments = append(nameSegments, string(segment))
}
}
return strings.Join(nameSegments, "/")
}
func resourceNameSegments(pattern string) []resourcename.Segment {
var s resourcename.Scanner
s.Init(pattern)
segments := make([]resourcename.Segment, 0)
for s.Scan() {
segments = append(segments, s.Segment())
}
return segments
}
func HasAnyStandardMethodFor(s protoreflect.ServiceDescriptor, r *annotations.ResourceDescriptor) bool {
for _, resource := range method.NewMethods(s).Resources() {
if resource.GetType() == r.GetType() {
return true
}
}
return false
}
func ReturnsLRO(method protoreflect.MethodDescriptor) bool {
return method.Output().FullName() == "google.longrunning.Operation"
}
func IsAlternativeBatch(method protoreflect.MethodDescriptor) bool {
switch {
case strings.HasPrefix(string(method.Name()), "BatchGet"):
return IsAlternativeBatchGet(method)
case strings.HasPrefix(string(method.Name()), "BatchDelete"):
return IsAlternativeBatchDelete(method)
default:
return false
}
}
func IsAlternativeBatchGet(method protoreflect.MethodDescriptor) bool {
if !strings.HasPrefix(string(method.Name()), "BatchGet") {
return false
}
inputFields := method.Input().Fields()
return inputFields.ByName("requests") != nil
}
func IsAlternativeBatchDelete(method protoreflect.MethodDescriptor) bool {
if !strings.HasPrefix(string(method.Name()), "BatchDelete") {
return false
}
inputFields := method.Input().Fields()
return inputFields.ByName("requests") != nil
}
func HasUpdateMask(method protoreflect.MethodDescriptor) bool {
if !strings.HasPrefix(string(method.Name()), "Update") {
return false
}
return method.Input().Fields().ByName("update_mask") != nil
}
func HasRequiredFields(message protoreflect.MessageDescriptor) bool {
var found bool
RangeRequiredFields(message, func(_ protopath.Path, _ protoreflect.FieldDescriptor) {
found = true
})
return found
}
func RangeRequiredFields(
message protoreflect.MessageDescriptor,
f func(protopath.Path, protoreflect.FieldDescriptor),
) {
xrange.RangeFields(
message,
func(p protopath.Path, field protoreflect.FieldDescriptor) {
if fieldbehavior.Has(field, annotations.FieldBehavior_REQUIRED) {
f(p, field)
}
},
)
}
func HasMutableResourceReferences(message protoreflect.MessageDescriptor) bool {
var found bool
RangeMutableResourceReferences(
message,
func(_ protopath.Path, _ protoreflect.FieldDescriptor, _ *annotations.ResourceReference) {
found = true
},
)
return found
}
func RangeMutableResourceReferences(
message protoreflect.MessageDescriptor,
f func(protopath.Path, protoreflect.FieldDescriptor, *annotations.ResourceReference),
) {
xrange.RangeResourceReferences(
message,
func(p protopath.Path, field protoreflect.FieldDescriptor, r *annotations.ResourceReference) {
if fieldbehavior.Has(field, annotations.FieldBehavior_OUTPUT_ONLY) {
return
}
f(p, field, r)
},
)
}
func PathChainGet(p protopath.Path) string {
gg := make([]string, 0, len(p))
for _, step := range p {
g := "Get" + strcase.UpperCamelCase(string(step.FieldDescriptor().Name())) + "()"
gg = append(gg, g)
}
return strings.Join(gg, ".")
}
func FindResourceField(
message protoreflect.MessageDescriptor,
resource *annotations.ResourceDescriptor,
) protoreflect.FieldDescriptor {
for i := 0; i < message.Fields().Len(); i++ {
field := message.Fields().Get(i)
if field.Kind() == protoreflect.MessageKind {
r := getResourceDescriptor(field.Message())
if r != nil && r.GetType() == resource.GetType() {
return field
}
}
}
return nil
}
func getResourceDescriptor(message protoreflect.MessageDescriptor) *annotations.ResourceDescriptor {
return proto.GetExtension(
message.Options(),
annotations.E_Resource,
).(*annotations.ResourceDescriptor)
}