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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
// Protocol Buffers - Google's data interchange format
// Copyright 2015 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "google/protobuf/compiler/objectivec/map_field.h"
#include <string>
#include <vector>
#include "y_absl/container/btree_set.h"
#include "y_absl/log/absl_log.h"
#include "y_absl/strings/match.h"
#include "google/protobuf/compiler/objectivec/helpers.h"
#include "google/protobuf/compiler/objectivec/names.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace objectivec {
// MapFieldGenerator uses RepeatedFieldGenerator as the parent because it
// provides a bunch of things (no has* methods, comments for contained type,
// etc.).
namespace {
const char* MapEntryTypeName(const FieldDescriptor* descriptor, bool isKey) {
ObjectiveCType type = GetObjectiveCType(descriptor);
switch (type) {
case OBJECTIVECTYPE_INT32:
return "Int32";
case OBJECTIVECTYPE_UINT32:
return "UInt32";
case OBJECTIVECTYPE_INT64:
return "Int64";
case OBJECTIVECTYPE_UINT64:
return "UInt64";
case OBJECTIVECTYPE_FLOAT:
return "Float";
case OBJECTIVECTYPE_DOUBLE:
return "Double";
case OBJECTIVECTYPE_BOOLEAN:
return "Bool";
case OBJECTIVECTYPE_STRING:
return (isKey ? "String" : "Object");
case OBJECTIVECTYPE_DATA:
return "Object";
case OBJECTIVECTYPE_ENUM:
return "Enum";
case OBJECTIVECTYPE_MESSAGE:
return "Object";
}
// Some compilers report reaching end of function even though all cases of
// the enum are handed in the switch.
Y_ABSL_LOG(FATAL) << "Can't get here.";
return nullptr;
}
} // namespace
MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor)
: RepeatedFieldGenerator(descriptor) {
const FieldDescriptor* key_descriptor = descriptor->message_type()->map_key();
const FieldDescriptor* value_descriptor =
descriptor->message_type()->map_value();
value_field_generator_.reset(FieldGenerator::Make(value_descriptor));
// Pull over some variables_ from the value.
variables_["field_type"] = value_field_generator_->variable("field_type");
variables_["default"] = value_field_generator_->variable("default");
variables_["default_name"] = value_field_generator_->variable("default_name");
// Build custom field flags.
std::vector<TProtoStringType> field_flags;
field_flags.push_back(
y_absl::StrCat("GPBFieldMapKey", GetCapitalizedType(key_descriptor)));
// Pull over the current text format custom name values that was calculated.
if (y_absl::StrContains(variables_["fieldflags"],
"GPBFieldTextFormatNameCustom")) {
field_flags.push_back("GPBFieldTextFormatNameCustom");
}
// Pull over some info from the value's flags.
const TProtoStringType& value_field_flags =
value_field_generator_->variable("fieldflags");
if (y_absl::StrContains(value_field_flags, "GPBFieldHasDefaultValue")) {
field_flags.push_back("GPBFieldHasDefaultValue");
}
if (y_absl::StrContains(value_field_flags, "GPBFieldHasEnumDescriptor")) {
field_flags.push_back("GPBFieldHasEnumDescriptor");
if (y_absl::StrContains(value_field_flags, "GPBFieldClosedEnum")) {
field_flags.push_back("GPBFieldClosedEnum");
}
}
variables_["fieldflags"] = BuildFlagsString(FLAGTYPE_FIELD, field_flags);
ObjectiveCType value_objc_type = GetObjectiveCType(value_descriptor);
const bool value_is_object_type =
((value_objc_type == OBJECTIVECTYPE_STRING) ||
(value_objc_type == OBJECTIVECTYPE_DATA) ||
(value_objc_type == OBJECTIVECTYPE_MESSAGE));
if ((GetObjectiveCType(key_descriptor) == OBJECTIVECTYPE_STRING) &&
value_is_object_type) {
variables_["array_storage_type"] = "NSMutableDictionary";
variables_["array_property_type"] =
y_absl::StrCat("NSMutableDictionary<NSString*, ",
value_field_generator_->variable("storage_type"), "*>");
} else {
TProtoStringType class_name =
y_absl::StrCat("GPB", MapEntryTypeName(key_descriptor, true),
MapEntryTypeName(value_descriptor, false), "Dictionary");
variables_["array_storage_type"] = class_name;
if (value_is_object_type) {
variables_["array_property_type"] =
y_absl::StrCat(class_name, "<",
value_field_generator_->variable("storage_type"), "*>");
}
}
variables_["dataTypeSpecific_name"] =
value_field_generator_->variable("dataTypeSpecific_name");
variables_["dataTypeSpecific_value"] =
value_field_generator_->variable("dataTypeSpecific_value");
}
void MapFieldGenerator::FinishInitialization() {
RepeatedFieldGenerator::FinishInitialization();
// Use the array_comment support in RepeatedFieldGenerator to output what the
// values in the map are.
const FieldDescriptor* value_descriptor =
descriptor_->message_type()->map_value();
if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_ENUM) {
TProtoStringType name = variables_["name"];
variables_["array_comment"] =
y_absl::StrCat("// |", name, "| values are |",
value_field_generator_->variable("storage_type"), "|\n");
}
}
void MapFieldGenerator::DetermineForwardDeclarations(
y_absl::btree_set<TProtoStringType>* fwd_decls,
bool include_external_types) const {
RepeatedFieldGenerator::DetermineForwardDeclarations(fwd_decls,
include_external_types);
const FieldDescriptor* value_descriptor =
descriptor_->message_type()->map_value();
// NOTE: Maps with values of enums don't have to worry about adding the
// forward declaration because `GPB*EnumDictionary` isn't generic to the
// specific enum (like say `NSDictionary<String, MyMessage>`) and thus doesn't
// reference the type in the header.
if (GetObjectiveCType(value_descriptor) != OBJECTIVECTYPE_MESSAGE) {
return;
}
const Descriptor* value_msg_descriptor = value_descriptor->message_type();
// Within a file there is no requirement on the order of the messages, so
// local references need a forward declaration. External files (not WKTs),
// need one when requested.
if ((include_external_types &&
!IsProtobufLibraryBundledProtoFile(value_msg_descriptor->file())) ||
descriptor_->file() == value_msg_descriptor->file()) {
const TProtoStringType& value_storage_type =
value_field_generator_->variable("storage_type");
fwd_decls->insert(y_absl::StrCat("@class ", value_storage_type, ";"));
}
}
void MapFieldGenerator::DetermineObjectiveCClassDefinitions(
y_absl::btree_set<TProtoStringType>* fwd_decls) const {
// Class name is already in "storage_type".
const FieldDescriptor* value_descriptor =
descriptor_->message_type()->map_value();
if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_MESSAGE) {
fwd_decls->insert(
ObjCClassDeclaration(value_field_generator_->variable("storage_type")));
}
}
} // namespace objectivec
} // namespace compiler
} // namespace protobuf
} // namespace google
|