• 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 #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_UTILITY_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_UTILITY_H__
33 
34 #include <memory>
35 #ifndef _SHARED_PTR_H
36 #include <google/protobuf/stubs/shared_ptr.h>
37 #endif
38 #include <string>
39 #include <utility>
40 
41 #include <google/protobuf/stubs/common.h>
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/type.pb.h>
44 #include <google/protobuf/repeated_field.h>
45 #include <google/protobuf/stubs/stringpiece.h>
46 #include <google/protobuf/stubs/strutil.h>
47 #include <google/protobuf/stubs/status.h>
48 #include <google/protobuf/stubs/statusor.h>
49 
50 
51 namespace google {
52 namespace protobuf {
53 class Method;
54 class Any;
55 class Bool;
56 class Option;
57 class Field;
58 class Type;
59 class Enum;
60 class EnumValue;
61 }  // namespace protobuf
62 
63 
64 namespace protobuf {
65 namespace util {
66 namespace converter {
67 
68 // Size of "type.googleapis.com"
69 static const int64 kTypeUrlSize = 19;
70 
71 // Finds the tech option identified by option_name. Parses the boolean value and
72 // returns it.
73 // When the option with the given name is not found, default_value is returned.
74 LIBPROTOBUF_EXPORT bool GetBoolOptionOrDefault(
75     const google::protobuf::RepeatedPtrField<google::protobuf::Option>& options,
76     const string& option_name, bool default_value);
77 
78 // Returns int64 option value. If the option isn't found, returns the
79 // default_value.
80 LIBPROTOBUF_EXPORT int64 GetInt64OptionOrDefault(
81     const google::protobuf::RepeatedPtrField<google::protobuf::Option>& options,
82     const string& option_name, int64 default_value);
83 
84 // Returns double option value. If the option isn't found, returns the
85 // default_value.
86 LIBPROTOBUF_EXPORT double GetDoubleOptionOrDefault(
87     const google::protobuf::RepeatedPtrField<google::protobuf::Option>& options,
88     const string& option_name, double default_value);
89 
90 // Returns string option value. If the option isn't found, returns the
91 // default_value.
92 LIBPROTOBUF_EXPORT string GetStringOptionOrDefault(
93     const google::protobuf::RepeatedPtrField<google::protobuf::Option>& options,
94     const string& option_name, const string& default_value);
95 
96 // Returns a boolean value contained in Any type.
97 // TODO(skarvaje): Make these utilities dealing with Any types more generic,
98 // add more error checking and move to a more public/sharable location so others
99 // can use.
100 LIBPROTOBUF_EXPORT bool GetBoolFromAny(const google::protobuf::Any& any);
101 
102 // Returns int64 value contained in Any type.
103 LIBPROTOBUF_EXPORT int64 GetInt64FromAny(const google::protobuf::Any& any);
104 
105 // Returns double value contained in Any type.
106 LIBPROTOBUF_EXPORT double GetDoubleFromAny(const google::protobuf::Any& any);
107 
108 // Returns string value contained in Any type.
109 LIBPROTOBUF_EXPORT string GetStringFromAny(const google::protobuf::Any& any);
110 
111 // Returns the type string without the url prefix. e.g.: If the passed type is
112 // 'type.googleapis.com/tech.type.Bool', the returned value is 'tech.type.Bool'.
113 LIBPROTOBUF_EXPORT const StringPiece GetTypeWithoutUrl(StringPiece type_url);
114 
115 // Returns the simple_type with the base type url (kTypeServiceBaseUrl)
116 // prefixed.
117 //
118 // E.g:
119 // GetFullTypeWithUrl("google.protobuf.Timestamp") returns the string
120 // "type.googleapis.com/google.protobuf.Timestamp".
121 LIBPROTOBUF_EXPORT const string GetFullTypeWithUrl(StringPiece simple_type);
122 
123 // Finds and returns option identified by name and option_name within the
124 // provided map. Returns NULL if none found.
125 const google::protobuf::Option* FindOptionOrNull(
126     const google::protobuf::RepeatedPtrField<google::protobuf::Option>& options,
127     const string& option_name);
128 
129 // Finds and returns the field identified by field_name in the passed tech Type
130 // object. Returns NULL if none found.
131 const google::protobuf::Field* FindFieldInTypeOrNull(
132     const google::protobuf::Type* type, StringPiece field_name);
133 
134 // Similar to FindFieldInTypeOrNull, but this looks up fields with given
135 // json_name.
136 const google::protobuf::Field* FindJsonFieldInTypeOrNull(
137     const google::protobuf::Type* type, StringPiece json_name);
138 
139 // Finds and returns the EnumValue identified by enum_name in the passed tech
140 // Enum object. Returns NULL if none found.
141 const google::protobuf::EnumValue* FindEnumValueByNameOrNull(
142     const google::protobuf::Enum* enum_type, StringPiece enum_name);
143 
144 // Finds and returns the EnumValue identified by value in the passed tech
145 // Enum object. Returns NULL if none found.
146 const google::protobuf::EnumValue* FindEnumValueByNumberOrNull(
147     const google::protobuf::Enum* enum_type, int32 value);
148 
149 // Converts input to camel-case and returns it.
150 LIBPROTOBUF_EXPORT string ToCamelCase(const StringPiece input);
151 
152 // Converts input to snake_case and returns it.
153 LIBPROTOBUF_EXPORT string ToSnakeCase(StringPiece input);
154 
155 // Returns true if type_name represents a well-known type.
156 LIBPROTOBUF_EXPORT bool IsWellKnownType(const string& type_name);
157 
158 // Returns true if 'bool_string' represents a valid boolean value. Only "true",
159 // "false", "0" and "1" are allowed.
160 LIBPROTOBUF_EXPORT bool IsValidBoolString(const string& bool_string);
161 
162 // Returns true if "field" is a protobuf map field based on its type.
163 LIBPROTOBUF_EXPORT bool IsMap(const google::protobuf::Field& field,
164            const google::protobuf::Type& type);
165 
166 // Returns true if the given type has special MessageSet wire format.
167 bool IsMessageSetWireFormat(const google::protobuf::Type& type);
168 
169 // Infinity/NaN-aware conversion to string.
170 LIBPROTOBUF_EXPORT string DoubleAsString(double value);
171 LIBPROTOBUF_EXPORT string FloatAsString(float value);
172 
173 // Convert from int32, int64, uint32, uint64, double or float to string.
174 template <typename T>
ValueAsString(T value)175 string ValueAsString(T value) {
176   return SimpleItoa(value);
177 }
178 
179 template <>
ValueAsString(float value)180 inline string ValueAsString(float value) {
181   return FloatAsString(value);
182 }
183 
184 template <>
ValueAsString(double value)185 inline string ValueAsString(double value) {
186   return DoubleAsString(value);
187 }
188 
189 // Converts a string to float. Unlike safe_strtof, conversion will fail if the
190 // value fits into double but not float (e.g., DBL_MAX).
191 LIBPROTOBUF_EXPORT bool SafeStrToFloat(StringPiece str, float* value);
192 }  // namespace converter
193 }  // namespace util
194 }  // namespace protobuf
195 
196 }  // namespace google
197 #endif  // GOOGLE_PROTOBUF_UTIL_CONVERTER_UTILITY_H__
198