/
method.go
192 lines (164 loc) · 4.72 KB
/
method.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package util
import (
"github.com/stoewer/go-strcase"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen"
)
type MethodCreate struct {
Resource *annotations.ResourceDescriptor
Method *protogen.Method
Parent string
Message string
UserSettableID string
}
func (m MethodCreate) Generate(f *protogen.GeneratedFile, response, err, assign string) {
userSetID := m.UserSettableID
if userSetID == "" && HasUserSettableIDField(m.Resource, m.Method.Input.Desc) {
userSetID = "userSetID"
f.P(userSetID + " := \"\"")
f.P("if fx.IDGenerator != nil {")
f.P(userSetID + " = fx.IDGenerator()")
f.P("}")
}
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.ctx, &", m.Method.Input.GoIdent, "{")
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
upper := strcase.UpperCamelCase(string(FindResourceField(
m.Method.Input.Desc,
m.Resource,
).Name()))
switch {
case m.Message != "":
f.P(upper, ": ", m.Message, ",")
case !HasParent(m.Resource):
f.P(upper, ": fx.Create(),")
default:
f.P(upper, ": fx.Create(", m.Parent, "),")
}
if userSetID != "" && HasUserSettableIDField(m.Resource, m.Method.Input.Desc) {
f.P(upper, "Id: ", userSetID, ",")
}
f.P("})")
}
type MethodGet struct {
Resource *annotations.ResourceDescriptor
Method *protogen.Method
Name string
}
func (m MethodGet) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.ctx, &", m.Method.Input.GoIdent, "{")
f.P("Name: ", m.Name, ",")
f.P("})")
}
type MethodBatchGet struct {
Resource *annotations.ResourceDescriptor
Method *protogen.Method
Parent string
Names []string
}
func (m MethodBatchGet) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.ctx, &", m.Method.Input.GoIdent, "{")
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
f.P("Names: []string{")
for _, name := range m.Names {
f.P(name, ",")
}
f.P("},")
f.P("})")
}
type MethodUpdate struct {
Resource *annotations.ResourceDescriptor
Method *protogen.Method
// set either Parent + Name, or Msg
Name string
Parent string
Msg string
UpdateMask []string
}
func (m MethodUpdate) Generate(f *protogen.GeneratedFile, response, err, assign string) {
upper := strcase.UpperCamelCase(string(FindResourceField(
m.Method.Input.Desc,
m.Resource,
).Name()))
if m.Msg == "" {
if HasParent(m.Resource) {
f.P("msg := fx.Update(", m.Parent, ")")
} else {
f.P("msg := fx.Update()")
}
f.P("msg.Name = ", m.Name)
}
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.ctx, &", m.Method.Input.GoIdent, "{")
if m.Msg != "" {
f.P(upper, ":", m.Msg, ",")
} else {
f.P(upper, ": msg,")
}
if HasUpdateMask(m.Method.Desc) && len(m.UpdateMask) > 0 {
fieldmaskpbFieldMask := f.QualifiedGoIdent(protogen.GoIdent{
GoName: "FieldMask",
GoImportPath: "google.golang.org/protobuf/types/known/fieldmaskpb",
})
f.P("UpdateMask: &", fieldmaskpbFieldMask, "{")
f.P("Paths: []string{")
for _, path := range m.UpdateMask {
f.P(path, ",")
}
f.P("},")
f.P("},")
}
f.P("})")
}
type MethodList struct {
Resource *annotations.ResourceDescriptor
Method *protogen.Method
Parent string
PageSize string
PageToken string
}
func (m MethodList) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.ctx, &", m.Method.Input.GoIdent, "{")
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
if m.PageSize != "" {
f.P("PageSize: ", m.PageSize, ",")
}
if m.PageToken != "" {
f.P("PageToken: ", m.PageToken, ",")
}
f.P("})")
}
type MethodSearch struct {
Resource *annotations.ResourceDescriptor
Method *protogen.Method
Parent string
PageSize string
PageToken string
}
func (m MethodSearch) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.ctx, &", m.Method.Input.GoIdent, "{")
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
if m.PageSize != "" {
f.P("PageSize: ", m.PageSize, ",")
}
if m.PageToken != "" {
f.P("PageToken: ", m.PageToken, ",")
}
f.P("})")
}
type MethodDelete struct {
Resource *annotations.ResourceDescriptor
Method *protogen.Method
Name string
}
func (m MethodDelete) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.ctx, &", m.Method.Input.GoIdent, "{")
f.P("Name: ", m.Name, ",")
f.P("})")
}