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