objectivec_map_field.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <map>
  31. #include <string>
  32. #include <google/protobuf/compiler/objectivec/objectivec_map_field.h>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. #include <google/protobuf/stubs/substitute.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace compiler {
  41. namespace objectivec {
  42. // MapFieldGenerator uses RepeatedFieldGenerator as the parent because it
  43. // provides a bunch of things (no has* methods, comments for contained type,
  44. // etc.).
  45. namespace {
  46. const char* MapEntryTypeName(const FieldDescriptor* descriptor, bool isKey) {
  47. ObjectiveCType type = GetObjectiveCType(descriptor);
  48. switch (type) {
  49. case OBJECTIVECTYPE_INT32:
  50. return "Int32";
  51. case OBJECTIVECTYPE_UINT32:
  52. return "UInt32";
  53. case OBJECTIVECTYPE_INT64:
  54. return "Int64";
  55. case OBJECTIVECTYPE_UINT64:
  56. return "UInt64";
  57. case OBJECTIVECTYPE_FLOAT:
  58. return "Float";
  59. case OBJECTIVECTYPE_DOUBLE:
  60. return "Double";
  61. case OBJECTIVECTYPE_BOOLEAN:
  62. return "Bool";
  63. case OBJECTIVECTYPE_STRING:
  64. return (isKey ? "String" : "Object");
  65. case OBJECTIVECTYPE_DATA:
  66. return "Object";
  67. case OBJECTIVECTYPE_ENUM:
  68. return "Enum";
  69. case OBJECTIVECTYPE_MESSAGE:
  70. return "Object";
  71. }
  72. // Some compilers report reaching end of function even though all cases of
  73. // the enum are handed in the switch.
  74. GOOGLE_LOG(FATAL) << "Can't get here.";
  75. return NULL;
  76. }
  77. } // namespace
  78. MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor,
  79. const Options& options)
  80. : RepeatedFieldGenerator(descriptor, options) {
  81. const FieldDescriptor* key_descriptor =
  82. descriptor->message_type()->FindFieldByName("key");
  83. const FieldDescriptor* value_descriptor =
  84. descriptor->message_type()->FindFieldByName("value");
  85. value_field_generator_.reset(FieldGenerator::Make(value_descriptor, options));
  86. // Pull over some variables_ from the value.
  87. variables_["field_type"] = value_field_generator_->variable("field_type");
  88. variables_["default"] = value_field_generator_->variable("default");
  89. variables_["default_name"] = value_field_generator_->variable("default_name");
  90. // Build custom field flags.
  91. std::vector<string> field_flags;
  92. field_flags.push_back("GPBFieldMapKey" + GetCapitalizedType(key_descriptor));
  93. // Pull over the current text format custom name values that was calculated.
  94. if (variables_["fieldflags"].find("GPBFieldTextFormatNameCustom") !=
  95. string::npos) {
  96. field_flags.push_back("GPBFieldTextFormatNameCustom");
  97. }
  98. // Pull over some info from the value's flags.
  99. const string& value_field_flags =
  100. value_field_generator_->variable("fieldflags");
  101. if (value_field_flags.find("GPBFieldHasDefaultValue") != string::npos) {
  102. field_flags.push_back("GPBFieldHasDefaultValue");
  103. }
  104. if (value_field_flags.find("GPBFieldHasEnumDescriptor") != string::npos) {
  105. field_flags.push_back("GPBFieldHasEnumDescriptor");
  106. }
  107. variables_["fieldflags"] = BuildFlagsString(FLAGTYPE_FIELD, field_flags);
  108. ObjectiveCType value_objc_type = GetObjectiveCType(value_descriptor);
  109. const bool value_is_object_type =
  110. ((value_objc_type == OBJECTIVECTYPE_STRING) ||
  111. (value_objc_type == OBJECTIVECTYPE_DATA) ||
  112. (value_objc_type == OBJECTIVECTYPE_MESSAGE));
  113. if ((GetObjectiveCType(key_descriptor) == OBJECTIVECTYPE_STRING) &&
  114. value_is_object_type) {
  115. variables_["array_storage_type"] = "NSMutableDictionary";
  116. variables_["array_property_type"] =
  117. "NSMutableDictionary<NSString*, " +
  118. value_field_generator_->variable("storage_type") + "*>";
  119. } else {
  120. string class_name("GPB");
  121. class_name += MapEntryTypeName(key_descriptor, true);
  122. class_name += MapEntryTypeName(value_descriptor, false);
  123. class_name += "Dictionary";
  124. variables_["array_storage_type"] = class_name;
  125. if (value_is_object_type) {
  126. variables_["array_property_type"] =
  127. class_name + "<" +
  128. value_field_generator_->variable("storage_type") + "*>";
  129. }
  130. }
  131. variables_["dataTypeSpecific_name"] =
  132. value_field_generator_->variable("dataTypeSpecific_name");
  133. variables_["dataTypeSpecific_value"] =
  134. value_field_generator_->variable("dataTypeSpecific_value");
  135. }
  136. MapFieldGenerator::~MapFieldGenerator() {}
  137. void MapFieldGenerator::FinishInitialization(void) {
  138. RepeatedFieldGenerator::FinishInitialization();
  139. // Use the array_comment support in RepeatedFieldGenerator to output what the
  140. // values in the map are.
  141. const FieldDescriptor* value_descriptor =
  142. descriptor_->message_type()->FindFieldByName("value");
  143. if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_ENUM) {
  144. variables_["array_comment"] =
  145. "// |" + variables_["name"] + "| values are |" + value_field_generator_->variable("storage_type") + "|\n";
  146. }
  147. }
  148. void MapFieldGenerator::DetermineForwardDeclarations(
  149. std::set<string>* fwd_decls) const {
  150. RepeatedFieldGenerator::DetermineForwardDeclarations(fwd_decls);
  151. const FieldDescriptor* value_descriptor =
  152. descriptor_->message_type()->FindFieldByName("value");
  153. if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_MESSAGE) {
  154. const string& value_storage_type =
  155. value_field_generator_->variable("storage_type");
  156. fwd_decls->insert("@class " + value_storage_type);
  157. }
  158. }
  159. } // namespace objectivec
  160. } // namespace compiler
  161. } // namespace protobuf
  162. } // namespace google