• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
38 #include <google/protobuf/field_mask.pb.h>
39 #include <google/protobuf/descriptor.h>
40 #include <google/protobuf/stubs/stringpiece.h>
41 
42 #include <google/protobuf/port_def.inc>
43 
44 namespace google {
45 namespace protobuf {
46 namespace util {
47 
48 class PROTOBUF_EXPORT FieldMaskUtil {
49   typedef google::protobuf::FieldMask FieldMask;
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(
69       const Descriptor* descriptor, StringPiece path,
70       std::vector<const FieldDescriptor*>* field_descriptors);
71 
72   // Checks whether the given path is valid for type T.
73   template <typename T>
IsValidPath(StringPiece path)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>
IsValidFieldMask(const FieldMask & mask)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>
AddPathToFieldMask(StringPiece path,FieldMask * mask)91   static void AddPathToFieldMask(StringPiece path, FieldMask* mask) {
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>
GetFieldMaskForAllFields()99   static FieldMask GetFieldMaskForAllFields() {
100     FieldMask out;
101     GetFieldMaskForAllFields(T::descriptor(), &out);
102     return out;
103   }
104   template <typename T>
105   PROTOBUF_DEPRECATED_MSG("Use *out = GetFieldMaskForAllFields() instead")
GetFieldMaskForAllFields(FieldMask * out)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>
Subtract(const FieldMask & mask1,const FieldMask & mask2,FieldMask * out)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:
165   friend class SnakeCaseCamelCaseTest;
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,
180                                    std::string* output);
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,
194                                    std::string* output);
195 };
196 
197 class PROTOBUF_EXPORT FieldMaskUtil::MergeOptions {
198  public:
MergeOptions()199   MergeOptions()
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.
set_replace_message_fields(bool value)207   void set_replace_message_fields(bool value) {
208     replace_message_fields_ = value;
209   }
replace_message_fields()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.
set_replace_repeated_fields(bool value)215   void set_replace_repeated_fields(bool value) {
216     replace_repeated_fields_ = value;
217   }
replace_repeated_fields()218   bool replace_repeated_fields() const { return replace_repeated_fields_; }
219 
220  private:
221   bool replace_message_fields_;
222   bool replace_repeated_fields_;
223 };
224 
225 class PROTOBUF_EXPORT FieldMaskUtil::TrimOptions {
226  public:
TrimOptions()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.
set_keep_required_fields(bool value)232   void set_keep_required_fields(bool value) { keep_required_fields_ = value; }
keep_required_fields()233   bool keep_required_fields() const { return keep_required_fields_; }
234 
235  private:
236   bool keep_required_fields_;
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__
246