OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """protoc plugin to create C++ reader/writer for JSON-encoded protobufs | 6 """protoc plugin to create C++ reader/writer for JSON-encoded protobufs |
7 | 7 |
8 The reader/writer use Chrome's base::Values. | 8 The reader/writer use Chrome's base::Values. |
9 """ | 9 """ |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 self.Output('#include "{output_dir}{generated_pb_h}"', | 26 self.Output('#include "{output_dir}{generated_pb_h}"', |
27 output_dir=output_dir + '/' if output_dir else '', | 27 output_dir=output_dir + '/' if output_dir else '', |
28 generated_pb_h=proto_file.CppBaseHeader()) | 28 generated_pb_h=proto_file.CppBaseHeader()) |
29 self.Output('') | 29 self.Output('') |
30 | 30 |
31 # import is not supported | 31 # import is not supported |
32 assert [] == proto_file.GetDependencies() | 32 assert [] == proto_file.GetDependencies() |
33 | 33 |
34 self.Output('// base dependencies') | 34 self.Output('// base dependencies') |
| 35 self.Output('#include "base/memory/ptr_util.h"') |
35 self.Output('#include "base/values.h"') | 36 self.Output('#include "base/values.h"') |
36 self.Output('') | 37 self.Output('') |
37 self.Output('#include <memory>') | 38 self.Output('#include <memory>') |
38 self.Output('#include <string>') | 39 self.Output('#include <string>') |
39 self.Output('#include <utility>') | 40 self.Output('#include <utility>') |
40 self.Output('') | 41 self.Output('') |
41 | 42 |
42 namespaces = proto_file.ProtoNamespaces() + ['json'] | 43 namespaces = proto_file.ProtoNamespaces() + ['json'] |
43 for name in namespaces: | 44 for name in namespaces: |
44 self.Output('namespace {name} {{', name=name) | 45 self.Output('namespace {name} {{', name=name) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 with self.AddIndent(): | 110 with self.AddIndent(): |
110 if field.IsRepeated(): | 111 if field.IsRepeated(): |
111 self.RepeatedMemberFieldWriteToValue(field) | 112 self.RepeatedMemberFieldWriteToValue(field) |
112 else: | 113 else: |
113 self.OptionalMemberFieldWriteToValue(field) | 114 self.OptionalMemberFieldWriteToValue(field) |
114 | 115 |
115 self.Output('}}') | 116 self.Output('}}') |
116 | 117 |
117 def RepeatedMemberFieldWriteToValue(self, field): | 118 def RepeatedMemberFieldWriteToValue(self, field): |
118 prologue = ( | 119 prologue = ( |
119 'base::ListValue* field_list = new base::ListValue();\n' | 120 'auto field_list = base::MakeUnique<base::ListValue>();\n' |
120 'dict->Set("{field_number}", field_list);\n' | |
121 'for (int i = 0; i < message.{field_name}_size(); ++i) {{\n' | 121 'for (int i = 0; i < message.{field_name}_size(); ++i) {{\n' |
122 ) | 122 ) |
123 | 123 |
124 if field.IsClassType(): | 124 if field.IsClassType(): |
125 middle = ( | 125 middle = ( |
126 'std::unique_ptr<base::Value> inner_message_value = \n' | 126 'std::unique_ptr<base::Value> inner_message_value = \n' |
127 ' {inner_class_converter}::WriteToValue(message.{field_name}(i));\n
' | 127 ' {inner_class_converter}::WriteToValue(message.{field_name}(i));\n
' |
128 'field_list->Append(std::move(inner_message_value));\n' | 128 'field_list->Append(std::move(inner_message_value));\n' |
129 ) | 129 ) |
130 else: | 130 else: |
131 middle = ( | 131 middle = ( |
132 'field_list->Append{value_type}(message.{field_name}(i));\n' | 132 'field_list->Append{value_type}(message.{field_name}(i));\n' |
133 ) | 133 ) |
| 134 |
| 135 epilogue = ( |
| 136 '\n}}\n' |
| 137 'dict->Set("{field_number}", std::move(field_list));' |
| 138 ) |
134 self.Output( | 139 self.Output( |
135 prologue + Indented(middle) + '\n}}', | 140 prologue + Indented(middle) + epilogue, |
136 field_number=field.JavascriptIndex(), | 141 field_number=field.JavascriptIndex(), |
137 field_name=field.name, | 142 field_name=field.name, |
138 value_type=field.CppValueType() if not field.IsClassType() else None, | 143 value_type=field.CppValueType() if not field.IsClassType() else None, |
139 inner_class_converter=field.CppConverterType() | 144 inner_class_converter=field.CppConverterType() |
140 ) | 145 ) |
141 | 146 |
142 def OptionalMemberFieldWriteToValue(self, field): | 147 def OptionalMemberFieldWriteToValue(self, field): |
143 if field.IsClassType(): | 148 if field.IsClassType(): |
144 body = ( | 149 body = ( |
145 'std::unique_ptr<base::Value> inner_message_value = \n' | 150 'std::unique_ptr<base::Value> inner_message_value = \n' |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 280 |
276 response.AddFileWithContent(converter_filename, cppwriter.GetValue()) | 281 response.AddFileWithContent(converter_filename, cppwriter.GetValue()) |
277 if cppwriter.GetErrors(): | 282 if cppwriter.GetErrors(): |
278 response.AddError('\n'.join(cppwriter.GetErrors())) | 283 response.AddError('\n'.join(cppwriter.GetErrors())) |
279 | 284 |
280 response.WriteToStdout() | 285 response.WriteToStdout() |
281 | 286 |
282 | 287 |
283 if __name__ == '__main__': | 288 if __name__ == '__main__': |
284 main() | 289 main() |
OLD | NEW |