-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_message.go
More file actions
192 lines (166 loc) · 5.14 KB
/
dump_message.go
File metadata and controls
192 lines (166 loc) · 5.14 KB
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 protoss
import (
"fmt"
"strconv"
"strings"
"google.golang.org/protobuf/types/descriptorpb"
)
func (fd *filedumper) dump_message_definition(message_descriptor *descriptorpb.DescriptorProto) (err error) {
original_scope := fd.package_scope
fd.package_scope += fmt.Sprintf(".%s", message_descriptor.GetName())
// Open message definition
fd.indent()
fd.printf("message %s {\n", message_descriptor.GetName())
fd.line_indentation++
// Encode enums
for _, enum_type := range message_descriptor.EnumType {
if err = fd.dump_enum_definition(enum_type); err != nil {
return
}
}
// Encode nested types
for _, nested_type := range message_descriptor.NestedType {
if err = fd.dump_message_definition(nested_type); err != nil {
return
}
}
was_writing_oneof := false
for _, field := range message_descriptor.Field {
is_oneof := field.OneofIndex != nil
label_prefix := ""
if is_oneof {
if !was_writing_oneof {
fd.indent()
fd.printf("oneof %s {\n", *message_descriptor.OneofDecl[field.GetOneofIndex()].Name)
fd.line_indentation++
was_writing_oneof = true
}
} else {
if was_writing_oneof {
fd.line_indentation--
fd.indent()
fd.printf("}\n")
was_writing_oneof = false
}
label := field.GetLabel()
switch label {
case descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL:
label_prefix = "optional"
// all fields are optional in proto3
if fd.syntax == "proto3" {
label_prefix = ""
}
case descriptorpb.FieldDescriptorProto_LABEL_REPEATED:
label_prefix = "repeated"
case descriptorpb.FieldDescriptorProto_LABEL_REQUIRED:
if fd.syntax == "proto3" {
return fmt.Errorf("protoss: '%s': a field cannot be required in proto3", fd.file_descriptor.GetName())
}
label_prefix = "required"
default:
panic(label.String())
}
if label_prefix != "" {
label_prefix += " "
}
}
type_name := field.GetTypeName()
if type_name == "" {
switch field.GetType() {
case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE:
type_name = "double"
case descriptorpb.FieldDescriptorProto_TYPE_FLOAT:
type_name = "float"
case descriptorpb.FieldDescriptorProto_TYPE_INT64:
type_name = "int64"
case descriptorpb.FieldDescriptorProto_TYPE_UINT64:
type_name = "uint64"
case descriptorpb.FieldDescriptorProto_TYPE_INT32:
type_name = "int32"
case descriptorpb.FieldDescriptorProto_TYPE_FIXED64:
type_name = "fixed64"
case descriptorpb.FieldDescriptorProto_TYPE_FIXED32:
type_name = "fixed32"
case descriptorpb.FieldDescriptorProto_TYPE_BOOL:
type_name = "bool"
case descriptorpb.FieldDescriptorProto_TYPE_STRING:
type_name = "string"
case descriptorpb.FieldDescriptorProto_TYPE_GROUP:
type_name = "group"
case descriptorpb.FieldDescriptorProto_TYPE_MESSAGE:
type_name = "message"
case descriptorpb.FieldDescriptorProto_TYPE_BYTES:
type_name = "bytes"
case descriptorpb.FieldDescriptorProto_TYPE_UINT32:
type_name = "uint32"
case descriptorpb.FieldDescriptorProto_TYPE_ENUM:
type_name = "enum"
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED32:
type_name = "sfixed32"
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED64:
type_name = "sfixed64"
case descriptorpb.FieldDescriptorProto_TYPE_SINT32:
type_name = "sint32"
case descriptorpb.FieldDescriptorProto_TYPE_SINT64:
type_name = "sint64"
default:
panic(field.GetType())
}
} else {
type_name, err = fd.resolve_type_name_in_scope(type_name)
if err != nil {
return
}
}
// if type_name == "" {
// // no type name
// type_name = field.Type.
// }
// if field.OneofIndex != nil {
// oneof := message_def.OneofDecl[*field.OneofIndex]
// oneof.Options.
// }
number := field.GetNumber()
fd.indent()
fd.printf("%s%s %s = %d", label_prefix, type_name, field.GetName(), number)
var options []string
if field.Options != nil {
if field.Options.GetDeprecated() {
options = append(options, fmt.Sprintf("deprecated = %t", field.Options.GetDeprecated()))
}
}
if field.DefaultValue != nil {
default_string := field.GetDefaultValue()
if field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_STRING {
default_string = strconv.Quote(default_string)
}
options = append(options, fmt.Sprintf("default = %s", default_string))
}
if len(options) > 0 {
fd.printf(" [%s]", strings.Join(options, ", "))
}
fd.printf(";\n")
}
if was_writing_oneof {
fd.line_indentation--
fd.indent()
fd.printf("}\n")
was_writing_oneof = false
}
for _, extension_range := range message_descriptor.GetExtensionRange() {
upper_range := extension_range.GetEnd()
upper_string := ""
if upper_range == 536870912 {
upper_string = "max"
} else {
upper_string = fmt.Sprintf("%d", upper_range)
}
fd.indent()
fd.printf("extensions %d to %s;\n", extension_range.GetStart(), upper_string)
}
fd.line_indentation--
fd.indent()
fd.printf("}\n")
fd.package_scope = original_scope
return
}