utility.h 8.6 KB

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