field_mask_util.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 // Defines utilities for the FieldMask well known type.
32 
33 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
34 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
35 
36 #include <string>
37 
41 
42 #include <google/protobuf/port_def.inc>
43 
44 namespace google {
45 namespace protobuf {
46 namespace util {
47 
48 class PROTOBUF_EXPORT FieldMaskUtil {
50 
51  public:
52  // Converts FieldMask to/from string, formatted by separating each path
53  // with a comma (e.g., "foo_bar,baz.quz").
54  static std::string ToString(const FieldMask& mask);
55  static void FromString(StringPiece str, FieldMask* out);
56 
57  // Converts FieldMask to/from string, formatted according to proto3 JSON
58  // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not
59  // style conforming (i.e., not snake_case when converted to string, or not
60  // camelCase when converted from string), the conversion will fail.
61  static bool ToJsonString(const FieldMask& mask, std::string* out);
62  static bool FromJsonString(StringPiece str, FieldMask* out);
63 
64  // Get the descriptors of the fields which the given path from the message
65  // descriptor traverses, if field_descriptors is not null.
66  // Return false if the path is not valid, and the content of field_descriptors
67  // is unspecified.
68  static bool GetFieldDescriptors(
70  std::vector<const FieldDescriptor*>* field_descriptors);
71 
72  // Checks whether the given path is valid for type T.
73  template <typename T>
74  static bool IsValidPath(StringPiece path) {
75  return GetFieldDescriptors(T::descriptor(), path, nullptr);
76  }
77 
78  // Checks whether the given FieldMask is valid for type T.
79  template <typename T>
80  static bool IsValidFieldMask(const FieldMask& mask) {
81  for (int i = 0; i < mask.paths_size(); ++i) {
82  if (!GetFieldDescriptors(T::descriptor(), mask.paths(i), nullptr))
83  return false;
84  }
85  return true;
86  }
87 
88  // Adds a path to FieldMask after checking whether the given path is valid.
89  // This method check-fails if the path is not a valid path for type T.
90  template <typename T>
92  GOOGLE_CHECK(IsValidPath<T>(path));
93  mask->add_paths(path);
94  }
95 
96  // Creates a FieldMask with all fields of type T. This FieldMask only
97  // contains fields of T but not any sub-message fields.
98  template <typename T>
100  FieldMask out;
101  GetFieldMaskForAllFields(T::descriptor(), &out);
102  return out;
103  }
104  template <typename T>
105  PROTOBUF_DEPRECATED_MSG("Use *out = GetFieldMaskForAllFields() instead")
106  static void GetFieldMaskForAllFields(FieldMask* out) {
107  GetFieldMaskForAllFields(T::descriptor(), out);
108  }
109  // This flavor takes the protobuf type descriptor as an argument.
110  // Useful when the type is not known at compile time.
111  static void GetFieldMaskForAllFields(const Descriptor* descriptor,
112  FieldMask* out);
113 
114  // Converts a FieldMask to the canonical form. It will:
115  // 1. Remove paths that are covered by another path. For example,
116  // "foo.bar" is covered by "foo" and will be removed if "foo"
117  // is also in the FieldMask.
118  // 2. Sort all paths in alphabetical order.
119  static void ToCanonicalForm(const FieldMask& mask, FieldMask* out);
120 
121  // Creates an union of two FieldMasks.
122  static void Union(const FieldMask& mask1, const FieldMask& mask2,
123  FieldMask* out);
124 
125  // Creates an intersection of two FieldMasks.
126  static void Intersect(const FieldMask& mask1, const FieldMask& mask2,
127  FieldMask* out);
128 
129  // Subtracts mask2 from mask1 base of type T.
130  template <typename T>
131  static void Subtract(const FieldMask& mask1, const FieldMask& mask2,
132  FieldMask* out) {
133  Subtract(T::descriptor(), mask1, mask2, out);
134  }
135  // This flavor takes the protobuf type descriptor as an argument.
136  // Useful when the type is not known at compile time.
137  static void Subtract(const Descriptor* descriptor, const FieldMask& mask1,
138  const FieldMask& mask2, FieldMask* out);
139 
140  // Returns true if path is covered by the given FieldMask. Note that path
141  // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc.
142  // Also note that parent paths are not covered by explicit child path, i.e.
143  // "foo.bar" does NOT cover "foo", even if "bar" is the only child.
144  static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask);
145 
146  class MergeOptions;
147  // Merges fields specified in a FieldMask into another message.
148  static void MergeMessageTo(const Message& source, const FieldMask& mask,
149  const MergeOptions& options, Message* destination);
150 
151  class TrimOptions;
152  // Removes from 'message' any field that is not represented in the given
153  // FieldMask. If the FieldMask is empty, does nothing.
154  // Returns true if the message is modified.
155  static bool TrimMessage(const FieldMask& mask, Message* message);
156 
157  // Removes from 'message' any field that is not represented in the given
158  // FieldMask with customized TrimOptions.
159  // If the FieldMask is empty, does nothing.
160  // Returns true if the message is modified.
161  static bool TrimMessage(const FieldMask& mask, Message* message,
162  const TrimOptions& options);
163 
164  private:
166  // Converts a field name from snake_case to camelCase:
167  // 1. Every character after "_" will be converted to uppercase.
168  // 2. All "_"s are removed.
169  // The conversion will fail if:
170  // 1. The field name contains uppercase letters.
171  // 2. Any character after a "_" is not a lowercase letter.
172  // If the conversion succeeds, it's guaranteed that the resulted
173  // camelCase name will yield the original snake_case name when
174  // converted using CamelCaseToSnakeCase().
175  //
176  // Note that the input can contain characters not allowed in C identifiers.
177  // For example, "foo_bar,baz_quz" will be converted to "fooBar,bazQuz"
178  // successfully.
179  static bool SnakeCaseToCamelCase(StringPiece input,
181  // Converts a field name from camelCase to snake_case:
182  // 1. Every uppercase letter is converted to lowercase with a additional
183  // preceding "-".
184  // The conversion will fail if:
185  // 1. The field name contains "_"s.
186  // If the conversion succeeds, it's guaranteed that the resulted
187  // snake_case name will yield the original camelCase name when
188  // converted using SnakeCaseToCamelCase().
189  //
190  // Note that the input can contain characters not allowed in C identifiers.
191  // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz"
192  // successfully.
193  static bool CamelCaseToSnakeCase(StringPiece input,
195 };
196 
197 class PROTOBUF_EXPORT FieldMaskUtil::MergeOptions {
198  public:
200  : replace_message_fields_(false), replace_repeated_fields_(false) {}
201  // When merging message fields, the default behavior is to merge the
202  // content of two message fields together. If you instead want to use
203  // the field from the source message to replace the corresponding field
204  // in the destination message, set this flag to true. When this flag is set,
205  // specified submessage fields that are missing in source will be cleared in
206  // destination.
208  replace_message_fields_ = value;
209  }
210  bool replace_message_fields() const { return replace_message_fields_; }
211  // The default merging behavior will append entries from the source
212  // repeated field to the destination repeated field. If you only want
213  // to keep the entries from the source repeated field, set this flag
214  // to true.
216  replace_repeated_fields_ = value;
217  }
218  bool replace_repeated_fields() const { return replace_repeated_fields_; }
219 
220  private:
223 };
224 
225 class PROTOBUF_EXPORT FieldMaskUtil::TrimOptions {
226  public:
227  TrimOptions() : keep_required_fields_(false) {}
228  // When trimming message fields, the default behavior is to trim required
229  // fields of the present message if they are not specified in the field mask.
230  // If you instead want to keep required fields of the present message even
231  // they are not specified in the field mask, set this flag to true.
232  void set_keep_required_fields(bool value) { keep_required_fields_ = value; }
233  bool keep_required_fields() const { return keep_required_fields_; }
234 
235  private:
237 };
238 
239 } // namespace util
240 } // namespace protobuf
241 } // namespace google
242 
243 #include <google/protobuf/port_undef.inc>
244 
245 #endif // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
google::protobuf::util::FieldMaskUtil::TrimOptions
TrimOptions()
Definition: field_mask_util.h:227
google::protobuf::util::FieldMaskUtil::set_replace_repeated_fields
void set_replace_repeated_fields(bool value)
Definition: field_mask_util.h:215
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
field_mask.pb.h
google::protobuf::util::FieldMaskUtil::replace_repeated_fields_
bool replace_repeated_fields_
Definition: field_mask_util.h:222
FieldMask
Definition: field_mask.pb.h:69
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
input
std::string input
Definition: tokenizer_unittest.cc:197
google::protobuf::util::FieldMaskUtil::replace_message_fields
bool replace_message_fields() const
Definition: field_mask_util.h:210
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
FieldMask
struct FieldMask FieldMask
Definition: php/ext/google/protobuf/protobuf.h:640
google::protobuf::util::FieldMaskUtil::replace_repeated_fields
bool replace_repeated_fields() const
Definition: field_mask_util.h:218
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
stringpiece.h
google::protobuf::util::SnakeCaseCamelCaseTest
Definition: field_mask_util_test.cc:46
google::protobuf::util::FieldMaskUtil::replace_message_fields_
bool replace_message_fields_
Definition: field_mask_util.h:221
path
GLsizei const GLchar ** path
Definition: glcorearb.h:3658
mask
GLint GLuint mask
Definition: glcorearb.h:2789
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf::util::FieldMaskUtil::IsValidPath
static bool IsValidPath(StringPiece path)
Definition: field_mask_util.h:74
update_failure_list.str
str
Definition: update_failure_list.py:41
google::protobuf::util::FieldMaskUtil::IsValidFieldMask
static bool IsValidFieldMask(const FieldMask &mask)
Definition: field_mask_util.h:80
google::protobuf::util::FieldMaskUtil
Definition: field_mask_util.h:48
source
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:3072
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
google::protobuf::util::FieldMaskUtil::FieldMask
google::protobuf::FieldMask FieldMask
Definition: field_mask_util.h:49
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::FieldMask
google::protobuf::util::FieldMaskUtil::set_replace_message_fields
void set_replace_message_fields(bool value)
Definition: field_mask_util.h:207
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
google::protobuf.internal.python_message.FromString
FromString
Definition: python_message.py:796
google::protobuf::util::FieldMaskUtil::set_keep_required_fields
void set_keep_required_fields(bool value)
Definition: field_mask_util.h:232
google::protobuf::util::FieldMaskUtil::keep_required_fields
bool keep_required_fields() const
Definition: field_mask_util.h:233
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
descriptor.h
google::protobuf::util::FieldMaskUtil::MergeOptions
MergeOptions()
Definition: field_mask_util.h:199
google::protobuf::util::FieldMaskUtil::keep_required_fields_
bool keep_required_fields_
Definition: field_mask_util.h:236
google::protobuf::util::FieldMaskUtil::GetFieldMaskForAllFields
static FieldMask GetFieldMaskForAllFields()
Definition: field_mask_util.h:99
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::util::FieldMaskUtil::AddPathToFieldMask
static void AddPathToFieldMask(StringPiece path, FieldMask *mask)
Definition: field_mask_util.h:91
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
false
#define false
Definition: cJSON.c:70
google::protobuf::util::FieldMaskUtil::Subtract
static void Subtract(const FieldMask &mask1, const FieldMask &mask2, FieldMask *out)
Definition: field_mask_util.h:131
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51