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