utility.h 8.3 KB

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