forked from ZacxDev/protoc-gen-struct-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
332 lines (273 loc) · 8.79 KB
/
field.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package generator
import (
"errors"
"fmt"
"io"
"strings"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/iancoleman/strcase"
"github.com/innovation-upstream/protoc-gen-struct-transformer/options"
"github.com/innovation-upstream/protoc-gen-struct-transformer/source"
pkgerrors "github.com/pkg/errors"
)
// lastName splits string by "." and returns last part.
func lastName(s string) string {
splt := strings.Split(s, ".")
return splt[len(splt)-1]
}
// wktgoogleProtobufTimestamp returns *Field created out of
// google.protobuf.Timestamp protobuf field.
func wktgoogleProtobufTimestamp(pname, gname string, gf source.FieldInfo, pnullable bool) *Field {
p2g := ""
g2p := ""
if gf.Type != "time.Time" {
g := strcase.ToCamel(strings.Replace(gf.Type, ".", "", -1))
p := "Time"
if pnullable {
p += "Ptr"
}
if gf.IsPointer {
g += "Ptr"
}
p2g = fmt.Sprintf("%sTo%s", p, g)
g2p = fmt.Sprintf("%sTo%s", g, p)
}
return &Field{
Name: gname,
ProtoName: pname,
ProtoToGoType: p2g,
GoToProtoType: g2p,
UsePackage: p2g != "",
}
}
// wktgoogleProtobufString returns *Field created out of
// google.protobuf.StringValue field.
func wktgoogleProtobufString(pname, gname, ftype string) *Field {
g := strcase.ToCamel(strings.Replace(ftype, ".", "", -1))
p := "StringValue"
return &Field{
Name: gname,
ProtoName: pname,
ProtoToGoType: fmt.Sprintf("%sTo%s", p, g),
GoToProtoType: fmt.Sprintf("%sTo%s", g, p),
UsePackage: true,
}
}
// processSubMessage processes sub messages of current message. Sub message is
// a message type which is used as field type.
//
// In the next example message B is a current message and message A is sub
// message.
//
// message A {}
// message B { A a_field = 1; }
func processSubMessage(w io.Writer,
fdp *descriptor.FieldDescriptorProto,
pname, gname, pbtype string,
mo MessageOption,
goStructFields source.Structure,
customTransformer bool,
) (*Field, error) {
if fdp == nil {
return nil, errors.New("input field is nil")
}
if fdp.Name == nil {
return nil, errors.New("input field name is nil")
}
tpl := "%sTo%s"
pb := "Pb"
p2g := ""
g2p := ""
if mo != nil {
if mo.OneofDecl() != "" && !customTransformer {
pb = strcase.ToCamel(goStructFields[gname].Type)
} else {
pb, pbtype = mo.Target(), pb
}
pb = strcase.ToCamel(pb)
}
// custom type converter (methods won't be generated)
if customTransformer {
pb = strcase.ToCamel(goStructFields[gname].Type)
if ln := lastName(pb); strings.Contains(pb, ".") {
pb = strcase.ToCamel(ln)
}
ptype := lastName(fdp.GetTypeName())
pbtype = fmt.Sprintf("Pb%s", strcase.ToCamel(ptype))
}
if l := fdp.Label; l != nil && *l == descriptor.FieldDescriptorProto_LABEL_REPEATED {
tpl += "List"
if g, ok := goStructFields[gname]; ok {
pb = strcase.ToCamel(g.Type)
}
}
// embedded fields
fname := gname
if isEmbed := extractEmbedOption(fdp.Options); isEmbed {
// if sub message is embedded use type name as field name.
fname = pb
pb = strcase.ToCamel(pb)
}
if ln := lastName(pbtype); strings.Contains(pbtype, ".") {
pbtype = strcase.ToCamel(ln)
}
isNullable := extractNullOption(fdp)
p2g = fmt.Sprintf(tpl, pbtype, pb)
g2p = fmt.Sprintf(tpl, pb, pbtype)
f := &Field{
Name: strcase.ToCamel(fname),
ProtoName: strcase.ToCamel(*fdp.Name),
ProtoType: pbtype,
ProtoToGoType: p2g,
GoToProtoType: g2p,
Opts: ", opts...",
ProtoIsPointer: isNullable,
}
if fm, ok := goStructFields[gname]; ok {
if mo == nil {
return nil, errors.New("mo is nil")
}
f.GoIsPointer = fm.IsPointer
if !customTransformer {
// OneofDecl is used for the BoldCommerce-specific implementation of OneOf for the migration from Int64ToString
f.OneofDecl = mo.OneofDecl()
}
}
return f, nil
}
// processSimpleField processes fields of basic types such as int, string and
// so on.
func processSimpleField(w io.Writer, pname, gname string, ftype *descriptor.FieldDescriptorProto_Type, sf source.FieldInfo, fdp *descriptor.FieldDescriptorProto) (*Field, error) {
sf.Type = strcase.ToCamel(strings.Replace(sf.Type, ".", "", -1)) // pkg.Type => PkgType
t := types[*ftype]
// sf: NullsString, pbType: , goType: string, ft: TYPE_STRING, name: Tags, pbaname: Tags
p(w, "// sf: %#v, pbType: %q, goType: %q, ft: %q, pname: %q, gname: %q\n",
sf, t.pbType, t.goType, ftype, pname, gname)
sft := strings.ToLower(sf.Type)
tpb := strings.ToLower(t.pbType)
tgo := strings.ToLower(t.goType)
f := &Field{
Name: gname,
ProtoName: pname,
}
switch true {
case (sft == tpb && tpb != "") || (sft == tgo && tpb == ""): // equal types
f.ProtoToGoType = ""
f.GoToProtoType = ""
case sft != tgo:
p := t.pbType
if *ftype == descriptor.FieldDescriptorProto_TYPE_ENUM {
p = fdp.GetTypeName()
}
if p == "" {
p = t.goType
}
f.ProtoToGoType = fmt.Sprintf("%sTo%s", strcase.ToCamel(p), sf.Type)
f.GoToProtoType = fmt.Sprintf("%sTo%s", sf.Type, strcase.ToCamel(p))
f.UsePackage = true
case sft != tpb:
p(w, "// sft: %s, tpb: %s\n", sft, tpb)
f.ProtoToGoType = sft
f.GoToProtoType = tpb
default:
f.ProtoToGoType = t.pbType
f.GoToProtoType = t.goType
f.UsePackage = t.usePackage
}
return f, nil
}
// processField returns filled Field struct for template.
func processField(
w io.Writer,
fdp *descriptor.FieldDescriptorProto,
subMessages MessageOptionList,
goStructFields source.Structure,
) (*Field, error) {
// If field has transformer.skip == true, it will be not processed.
if skip := extractSkipOption(fdp.Options); skip {
return nil, newLoggableError("field skipped: %s", *fdp.Name)
}
mapTo, err := getStringOption(fdp.Options, options.E_MapTo)
if _, ok := err.(errOptionNotExists); err != nil && err != ErrNilOptions && !ok {
return nil, pkgerrors.Wrap(err, "mapTo option")
}
// if field has an options map_as then overwrite fieldName which is pbname
mapAs, err := getStringOption(fdp.Options, options.E_MapAs)
if _, ok := err.(errOptionNotExists); err != nil && err != ErrNilOptions && !ok {
return nil, pkgerrors.Wrap(err, "mapAs option")
}
pname, gname := prepareFieldNames(*fdp.Name, mapAs, mapTo)
// check if field exists in destination/Go structure.
gf, ok := goStructFields[gname]
if !ok {
// do not check for embedded fields.
if isEmbed := extractEmbedOption(fdp.Options); !isEmbed {
return nil, pkgerrors.Wrap(errors.New("field not found in destination structure"), gname)
}
}
p(w, "\n\n// ===============================\n")
if oi := fdp.OneofIndex; oi != nil {
p(w, "// fdp.OneofIndex: %#v\n\n", *oi)
}
if tn := fdp.TypeName; tn != nil {
p(w, "// fdp.TypeName: %#v\n\n", *tn)
}
if opt := fdp.Options; opt != nil {
p(w, "// fdp.Options: %s\n\n", strings.Replace(fmt.Sprintf("%#v", opt), "\n", "", -1))
}
p(w, "// fdp.Name: %q, mapAs: %q, mapTo: %q\n", *fdp.Name, mapAs, mapTo)
// Process subMessages. For details see comments for the TypeName.
if typ := fdp.TypeName; *fdp.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE && typ != nil {
t := *typ
switch t {
case ".google.protobuf.Timestamp":
isNullable := extractNullOption(fdp)
return wktgoogleProtobufTimestamp(pname, gname, gf, isNullable), nil
case ".google.protobuf.StringValue":
return wktgoogleProtobufString(pname, gname, gf.Type), nil
}
// if the field has the custom=true - the custom transformer will be used for this field
customTransformer := getBoolOption(fdp.Options, options.E_Custom)
// Submessage has a name like ".package.type", 1: removes first ".".
mo, _ := subMessages[t[1:]]
// TODO(ekhabarov): pass gf instead of goStructFields
return processSubMessage(w, fdp, pname, gname, t, mo, goStructFields, customTransformer)
}
return processSimpleField(w, pname, gname, fdp.Type, gf, fdp)
}
// abbreviationUpper checks a incoming string for equality and suffixes, if it
// exists it will be converted to uppercase.
// For instance, identifier fields in models often have a name like SomeID, with
// capitalized "ID", while protobuf auto-generated structures use names like
// "SomeId".
// TODO(ekhabarov): Add cli parameter for such mapping.
func abbreviationUpper(name string) string {
abbreviation := []string{"Id", "Sku", "Url"}
for _, a := range abbreviation {
if name == a {
return strings.ToUpper(a)
}
if strings.HasSuffix(name, a) {
return strings.TrimSuffix(name, a) + strings.ToUpper(a)
}
}
return name
}
// prepareFieldNames returns names Protobuf and Go for field, considering
// map_to/map_as options and abbreviation rules.
func prepareFieldNames(fname, mapAs, mapTo string) (string, string) {
pname := fname
if strings.Contains(pname, "_") {
pname = strcase.ToCamel(pname)
} else {
pname = strings.Title(pname)
}
if mapAs != "" {
pname = mapAs
}
gname := abbreviationUpper(pname)
if mapTo != "" {
gname = mapTo
}
return pname, gname
}