• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 #include <map>
32 #include <string>
33 
34 #include <google/protobuf/compiler/objectivec/objectivec_map_field.h>
35 #include <google/protobuf/stubs/common.h>
36 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
37 #include <google/protobuf/io/printer.h>
38 #include <google/protobuf/stubs/strutil.h>
39 #include <google/protobuf/stubs/substitute.h>
40 
41 namespace google {
42 namespace protobuf {
43 namespace compiler {
44 namespace objectivec {
45 
46 // MapFieldGenerator uses RepeatedFieldGenerator as the parent because it
47 // provides a bunch of things (no has* methods, comments for contained type,
48 // etc.).
49 
50 namespace {
51 
MapEntryTypeName(const FieldDescriptor * descriptor,bool isKey)52 const char* MapEntryTypeName(const FieldDescriptor* descriptor, bool isKey) {
53   ObjectiveCType type = GetObjectiveCType(descriptor);
54   switch (type) {
55     case OBJECTIVECTYPE_INT32:
56       return "Int32";
57     case OBJECTIVECTYPE_UINT32:
58       return "UInt32";
59     case OBJECTIVECTYPE_INT64:
60       return "Int64";
61     case OBJECTIVECTYPE_UINT64:
62       return "UInt64";
63     case OBJECTIVECTYPE_FLOAT:
64       return "Float";
65     case OBJECTIVECTYPE_DOUBLE:
66       return "Double";
67     case OBJECTIVECTYPE_BOOLEAN:
68       return "Bool";
69     case OBJECTIVECTYPE_STRING:
70       return (isKey ? "String" : "Object");
71     case OBJECTIVECTYPE_DATA:
72       return "Object";
73     case OBJECTIVECTYPE_ENUM:
74       return "Enum";
75     case OBJECTIVECTYPE_MESSAGE:
76       return "Object";
77   }
78 
79   // Some compilers report reaching end of function even though all cases of
80   // the enum are handed in the switch.
81   GOOGLE_LOG(FATAL) << "Can't get here.";
82   return NULL;
83 }
84 
85 }  // namespace
86 
MapFieldGenerator(const FieldDescriptor * descriptor,const Options & options)87 MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor,
88                                      const Options& options)
89     : RepeatedFieldGenerator(descriptor, options) {
90   const FieldDescriptor* key_descriptor =
91       descriptor->message_type()->FindFieldByName("key");
92   const FieldDescriptor* value_descriptor =
93       descriptor->message_type()->FindFieldByName("value");
94   value_field_generator_.reset(FieldGenerator::Make(value_descriptor, options));
95 
96   // Pull over some variables_ from the value.
97   variables_["field_type"] = value_field_generator_->variable("field_type");
98   variables_["default"] = value_field_generator_->variable("default");
99   variables_["default_name"] = value_field_generator_->variable("default_name");
100 
101   // Build custom field flags.
102   std::vector<string> field_flags;
103   field_flags.push_back("GPBFieldMapKey" + GetCapitalizedType(key_descriptor));
104   // Pull over the current text format custom name values that was calculated.
105   if (variables_["fieldflags"].find("GPBFieldTextFormatNameCustom") !=
106       string::npos) {
107     field_flags.push_back("GPBFieldTextFormatNameCustom");
108   }
109   // Pull over some info from the value's flags.
110   const string& value_field_flags =
111       value_field_generator_->variable("fieldflags");
112   if (value_field_flags.find("GPBFieldHasDefaultValue") != string::npos) {
113     field_flags.push_back("GPBFieldHasDefaultValue");
114   }
115   if (value_field_flags.find("GPBFieldHasEnumDescriptor") != string::npos) {
116     field_flags.push_back("GPBFieldHasEnumDescriptor");
117   }
118   variables_["fieldflags"] = BuildFlagsString(field_flags);
119 
120   ObjectiveCType value_objc_type = GetObjectiveCType(value_descriptor);
121   const bool value_is_object_type =
122       ((value_objc_type == OBJECTIVECTYPE_STRING) ||
123        (value_objc_type == OBJECTIVECTYPE_DATA) ||
124        (value_objc_type == OBJECTIVECTYPE_MESSAGE));
125   if ((GetObjectiveCType(key_descriptor) == OBJECTIVECTYPE_STRING) &&
126       value_is_object_type) {
127     variables_["array_storage_type"] = "NSMutableDictionary";
128     variables_["array_property_type"] =
129         "NSMutableDictionary<NSString*, " +
130         value_field_generator_->variable("storage_type") + "*>";
131   } else {
132     string class_name("GPB");
133     class_name += MapEntryTypeName(key_descriptor, true);
134     class_name += MapEntryTypeName(value_descriptor, false);
135     class_name += "Dictionary";
136     variables_["array_storage_type"] = class_name;
137     if (value_is_object_type) {
138       variables_["array_property_type"] =
139           class_name + "<" +
140           value_field_generator_->variable("storage_type") + "*>";
141     }
142   }
143 
144   variables_["dataTypeSpecific_name"] =
145       value_field_generator_->variable("dataTypeSpecific_name");
146   variables_["dataTypeSpecific_value"] =
147       value_field_generator_->variable("dataTypeSpecific_value");
148 }
149 
~MapFieldGenerator()150 MapFieldGenerator::~MapFieldGenerator() {}
151 
FinishInitialization(void)152 void MapFieldGenerator::FinishInitialization(void) {
153   RepeatedFieldGenerator::FinishInitialization();
154   // Use the array_comment support in RepeatedFieldGenerator to output what the
155   // values in the map are.
156   const FieldDescriptor* value_descriptor =
157       descriptor_->message_type()->FindFieldByName("value");
158   if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_ENUM) {
159     variables_["array_comment"] =
160         "// |" + variables_["name"] + "| values are |" + value_field_generator_->variable("storage_type") + "|\n";
161   }
162 }
163 
DetermineForwardDeclarations(set<string> * fwd_decls) const164 void MapFieldGenerator::DetermineForwardDeclarations(
165     set<string>* fwd_decls) const {
166   RepeatedFieldGenerator::DetermineForwardDeclarations(fwd_decls);
167   const FieldDescriptor* value_descriptor =
168       descriptor_->message_type()->FindFieldByName("value");
169   if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_MESSAGE) {
170     const string& value_storage_type =
171         value_field_generator_->variable("storage_type");
172     fwd_decls->insert("@class " + value_storage_type);
173   }
174 }
175 
176 
177 }  // namespace objectivec
178 }  // namespace compiler
179 }  // namespace protobuf
180 }  // namespace google
181