/
message.go
64 lines (52 loc) · 1.29 KB
/
message.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
package generator
import (
"io"
"github.com/bold-commerce/protoc-gen-struct-transformer/source"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
)
// processMessage processes each message regardless of contains it an options or
// it doesn't. It returns set of fields for template and destination structure
// name extracted from proto message go_struct option.
func processMessage(
w io.Writer,
msg *descriptor.DescriptorProto,
subMessages map[string]MessageOption,
str source.StructureList,
debug bool,
) ([]Field, string, error) {
structName, err := extractStructNameOption(msg)
if err != nil {
if msg != nil {
for _, d := range msg.OneofDecl {
p(w, "// Oneof: %#v\n\n", *d.Name)
}
}
return nil, "", err
}
tsf, err := source.Lookup(str, structName)
if err != nil {
return nil, "", err
}
debugWriter := (io.Writer)(nil)
if debug {
debugWriter = w
}
p(debugWriter, "%s", tsf)
fields := []Field{}
for _, f := range msg.Field {
pf, err := processField(debugWriter, f, subMessages, tsf)
if err != nil {
if e, ok := err.(loggableError); ok {
p(w, "// %s\n", e)
continue
}
if err != ErrNilOptions {
return nil, "", err
}
p(w, "// error: %s\n", err)
continue
}
fields = append(fields, *pf)
}
return fields, structName, nil
}