protobuf/src/google/protobuf/util/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 <cstdint>
37 #include <string>
38 
39 #include <google/protobuf/field_mask.pb.h>
40 #include <google/protobuf/descriptor.h>
41 #include <google/protobuf/stubs/strutil.h>
42 
43 // Must be included last.
44 #include <google/protobuf/port_def.inc>
45 
46 namespace google {
47 namespace protobuf {
48 namespace util {
49 
50 class PROTOBUF_EXPORT FieldMaskUtil {
52 
53  public:
54  // Converts FieldMask to/from string, formatted by separating each path
55  // with a comma (e.g., "foo_bar,baz.quz").
56  static std::string ToString(const FieldMask& mask);
57  static void FromString(StringPiece str, FieldMask* out);
58 
59  // Populates the FieldMask with the paths corresponding to the fields with the
60  // given numbers, after checking that all field numbers are valid.
61  template <typename T>
62  static void FromFieldNumbers(const std::vector<int64_t>& field_numbers,
63  FieldMask* out) {
64  for (const auto field_number : field_numbers) {
65  const FieldDescriptor* field_desc =
66  T::descriptor()->FindFieldByNumber(field_number);
67  GOOGLE_CHECK(field_desc != nullptr)
68  << "Invalid field number for " << T::descriptor()->full_name() << ": "
69  << field_number;
70  AddPathToFieldMask<T>(field_desc->lowercase_name(), out);
71  }
72  }
73 
74  // Converts FieldMask to/from string, formatted according to proto3 JSON
75  // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not
76  // style conforming (i.e., not snake_case when converted to string, or not
77  // camelCase when converted from string), the conversion will fail.
78  static bool ToJsonString(const FieldMask& mask, std::string* out);
79  static bool FromJsonString(StringPiece str, FieldMask* out);
80 
81  // Get the descriptors of the fields which the given path from the message
82  // descriptor traverses, if field_descriptors is not null.
83  // Return false if the path is not valid, and the content of field_descriptors
84  // is unspecified.
85  static bool GetFieldDescriptors(
87  std::vector<const FieldDescriptor*>* field_descriptors);
88 
89  // Checks whether the given path is valid for type T.
90  template <typename T>
91  static bool IsValidPath(StringPiece path) {
92  return GetFieldDescriptors(T::descriptor(), path, nullptr);
93  }
94 
95  // Checks whether the given FieldMask is valid for type T.
96  template <typename T>
97  static bool IsValidFieldMask(const FieldMask& mask) {
98  for (int i = 0; i < mask.paths_size(); ++i) {
99  if (!GetFieldDescriptors(T::descriptor(), mask.paths(i), nullptr))
100  return false;
101  }
102  return true;
103  }
104 
105  // Adds a path to FieldMask after checking whether the given path is valid.
106  // This method check-fails if the path is not a valid path for type T.
107  template <typename T>
109  GOOGLE_CHECK(IsValidPath<T>(path)) << path;
110  mask->add_paths(std::string(path));
111  }
112 
113  // Creates a FieldMask with all fields of type T. This FieldMask only
114  // contains fields of T but not any sub-message fields.
115  template <typename T>
117  FieldMask out;
118  GetFieldMaskForAllFields(T::descriptor(), &out);
119  return out;
120  }
121  template <typename T>
122  PROTOBUF_DEPRECATED_MSG("Use *out = GetFieldMaskForAllFields() instead")
123  static void GetFieldMaskForAllFields(FieldMask* out) {
124  GetFieldMaskForAllFields(T::descriptor(), out);
125  }
126  // This flavor takes the protobuf type descriptor as an argument.
127  // Useful when the type is not known at compile time.
128  static void GetFieldMaskForAllFields(const Descriptor* descriptor,
129  FieldMask* out);
130 
131  // Converts a FieldMask to the canonical form. It will:
132  // 1. Remove paths that are covered by another path. For example,
133  // "foo.bar" is covered by "foo" and will be removed if "foo"
134  // is also in the FieldMask.
135  // 2. Sort all paths in alphabetical order.
136  static void ToCanonicalForm(const FieldMask& mask, FieldMask* out);
137 
138  // Creates an union of two FieldMasks.
139  static void Union(const FieldMask& mask1, const FieldMask& mask2,
140  FieldMask* out);
141 
142  // Creates an intersection of two FieldMasks.
143  static void Intersect(const FieldMask& mask1, const FieldMask& mask2,
144  FieldMask* out);
145 
146  // Subtracts mask2 from mask1 base of type T.
147  template <typename T>
148  static void Subtract(const FieldMask& mask1, const FieldMask& mask2,
149  FieldMask* out) {
150  Subtract(T::descriptor(), mask1, mask2, out);
151  }
152  // This flavor takes the protobuf type descriptor as an argument.
153  // Useful when the type is not known at compile time.
154  static void Subtract(const Descriptor* descriptor, const FieldMask& mask1,
155  const FieldMask& mask2, FieldMask* out);
156 
157  // Returns true if path is covered by the given FieldMask. Note that path
158  // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc.
159  // Also note that parent paths are not covered by explicit child path, i.e.
160  // "foo.bar" does NOT cover "foo", even if "bar" is the only child.
161  static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask);
162 
163  class MergeOptions;
164  // Merges fields specified in a FieldMask into another message.
165  static void MergeMessageTo(const Message& source, const FieldMask& mask,
166  const MergeOptions& options, Message* destination);
167 
168  class TrimOptions;
169  // Removes from 'message' any field that is not represented in the given
170  // FieldMask. If the FieldMask is empty, does nothing.
171  // Returns true if the message is modified.
172  static bool TrimMessage(const FieldMask& mask, Message* message);
173 
174  // Removes from 'message' any field that is not represented in the given
175  // FieldMask with customized TrimOptions.
176  // If the FieldMask is empty, does nothing.
177  // Returns true if the message is modified.
178  static bool TrimMessage(const FieldMask& mask, Message* message,
179  const TrimOptions& options);
180 
181  private:
182  friend class SnakeCaseCamelCaseTest;
183  // Converts a field name from snake_case to camelCase:
184  // 1. Every character after "_" will be converted to uppercase.
185  // 2. All "_"s are removed.
186  // The conversion will fail if:
187  // 1. The field name contains uppercase letters.
188  // 2. Any character after a "_" is not a lowercase letter.
189  // If the conversion succeeds, it's guaranteed that the resulted
190  // camelCase name will yield the original snake_case name when
191  // converted using CamelCaseToSnakeCase().
192  //
193  // Note that the input can contain characters not allowed in C identifiers.
194  // For example, "foo_bar,baz_quz" will be converted to "fooBar,bazQuz"
195  // successfully.
196  static bool SnakeCaseToCamelCase(StringPiece input,
198  // Converts a field name from camelCase to snake_case:
199  // 1. Every uppercase letter is converted to lowercase with an additional
200  // preceding "_".
201  // The conversion will fail if:
202  // 1. The field name contains "_"s.
203  // If the conversion succeeds, it's guaranteed that the resulted
204  // snake_case name will yield the original camelCase name when
205  // converted using SnakeCaseToCamelCase().
206  //
207  // Note that the input can contain characters not allowed in C identifiers.
208  // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz"
209  // successfully.
210  static bool CamelCaseToSnakeCase(StringPiece input,
212 };
213 
214 class PROTOBUF_EXPORT FieldMaskUtil::MergeOptions {
215  public:
217  : replace_message_fields_(false), replace_repeated_fields_(false) {}
218  // When merging message fields, the default behavior is to merge the
219  // content of two message fields together. If you instead want to use
220  // the field from the source message to replace the corresponding field
221  // in the destination message, set this flag to true. When this flag is set,
222  // specified submessage fields that are missing in source will be cleared in
223  // destination.
225  replace_message_fields_ = value;
226  }
227  bool replace_message_fields() const { return replace_message_fields_; }
228  // The default merging behavior will append entries from the source
229  // repeated field to the destination repeated field. If you only want
230  // to keep the entries from the source repeated field, set this flag
231  // to true.
233  replace_repeated_fields_ = value;
234  }
235  bool replace_repeated_fields() const { return replace_repeated_fields_; }
236 
237  private:
238  bool replace_message_fields_;
239  bool replace_repeated_fields_;
240 };
241 
242 class PROTOBUF_EXPORT FieldMaskUtil::TrimOptions {
243  public:
244  TrimOptions() : keep_required_fields_(false) {}
245  // When trimming message fields, the default behavior is to trim required
246  // fields of the present message if they are not specified in the field mask.
247  // If you instead want to keep required fields of the present message even
248  // when they are not specified in the field mask, set this flag to true.
249  void set_keep_required_fields(bool value) { keep_required_fields_ = value; }
250  bool keep_required_fields() const { return keep_required_fields_; }
251 
252  private:
253  bool keep_required_fields_;
254 };
255 
256 } // namespace util
257 } // namespace protobuf
258 } // namespace google
259 
260 #include <google/protobuf/port_undef.inc>
261 
262 #endif // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
xds_interop_client.str
str
Definition: xds_interop_client.py:487
google::protobuf::util::FieldMaskUtil::TrimOptions
TrimOptions()
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:244
google::protobuf::util::FieldMaskUtil::set_replace_repeated_fields
void set_replace_repeated_fields(bool value)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:232
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
false
#define false
Definition: setup_once.h:323
FieldMask
Definition: bloaty/third_party/protobuf/src/google/protobuf/field_mask.pb.h:69
FieldMask::add_paths
std::string * add_paths()
Definition: bloaty/third_party/protobuf/src/google/protobuf/field_mask.pb.h:254
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::util::FieldMaskUtil::replace_message_fields
bool replace_message_fields() const
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:227
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
check_documentation.path
path
Definition: check_documentation.py:57
google::protobuf::util::FieldMaskUtil::replace_repeated_fields
bool replace_repeated_fields() const
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:235
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
FieldMask::paths_size
int paths_size() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/field_mask.pb.h:248
google::protobuf::util::SnakeCaseCamelCaseTest
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/field_mask_util_test.cc:46
FieldMask
struct FieldMask FieldMask
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:649
ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf::StringPiece
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:180
google::protobuf::util::FieldMaskUtil::IsValidPath
static bool IsValidPath(StringPiece path)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:91
FieldMask::paths
const std::string & paths(int index) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/field_mask.pb.h:261
google::protobuf::util::FieldMaskUtil::IsValidFieldMask
static bool IsValidFieldMask(const FieldMask &mask)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:97
google::protobuf::util::FieldMaskUtil::FieldMask
google::protobuf::FieldMask FieldMask
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:51
google::protobuf::FieldMask
google::protobuf::util::FieldMaskUtil::set_replace_message_fields
void set_replace_message_fields(bool value)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:224
google::protobuf::Message
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:205
google::protobuf.internal.python_message.FromString
FromString
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:796
google::protobuf::util::FieldMaskUtil::set_keep_required_fields
void set_keep_required_fields(bool value)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:249
google::protobuf::util::FieldMaskUtil::keep_required_fields
bool keep_required_fields() const
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:250
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:153
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
google::protobuf::util::FieldMaskUtil::MergeOptions
MergeOptions()
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:216
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
google::protobuf::util::FieldMaskUtil::FromFieldNumbers
static void FromFieldNumbers(const std::vector< int64_t > &field_numbers, FieldMask *out)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:62
google::protobuf::util::FieldMaskUtil::GetFieldMaskForAllFields
static FieldMask GetFieldMaskForAllFields()
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:116
google::protobuf::util::FieldMaskUtil::AddPathToFieldMask
static void AddPathToFieldMask(StringPiece path, FieldMask *mask)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:108
google::protobuf::FieldDescriptor::lowercase_name
const std::string & lowercase_name() const
google::protobuf::util::FieldMaskUtil::Subtract
static void Subtract(const FieldMask &mask1, const FieldMask &mask2, FieldMask *out)
Definition: protobuf/src/google/protobuf/util/field_mask_util.h:148
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:22