• 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_test_helper.h>
32 
33 #include <memory>
34 #include <vector>
35 
36 #include <google/protobuf/stubs/logging.h>
37 #include <google/protobuf/stubs/common.h>
38 #include <google/protobuf/descriptor.h>
39 #include <google/protobuf/util/internal/default_value_objectwriter.h>
40 #include <google/protobuf/util/internal/type_info.h>
41 #include <google/protobuf/util/internal/constants.h>
42 #include <google/protobuf/util/internal/protostream_objectsource.h>
43 #include <google/protobuf/util/internal/protostream_objectwriter.h>
44 #include <google/protobuf/util/type_resolver.h>
45 #include <google/protobuf/util/type_resolver_util.h>
46 
47 namespace google {
48 namespace protobuf {
49 namespace util {
50 namespace converter {
51 namespace testing {
52 
53 
ResetTypeInfo(const std::vector<const Descriptor * > & descriptors)54 void TypeInfoTestHelper::ResetTypeInfo(
55     const std::vector<const Descriptor*>& descriptors) {
56   switch (type_) {
57     case USE_TYPE_RESOLVER: {
58       const DescriptorPool* pool = descriptors[0]->file()->pool();
59       for (int i = 1; i < descriptors.size(); ++i) {
60         GOOGLE_CHECK(pool == descriptors[i]->file()->pool())
61             << "Descriptors from different pools are not supported.";
62       }
63       type_resolver_.reset(
64           NewTypeResolverForDescriptorPool(kTypeServiceBaseUrl, pool));
65       typeinfo_.reset(TypeInfo::NewTypeInfo(type_resolver_.get()));
66       return;
67     }
68   }
69   GOOGLE_LOG(FATAL) << "Can not reach here.";
70 }
71 
ResetTypeInfo(const Descriptor * descriptor)72 void TypeInfoTestHelper::ResetTypeInfo(const Descriptor* descriptor) {
73   std::vector<const Descriptor*> descriptors;
74   descriptors.push_back(descriptor);
75   ResetTypeInfo(descriptors);
76 }
77 
ResetTypeInfo(const Descriptor * descriptor1,const Descriptor * descriptor2)78 void TypeInfoTestHelper::ResetTypeInfo(const Descriptor* descriptor1,
79                                        const Descriptor* descriptor2) {
80   std::vector<const Descriptor*> descriptors;
81   descriptors.push_back(descriptor1);
82   descriptors.push_back(descriptor2);
83   ResetTypeInfo(descriptors);
84 }
85 
GetTypeInfo()86 TypeInfo* TypeInfoTestHelper::GetTypeInfo() { return typeinfo_.get(); }
87 
NewProtoSource(io::CodedInputStream * coded_input,const std::string & type_url)88 ProtoStreamObjectSource* TypeInfoTestHelper::NewProtoSource(
89     io::CodedInputStream* coded_input, const std::string& type_url) {
90   const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
91   switch (type_) {
92     case USE_TYPE_RESOLVER: {
93       return new ProtoStreamObjectSource(coded_input, type_resolver_.get(),
94                                          *type);
95     }
96   }
97   GOOGLE_LOG(FATAL) << "Can not reach here.";
98   return NULL;
99 }
100 
NewProtoWriter(const std::string & type_url,strings::ByteSink * output,ErrorListener * listener,const ProtoStreamObjectWriter::Options & options)101 ProtoStreamObjectWriter* TypeInfoTestHelper::NewProtoWriter(
102     const std::string& type_url, strings::ByteSink* output,
103     ErrorListener* listener, const ProtoStreamObjectWriter::Options& options) {
104   const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
105   switch (type_) {
106     case USE_TYPE_RESOLVER: {
107       return new ProtoStreamObjectWriter(type_resolver_.get(), *type, output,
108                                          listener, options);
109     }
110   }
111   GOOGLE_LOG(FATAL) << "Can not reach here.";
112   return NULL;
113 }
114 
NewDefaultValueWriter(const std::string & type_url,ObjectWriter * writer)115 DefaultValueObjectWriter* TypeInfoTestHelper::NewDefaultValueWriter(
116     const std::string& type_url, ObjectWriter* writer) {
117   const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
118   switch (type_) {
119     case USE_TYPE_RESOLVER: {
120       return new DefaultValueObjectWriter(type_resolver_.get(), *type, writer);
121     }
122   }
123   GOOGLE_LOG(FATAL) << "Can not reach here.";
124   return NULL;
125 }
126 
127 }  // namespace testing
128 }  // namespace converter
129 }  // namespace util
130 }  // namespace protobuf
131 }  // namespace google
132