1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 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 <google/protobuf/util/internal/type_info.h>
32
33 #include <map>
34 #include <set>
35
36 #include <google/protobuf/stubs/common.h>
37 #include <google/protobuf/type.pb.h>
38 #include <google/protobuf/util/internal/utility.h>
39 #include <google/protobuf/stubs/status.h>
40 #include <google/protobuf/stubs/strutil.h>
41 #include <google/protobuf/stubs/map_util.h>
42 #include <google/protobuf/stubs/status.h>
43 #include <google/protobuf/stubs/statusor.h>
44
45 namespace google {
46 namespace protobuf {
47 namespace util {
48 namespace converter {
49
50 namespace {
51 // A TypeInfo that looks up information provided by a TypeResolver.
52 class TypeInfoForTypeResolver : public TypeInfo {
53 public:
TypeInfoForTypeResolver(TypeResolver * type_resolver)54 explicit TypeInfoForTypeResolver(TypeResolver* type_resolver)
55 : type_resolver_(type_resolver) {}
56
~TypeInfoForTypeResolver()57 virtual ~TypeInfoForTypeResolver() {
58 DeleteCachedTypes(&cached_types_);
59 DeleteCachedTypes(&cached_enums_);
60 }
61
ResolveTypeUrl(StringPiece type_url) const62 util::StatusOr<const google::protobuf::Type*> ResolveTypeUrl(
63 StringPiece type_url) const override {
64 std::map<StringPiece, StatusOrType>::iterator it =
65 cached_types_.find(type_url);
66 if (it != cached_types_.end()) {
67 return it->second;
68 }
69 // Stores the string value so it can be referenced using StringPiece in the
70 // cached_types_ map.
71 const std::string& string_type_url =
72 *string_storage_.insert(std::string(type_url)).first;
73 std::unique_ptr<google::protobuf::Type> type(new google::protobuf::Type());
74 util::Status status =
75 type_resolver_->ResolveMessageType(string_type_url, type.get());
76 StatusOrType result =
77 status.ok() ? StatusOrType(type.release()) : StatusOrType(status);
78 cached_types_[string_type_url] = result;
79 return result;
80 }
81
GetTypeByTypeUrl(StringPiece type_url) const82 const google::protobuf::Type* GetTypeByTypeUrl(
83 StringPiece type_url) const override {
84 StatusOrType result = ResolveTypeUrl(type_url);
85 return result.ok() ? result.value() : NULL;
86 }
87
GetEnumByTypeUrl(StringPiece type_url) const88 const google::protobuf::Enum* GetEnumByTypeUrl(
89 StringPiece type_url) const override {
90 std::map<StringPiece, StatusOrEnum>::iterator it =
91 cached_enums_.find(type_url);
92 if (it != cached_enums_.end()) {
93 return it->second.ok() ? it->second.value() : NULL;
94 }
95 // Stores the string value so it can be referenced using StringPiece in the
96 // cached_enums_ map.
97 const std::string& string_type_url =
98 *string_storage_.insert(std::string(type_url)).first;
99 std::unique_ptr<google::protobuf::Enum> enum_type(
100 new google::protobuf::Enum());
101 util::Status status =
102 type_resolver_->ResolveEnumType(string_type_url, enum_type.get());
103 StatusOrEnum result =
104 status.ok() ? StatusOrEnum(enum_type.release()) : StatusOrEnum(status);
105 cached_enums_[string_type_url] = result;
106 return result.ok() ? result.value() : NULL;
107 }
108
FindField(const google::protobuf::Type * type,StringPiece camel_case_name) const109 const google::protobuf::Field* FindField(
110 const google::protobuf::Type* type,
111 StringPiece camel_case_name) const override {
112 std::map<const google::protobuf::Type*, CamelCaseNameTable>::const_iterator
113 it = indexed_types_.find(type);
114 const CamelCaseNameTable& camel_case_name_table =
115 (it == indexed_types_.end())
116 ? PopulateNameLookupTable(type, &indexed_types_[type])
117 : it->second;
118 StringPiece name = FindWithDefault(
119 camel_case_name_table, camel_case_name, StringPiece());
120 if (name.empty()) {
121 // Didn't find a mapping. Use whatever provided.
122 name = camel_case_name;
123 }
124 return FindFieldInTypeOrNull(type, name);
125 }
126
127 private:
128 typedef util::StatusOr<const google::protobuf::Type*> StatusOrType;
129 typedef util::StatusOr<const google::protobuf::Enum*> StatusOrEnum;
130 typedef std::map<StringPiece, StringPiece> CamelCaseNameTable;
131
132 template <typename T>
DeleteCachedTypes(std::map<StringPiece,T> * cached_types)133 static void DeleteCachedTypes(std::map<StringPiece, T>* cached_types) {
134 for (typename std::map<StringPiece, T>::iterator it =
135 cached_types->begin();
136 it != cached_types->end(); ++it) {
137 if (it->second.ok()) {
138 delete it->second.value();
139 }
140 }
141 }
142
PopulateNameLookupTable(const google::protobuf::Type * type,CamelCaseNameTable * camel_case_name_table) const143 const CamelCaseNameTable& PopulateNameLookupTable(
144 const google::protobuf::Type* type,
145 CamelCaseNameTable* camel_case_name_table) const {
146 for (int i = 0; i < type->fields_size(); ++i) {
147 const google::protobuf::Field& field = type->fields(i);
148 StringPiece name = field.name();
149 StringPiece camel_case_name = field.json_name();
150 const StringPiece* existing = InsertOrReturnExisting(
151 camel_case_name_table, camel_case_name, name);
152 if (existing && *existing != name) {
153 GOOGLE_LOG(WARNING) << "Field '" << name << "' and '" << *existing
154 << "' map to the same camel case name '" << camel_case_name
155 << "'.";
156 }
157 }
158 return *camel_case_name_table;
159 }
160
161 TypeResolver* type_resolver_;
162
163 // Stores string values that will be referenced by StringPieces in
164 // cached_types_, cached_enums_.
165 mutable std::set<std::string> string_storage_;
166
167 mutable std::map<StringPiece, StatusOrType> cached_types_;
168 mutable std::map<StringPiece, StatusOrEnum> cached_enums_;
169
170 mutable std::map<const google::protobuf::Type*, CamelCaseNameTable>
171 indexed_types_;
172 };
173 } // namespace
174
NewTypeInfo(TypeResolver * type_resolver)175 TypeInfo* TypeInfo::NewTypeInfo(TypeResolver* type_resolver) {
176 return new TypeInfoForTypeResolver(type_resolver);
177 }
178
179 } // namespace converter
180 } // namespace util
181 } // namespace protobuf
182 } // namespace google
183