blob: d8e06a97d212d5919e9c7014e5afac8e68894fff [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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 Silverman9c614bc2016-02-15 20:20:02 -050035#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
48namespace google {
49namespace protobuf {
50class Method;
51class Any;
52class Bool;
53class Option;
54class Field;
55class Type;
56class Enum;
57class EnumValue;
58} // namespace protobuf
59
60
61namespace protobuf {
62namespace util {
63namespace converter {
Austin Schuh40c16522018-10-28 20:27:54 -070064
65// Size of "type.googleapis.com"
66static const int64 kTypeUrlSize = 19;
67
Brian Silverman9c614bc2016-02-15 20:20:02 -050068// 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.
71LIBPROTOBUF_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.
77LIBPROTOBUF_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.
83LIBPROTOBUF_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.
89LIBPROTOBUF_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.
97LIBPROTOBUF_EXPORT bool GetBoolFromAny(const google::protobuf::Any& any);
98
99// Returns int64 value contained in Any type.
100LIBPROTOBUF_EXPORT int64 GetInt64FromAny(const google::protobuf::Any& any);
101
102// Returns double value contained in Any type.
103LIBPROTOBUF_EXPORT double GetDoubleFromAny(const google::protobuf::Any& any);
104
105// Returns string value contained in Any type.
106LIBPROTOBUF_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'.
110LIBPROTOBUF_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".
118LIBPROTOBUF_EXPORT const string GetFullTypeWithUrl(StringPiece simple_type);
119
120// Finds and returns option identified by name and option_name within the
Austin Schuh40c16522018-10-28 20:27:54 -0700121// provided map. Returns nullptr if none found.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500122const 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 Schuh40c16522018-10-28 20:27:54 -0700127// object. Returns nullptr if none found.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500128const 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.
133const google::protobuf::Field* FindJsonFieldInTypeOrNull(
134 const google::protobuf::Type* type, StringPiece json_name);
135
Austin Schuh40c16522018-10-28 20:27:54 -0700136// Similar to FindFieldInTypeOrNull, but this looks up fields by number.
137const google::protobuf::Field* FindFieldInTypeByNumberOrNull(
138 const google::protobuf::Type* type, int32 number);
139
Brian Silverman9c614bc2016-02-15 20:20:02 -0500140// Finds and returns the EnumValue identified by enum_name in the passed tech
Austin Schuh40c16522018-10-28 20:27:54 -0700141// Enum object. Returns nullptr if none found.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500142const 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 Schuh40c16522018-10-28 20:27:54 -0700146// Enum object. Returns nullptr if none found.
Brian Silverman9c614bc2016-02-15 20:20:02 -0500147const google::protobuf::EnumValue* FindEnumValueByNumberOrNull(
148 const google::protobuf::Enum* enum_type, int32 value);
149
Austin Schuh40c16522018-10-28 20:27:54 -0700150// 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.
154const google::protobuf::EnumValue* FindEnumValueByNameWithoutUnderscoreOrNull(
155 const google::protobuf::Enum* enum_type, StringPiece enum_name);
156
Brian Silverman9c614bc2016-02-15 20:20:02 -0500157// Converts input to camel-case and returns it.
158LIBPROTOBUF_EXPORT string ToCamelCase(const StringPiece input);
159
Austin Schuh40c16522018-10-28 20:27:54 -0700160// Converts enum name string to camel-case and returns it.
161string EnumValueNameToLowerCamelCase(const StringPiece input);
162
Brian Silverman9c614bc2016-02-15 20:20:02 -0500163// Converts input to snake_case and returns it.
164LIBPROTOBUF_EXPORT string ToSnakeCase(StringPiece input);
165
166// Returns true if type_name represents a well-known type.
167LIBPROTOBUF_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.
171LIBPROTOBUF_EXPORT bool IsValidBoolString(const string& bool_string);
172
173// Returns true if "field" is a protobuf map field based on its type.
174LIBPROTOBUF_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.
178bool IsMessageSetWireFormat(const google::protobuf::Type& type);
179
180// Infinity/NaN-aware conversion to string.
181LIBPROTOBUF_EXPORT string DoubleAsString(double value);
182LIBPROTOBUF_EXPORT string FloatAsString(float value);
183
184// Convert from int32, int64, uint32, uint64, double or float to string.
185template <typename T>
186string ValueAsString(T value) {
187 return SimpleItoa(value);
188}
189
190template <>
191inline string ValueAsString(float value) {
192 return FloatAsString(value);
193}
194
195template <>
196inline 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).
202LIBPROTOBUF_EXPORT bool SafeStrToFloat(StringPiece str, float* value);
Austin Schuh40c16522018-10-28 20:27:54 -0700203
204// Returns whether a StringPiece begins with the provided prefix.
205bool StringStartsWith(StringPiece text, StringPiece prefix);
206
207// Returns whether a StringPiece ends with the provided suffix.
208bool StringEndsWith(StringPiece text, StringPiece suffix);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500209} // namespace converter
210} // namespace util
211} // namespace protobuf
212
213} // namespace google
214#endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_UTILITY_H__