OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from google.protobuf.descriptor_pb2 import FieldDescriptorProto |
| 6 |
| 7 import plugin |
| 8 |
| 9 _cpp_base_type_map = { |
| 10 FieldDescriptorProto.TYPE_DOUBLE: 'double', |
| 11 FieldDescriptorProto.TYPE_FLOAT: 'float', |
| 12 FieldDescriptorProto.TYPE_INT64: None, |
| 13 FieldDescriptorProto.TYPE_UINT64: None, |
| 14 FieldDescriptorProto.TYPE_INT32: 'int', |
| 15 FieldDescriptorProto.TYPE_FIXED64: None, |
| 16 FieldDescriptorProto.TYPE_FIXED32: None, |
| 17 FieldDescriptorProto.TYPE_BOOL: 'bool', |
| 18 FieldDescriptorProto.TYPE_STRING: 'std::string', |
| 19 FieldDescriptorProto.TYPE_GROUP: None, |
| 20 FieldDescriptorProto.TYPE_MESSAGE: None, |
| 21 FieldDescriptorProto.TYPE_BYTES: None, |
| 22 FieldDescriptorProto.TYPE_UINT32: None, |
| 23 FieldDescriptorProto.TYPE_ENUM: 'int', |
| 24 FieldDescriptorProto.TYPE_SFIXED32: None, |
| 25 FieldDescriptorProto.TYPE_SFIXED64: None, |
| 26 FieldDescriptorProto.TYPE_SINT32: None, |
| 27 FieldDescriptorProto.TYPE_SINT64: None, |
| 28 } |
| 29 |
| 30 # Maps c++ types to names used in base::Value methods (like GetDouble, |
| 31 # GetInteger, etc). |
| 32 _cpp_type_to_value_type_map = { |
| 33 'double': 'Double', |
| 34 'float': 'Double', |
| 35 'int': 'Integer', |
| 36 'bool': 'Boolean', |
| 37 'std::string': 'String', |
| 38 } |
| 39 |
| 40 |
| 41 def GetCppPrimitiveType(type_name): |
| 42 return _cpp_base_type_map[type_name] |
| 43 |
| 44 |
| 45 def GetCppValueType(primitive_type): |
| 46 return _cpp_type_to_value_type_map[primitive_type] |
| 47 |
| 48 |
| 49 # TYPE_ENUM and TYPE_MESSAGE are supported, but their types depend on type_name. |
| 50 _java_base_type_map = { |
| 51 FieldDescriptorProto.TYPE_DOUBLE: 'double', |
| 52 FieldDescriptorProto.TYPE_FLOAT: 'float', |
| 53 FieldDescriptorProto.TYPE_INT64: None, |
| 54 FieldDescriptorProto.TYPE_UINT64: None, |
| 55 FieldDescriptorProto.TYPE_INT32: 'int', |
| 56 FieldDescriptorProto.TYPE_FIXED64: None, |
| 57 FieldDescriptorProto.TYPE_FIXED32: None, |
| 58 FieldDescriptorProto.TYPE_BOOL: 'boolean', |
| 59 FieldDescriptorProto.TYPE_STRING: 'String', |
| 60 FieldDescriptorProto.TYPE_GROUP: None, |
| 61 FieldDescriptorProto.TYPE_MESSAGE: None, |
| 62 FieldDescriptorProto.TYPE_BYTES: None, |
| 63 FieldDescriptorProto.TYPE_UINT32: None, |
| 64 FieldDescriptorProto.TYPE_ENUM: None, |
| 65 FieldDescriptorProto.TYPE_SFIXED32: None, |
| 66 FieldDescriptorProto.TYPE_SFIXED64: None, |
| 67 FieldDescriptorProto.TYPE_SINT32: None, |
| 68 FieldDescriptorProto.TYPE_SINT64: None, |
| 69 } |
| 70 |
| 71 |
| 72 # Maps java primitive types to java types usable in containers (e.g. |
| 73 # List<Integer>). |
| 74 _java_primitive_to_object_map = { |
| 75 'boolean': 'Boolean', |
| 76 'int': 'Integer', |
| 77 'long': 'Long', |
| 78 'float': 'Float', |
| 79 'double': 'Double', |
| 80 } |
| 81 |
| 82 |
| 83 def GetJavaPrimitiveType(field_type): |
| 84 return _java_base_type_map[field_type] |
| 85 |
| 86 |
| 87 def GetJavaObjectType(java_base_type): |
| 88 if not java_base_type in _java_primitive_to_object_map: |
| 89 return java_base_type |
| 90 return _java_primitive_to_object_map[java_base_type] |
| 91 |
| 92 |
| 93 _proto_cpp_converter_class_map = {} |
| 94 _proto_java_class_map = {} |
| 95 |
| 96 def ResolveCppConverterType(s): |
| 97 if s.startswith('.'): |
| 98 s = s[1:] |
| 99 return _proto_cpp_converter_class_map[s] |
| 100 return s |
| 101 |
| 102 |
| 103 def ResolveJavaClassType(s): |
| 104 if s.startswith('.'): |
| 105 s = s[1:] |
| 106 return _proto_java_class_map[s] |
| 107 return s |
| 108 |
| 109 |
| 110 class QualifiedTypes(object): |
| 111 def __init__(self, proto, java, cpp_base, cpp_converter): |
| 112 self.proto = proto |
| 113 self.java = java |
| 114 |
| 115 # cpp_base is the standard protoc-generated class for this type. |
| 116 self.cpp_base = cpp_base |
| 117 self.cpp_converter = cpp_converter |
| 118 |
| 119 def Register(self): |
| 120 _proto_cpp_converter_class_map[self.proto] = self.cpp_converter |
| 121 _proto_java_class_map[self.proto] = self.java |
| 122 |
| 123 |
| 124 def QualifiedTypesForChild(name, parent_typenames): |
| 125 title_name = plugin.TitleCase(name) |
| 126 proto = parent_typenames.proto + '.' + name |
| 127 java = parent_typenames.java + '.' + title_name |
| 128 cpp_base = parent_typenames.cpp_base + '::' + title_name |
| 129 cpp_converter = parent_typenames.cpp_converter + '::' + title_name |
| 130 return QualifiedTypes(proto, java, cpp_base, cpp_converter) |
| 131 |
| 132 |
| 133 def RegisterTypesForEnum(proto_enum): |
| 134 proto_enum.QualifiedTypes().Register() |
| 135 |
| 136 |
| 137 def RegisterTypesForMessage(proto_message): |
| 138 proto_message.QualifiedTypes().Register() |
| 139 for enum in proto_message.GetEnums(): |
| 140 RegisterTypesForEnum(enum) |
| 141 for child in proto_message.GetMessages(): |
| 142 RegisterTypesForMessage(child) |
| 143 |
| 144 |
| 145 def RegisterTypesForFile(proto_file): |
| 146 for enum in proto_file.GetEnums(): |
| 147 RegisterTypesForEnum(enum) |
| 148 for message in proto_file.GetMessages(): |
| 149 RegisterTypesForMessage(message) |
| 150 |
OLD | NEW |