blob: 49e18ed0ffec0c9a92daf7e64f3c2b4e2c165504 [file] [log] [blame]
Feng Xiaoe96ff302015-06-15 18:21:48 -07001// 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#ifndef _SHARED_PTR_H
35#include <google/protobuf/stubs/shared_ptr.h>
36#endif
37#include <vector>
38
Feng Xiaoeee38b02015-08-22 18:25:48 -070039#include <google/protobuf/stubs/logging.h>
Feng Xiaoe96ff302015-06-15 18:21:48 -070040#include <google/protobuf/stubs/common.h>
41#include <google/protobuf/descriptor.h>
42#include <google/protobuf/util/internal/default_value_objectwriter.h>
43#include <google/protobuf/util/internal/type_info.h>
44#include <google/protobuf/util/internal/constants.h>
45#include <google/protobuf/util/internal/protostream_objectsource.h>
46#include <google/protobuf/util/internal/protostream_objectwriter.h>
47#include <google/protobuf/util/type_resolver.h>
48#include <google/protobuf/util/type_resolver_util.h>
49
50namespace google {
51namespace protobuf {
52namespace util {
53namespace converter {
54namespace testing {
55
56
57void TypeInfoTestHelper::ResetTypeInfo(
58 const vector<const Descriptor*>& descriptors) {
59 switch (type_) {
60 case USE_TYPE_RESOLVER: {
61 const DescriptorPool* pool = descriptors[0]->file()->pool();
62 for (int i = 1; i < descriptors.size(); ++i) {
63 GOOGLE_CHECK(pool == descriptors[i]->file()->pool())
64 << "Descriptors from different pools are not supported.";
65 }
66 type_resolver_.reset(
67 NewTypeResolverForDescriptorPool(kTypeServiceBaseUrl, pool));
68 typeinfo_.reset(TypeInfo::NewTypeInfo(type_resolver_.get()));
69 return;
70 }
71 }
72 GOOGLE_LOG(FATAL) << "Can not reach here.";
73}
74
75void TypeInfoTestHelper::ResetTypeInfo(const Descriptor* descriptor) {
76 vector<const Descriptor*> descriptors;
77 descriptors.push_back(descriptor);
78 ResetTypeInfo(descriptors);
79}
80
81void TypeInfoTestHelper::ResetTypeInfo(const Descriptor* descriptor1,
82 const Descriptor* descriptor2) {
83 vector<const Descriptor*> descriptors;
84 descriptors.push_back(descriptor1);
85 descriptors.push_back(descriptor2);
86 ResetTypeInfo(descriptors);
87}
88
89TypeInfo* TypeInfoTestHelper::GetTypeInfo() { return typeinfo_.get(); }
90
91ProtoStreamObjectSource* TypeInfoTestHelper::NewProtoSource(
92 io::CodedInputStream* coded_input, const string& type_url) {
Feng Xiaoeee38b02015-08-22 18:25:48 -070093 const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
Feng Xiaoe96ff302015-06-15 18:21:48 -070094 switch (type_) {
95 case USE_TYPE_RESOLVER: {
96 return new ProtoStreamObjectSource(coded_input, type_resolver_.get(),
97 *type);
98 }
99 }
100 GOOGLE_LOG(FATAL) << "Can not reach here.";
Jisi Liu658e72d2015-06-24 14:30:33 -0700101 return NULL;
Feng Xiaoe96ff302015-06-15 18:21:48 -0700102}
103
104ProtoStreamObjectWriter* TypeInfoTestHelper::NewProtoWriter(
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700105 const string& type_url, strings::ByteSink* output, ErrorListener* listener,
106 const ProtoStreamObjectWriter::Options& options) {
Feng Xiaoeee38b02015-08-22 18:25:48 -0700107 const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
Feng Xiaoe96ff302015-06-15 18:21:48 -0700108 switch (type_) {
109 case USE_TYPE_RESOLVER: {
110 return new ProtoStreamObjectWriter(type_resolver_.get(), *type, output,
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700111 listener, options);
Feng Xiaoe96ff302015-06-15 18:21:48 -0700112 }
113 }
114 GOOGLE_LOG(FATAL) << "Can not reach here.";
Jisi Liu658e72d2015-06-24 14:30:33 -0700115 return NULL;
Feng Xiaoe96ff302015-06-15 18:21:48 -0700116}
117
118DefaultValueObjectWriter* TypeInfoTestHelper::NewDefaultValueWriter(
119 const string& type_url, ObjectWriter* writer) {
Feng Xiaoeee38b02015-08-22 18:25:48 -0700120 const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
Feng Xiaoe96ff302015-06-15 18:21:48 -0700121 switch (type_) {
122 case USE_TYPE_RESOLVER: {
123 return new DefaultValueObjectWriter(type_resolver_.get(), *type, writer);
124 }
125 }
126 GOOGLE_LOG(FATAL) << "Can not reach here.";
Jisi Liu658e72d2015-06-24 14:30:33 -0700127 return NULL;
Feng Xiaoe96ff302015-06-15 18:21:48 -0700128}
129
130} // namespace testing
131} // namespace converter
132} // namespace util
133} // namespace protobuf
134} // namespace google