protobuf/src/google/protobuf/util/internal/utility.h
Go to the documentation of this file.
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 <cstdint>
35 #include <memory>
36 #include <string>
37 #include <utility>
38 
39 #include <google/protobuf/stubs/common.h>
40 #include <google/protobuf/stubs/logging.h>
41 #include <google/protobuf/any.pb.h>
42 #include <google/protobuf/type.pb.h>
43 #include <google/protobuf/repeated_field.h>
44 #include <google/protobuf/stubs/strutil.h>
45 #include <google/protobuf/stubs/statusor.h>
46 #include <google/protobuf/stubs/status.h>
47 
48 // Must be included last.
49 #include <google/protobuf/port_def.inc>
50 
51 namespace google {
52 namespace protobuf {
53 namespace util {
54 namespace converter {
55 
56 // Size of "type.googleapis.com"
57 static const int64_t kTypeUrlSize = 19;
58 
59 // Finds the tech option identified by option_name. Parses the boolean value and
60 // returns it.
61 // When the option with the given name is not found, default_value is returned.
62 PROTOBUF_EXPORT bool GetBoolOptionOrDefault(
64  StringPiece option_name, bool default_value);
65 
66 // Returns int64 option value. If the option isn't found, returns the
67 // default_value.
68 PROTOBUF_EXPORT int64_t GetInt64OptionOrDefault(
70  StringPiece option_name, int64_t default_value);
71 
72 // Returns double option value. If the option isn't found, returns the
73 // default_value.
74 PROTOBUF_EXPORT double GetDoubleOptionOrDefault(
76  StringPiece option_name, double default_value);
77 
78 // Returns string option value. If the option isn't found, returns the
79 // default_value.
82  StringPiece option_name, StringPiece default_value);
83 
84 // Returns a boolean value contained in Any type.
85 // TODO(skarvaje): Make these utilities dealing with Any types more generic,
86 // add more error checking and move to a more public/shareable location so
87 // others can use.
88 PROTOBUF_EXPORT bool GetBoolFromAny(const google::protobuf::Any& any);
89 
90 // Returns int64 value contained in Any type.
91 PROTOBUF_EXPORT int64_t GetInt64FromAny(const google::protobuf::Any& any);
92 
93 // Returns double value contained in Any type.
94 PROTOBUF_EXPORT double GetDoubleFromAny(const google::protobuf::Any& any);
95 
96 // Returns string value contained in Any type.
97 PROTOBUF_EXPORT std::string GetStringFromAny(const google::protobuf::Any& any);
98 
99 // Returns the type string without the url prefix. e.g.: If the passed type is
100 // 'type.googleapis.com/tech.type.Bool', the returned value is 'tech.type.Bool'.
101 PROTOBUF_EXPORT const StringPiece GetTypeWithoutUrl(
103 
104 // Returns the simple_type with the base type url (kTypeServiceBaseUrl)
105 // prefixed.
106 //
107 // E.g:
108 // GetFullTypeWithUrl("google.protobuf.Timestamp") returns the string
109 // "type.googleapis.com/google.protobuf.Timestamp".
110 PROTOBUF_EXPORT const std::string GetFullTypeWithUrl(
111  StringPiece simple_type);
112 
113 // Finds and returns option identified by name and option_name within the
114 // provided map. Returns nullptr if none found.
117  StringPiece option_name);
118 
119 // Finds and returns the field identified by field_name in the passed tech Type
120 // object. Returns nullptr if none found.
122  const google::protobuf::Type* type, StringPiece field_name);
123 
124 // Similar to FindFieldInTypeOrNull, but this looks up fields with given
125 // json_name.
127  const google::protobuf::Type* type, StringPiece json_name);
128 
129 // Similar to FindFieldInTypeOrNull, but this looks up fields by number.
132 
133 // Finds and returns the EnumValue identified by enum_name in the passed tech
134 // Enum object. Returns nullptr if none found.
136  const google::protobuf::Enum* enum_type, StringPiece enum_name);
137 
138 // Finds and returns the EnumValue identified by value in the passed tech
139 // Enum object. Returns nullptr if none found.
142 
143 // Finds and returns the EnumValue identified by enum_name without underscore in
144 // the passed tech Enum object. Returns nullptr if none found.
145 // For Ex. if enum_name is ACTIONANDADVENTURE it can get accepted if
146 // EnumValue's name is action_and_adventure or ACTION_AND_ADVENTURE.
148  const google::protobuf::Enum* enum_type, StringPiece enum_name);
149 
150 // Converts input to camel-case and returns it.
151 PROTOBUF_EXPORT std::string ToCamelCase(const StringPiece input);
152 
153 // Converts enum name string to camel-case and returns it.
155 
156 // Converts input to snake_case and returns it.
157 PROTOBUF_EXPORT std::string ToSnakeCase(StringPiece input);
158 
159 // Returns true if type_name represents a well-known type.
160 PROTOBUF_EXPORT bool IsWellKnownType(const std::string& type_name);
161 
162 // Returns true if 'bool_string' represents a valid boolean value. Only "true",
163 // "false", "0" and "1" are allowed.
164 PROTOBUF_EXPORT bool IsValidBoolString(StringPiece bool_string);
165 
166 // Returns true if "field" is a protobuf map field based on its type.
167 PROTOBUF_EXPORT bool IsMap(const google::protobuf::Field& field,
169 
170 // Returns true if the given type has special MessageSet wire format.
172 
173 // Infinity/NaN-aware conversion to string.
174 PROTOBUF_EXPORT std::string DoubleAsString(double value);
175 PROTOBUF_EXPORT std::string FloatAsString(float value);
176 
177 // Convert from int32, int64, uint32, uint64, double or float to string.
178 template <typename T>
180  return StrCat(value);
181 }
182 
183 template <>
184 inline std::string ValueAsString(float value) {
185  return FloatAsString(value);
186 }
187 
188 template <>
189 inline std::string ValueAsString(double value) {
190  return DoubleAsString(value);
191 }
192 
193 // Converts a string to float. Unlike safe_strtof, conversion will fail if the
194 // value fits into double but not float (e.g., DBL_MAX).
195 PROTOBUF_EXPORT bool SafeStrToFloat(StringPiece str, float* value);
196 
197 } // namespace converter
198 } // namespace util
199 } // namespace protobuf
200 } // namespace google
201 
202 #include <google/protobuf/port_undef.inc>
203 
204 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_UTILITY_H__
xds_interop_client.str
str
Definition: xds_interop_client.py:487
Any
struct Any Any
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:633
Type
struct Type Type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:673
google::protobuf::RepeatedPtrField
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.h:62
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::util::converter::FindJsonFieldInTypeOrNull
const google::protobuf::Field * FindJsonFieldInTypeOrNull(const google::protobuf::Type *type, StringPiece json_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:165
google::protobuf::util::converter::ToCamelCase
std::string ToCamelCase(const StringPiece input)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:250
google::protobuf::util::converter::IsWellKnownType
bool IsWellKnownType(const std::string &type_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:349
google::protobuf::Enum
google::protobuf::util::converter::GetTypeWithoutUrl
const StringPiece GetTypeWithoutUrl(StringPiece type_url)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:124
options
double_dict options[]
Definition: capstone_test.c:55
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
make_cmakelists.converter
converter
Definition: make_cmakelists.py:317
google::protobuf::util::converter::SafeStrToFloat
bool SafeStrToFloat(StringPiece str, float *value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:391
google::protobuf::util::converter::ToSnakeCase
std::string ToSnakeCase(StringPiece input)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:293
google::protobuf::util::converter::GetInt64FromAny
int64 GetInt64FromAny(const google::protobuf::Any &any)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:106
T
#define T(upbtypeconst, upbtype, ctype, default_value)
google::protobuf::EnumValue
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1482
google::protobuf::util::converter::IsMessageSetWireFormat
bool IsMessageSetWireFormat(const google::protobuf::Type &type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:370
google::protobuf::util::converter::GetFullTypeWithUrl
const std::string GetFullTypeWithUrl(StringPiece simple_type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:136
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
google::protobuf::util::converter::GetInt64OptionOrDefault
int64 GetInt64OptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, int64 default_value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:63
google::protobuf::util::converter::kTypeUrlSize
static const int64 kTypeUrlSize
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.h:56
google::protobuf::StringPiece
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:180
google::protobuf::util::converter::GetDoubleFromAny
double GetDoubleFromAny(const google::protobuf::Any &any)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:112
google::protobuf::util::converter::IsMap
bool IsMap(const google::protobuf::Field &field, const google::protobuf::Type &type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:360
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
google::protobuf::util::converter::FindOptionOrNull
const google::protobuf::Option * FindOptionOrNull(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:140
google::protobuf::util::converter::FindEnumValueByNumberOrNull
const google::protobuf::EnumValue * FindEnumValueByNumberOrNull(const google::protobuf::Enum *enum_type, int32 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:204
google::protobuf::util::converter::GetDoubleOptionOrDefault
double GetDoubleOptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, double default_value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:73
google::protobuf::util::converter::EnumValueNameToLowerCamelCase
std::string EnumValueNameToLowerCamelCase(const StringPiece input)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:243
google::protobuf::util::converter::GetStringFromAny
std::string GetStringFromAny(const google::protobuf::Any &any)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:118
Field
struct Field Field
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:647
google::protobuf::util::converter::DoubleAsString
std::string DoubleAsString(double value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:378
google::protobuf::util::converter::FindEnumValueByNameWithoutUnderscoreOrNull
const google::protobuf::EnumValue * FindEnumValueByNameWithoutUnderscoreOrNull(const google::protobuf::Enum *enum_type, StringPiece enum_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:217
google::protobuf::util::converter::FindFieldInTypeByNumberOrNull
const google::protobuf::Field * FindFieldInTypeByNumberOrNull(const google::protobuf::Type *type, int32 number)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:178
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::util::converter::GetStringOptionOrDefault
std::string GetStringOptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, const std::string &default_value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:83
google::protobuf::util::converter::FindEnumValueByNameOrNull
const google::protobuf::EnumValue * FindEnumValueByNameOrNull(const google::protobuf::Enum *enum_type, StringPiece enum_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:191
google::protobuf::util::converter::GetBoolOptionOrDefault
bool GetBoolOptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, bool default_value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:53
type_url
string * type_url
Definition: bloaty/third_party/protobuf/conformance/conformance_cpp.cc:72
google::protobuf::util::converter::FindFieldInTypeOrNull
const google::protobuf::Field * FindFieldInTypeOrNull(const google::protobuf::Type *type, StringPiece field_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:152
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
google::protobuf::Option
google::protobuf::util::converter::GetBoolFromAny
bool GetBoolFromAny(const google::protobuf::Any &any)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:100
google::protobuf::util::converter::ValueAsString
std::string ValueAsString(T value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.h:178
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
google::protobuf::util::converter::FloatAsString
std::string FloatAsString(float value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:386
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
phone_pb2.enum_type
enum_type
Definition: phone_pb2.py:198
google::protobuf::util::converter::IsValidBoolString
bool IsValidBoolString(const std::string &bool_string)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:355
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
type_name
static const char * type_name(int type)
Definition: adig.c:889


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:49