• 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 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
32 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
33 
34 #include <string>
35 
36 #include <google/protobuf/descriptor.h>
37 #include <google/protobuf/field_mask.pb.h>
38 #include <google/protobuf/stubs/stringpiece.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace util {
43 
44 class LIBPROTOBUF_EXPORT FieldMaskUtil {
45   typedef google::protobuf::FieldMask FieldMask;
46 
47  public:
48   // Converts FieldMask to/from string, formatted by separating each path
49   // with a comma (e.g., "foo_bar,baz.quz").
50   static string ToString(const FieldMask& mask);
51   static void FromString(StringPiece str, FieldMask* out);
52 
53   // Converts FieldMask to/from string, formatted according to proto3 JSON
54   // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not
55   // style conforming (i.e., not snake_case when converted to string, or not
56   // camelCase when converted from string), the conversion will fail.
57   static bool ToJsonString(const FieldMask& mask, string* out);
58   static bool FromJsonString(StringPiece str, FieldMask* out);
59 
60   // Checks whether the given path is valid for type T.
61   template <typename T>
IsValidPath(StringPiece path)62   static bool IsValidPath(StringPiece path) {
63     return InternalIsValidPath(T::descriptor(), path);
64   }
65 
66   // Checks whether the given FieldMask is valid for type T.
67   template <typename T>
IsValidFieldMask(const FieldMask & mask)68   static bool IsValidFieldMask(const FieldMask& mask) {
69     for (int i = 0; i < mask.paths_size(); ++i) {
70       if (!InternalIsValidPath(T::descriptor(), mask.paths(i))) return false;
71     }
72     return true;
73   }
74 
75   // Adds a path to FieldMask after checking whether the given path is valid.
76   // This method check-fails if the path is not a valid path for type T.
77   template <typename T>
AddPathToFieldMask(StringPiece path,FieldMask * mask)78   static void AddPathToFieldMask(StringPiece path, FieldMask* mask) {
79     GOOGLE_CHECK(IsValidPath<T>(path));
80     mask->add_paths(path);
81   }
82 
83   // Creates a FieldMask with all fields of type T. This FieldMask only
84   // contains fields of T but not any sub-message fields.
85   template <typename T>
GetFieldMaskForAllFields(FieldMask * out)86   static void GetFieldMaskForAllFields(FieldMask* out) {
87     InternalGetFieldMaskForAllFields(T::descriptor(), out);
88   }
89 
90   // Converts a FieldMask to the canonical form. It will:
91   //   1. Remove paths that are covered by another path. For example,
92   //      "foo.bar" is covered by "foo" and will be removed if "foo"
93   //      is also in the FieldMask.
94   //   2. Sort all paths in alphabetical order.
95   static void ToCanonicalForm(const FieldMask& mask, FieldMask* out);
96 
97   // Creates an union of two FieldMasks.
98   static void Union(const FieldMask& mask1, const FieldMask& mask2,
99                     FieldMask* out);
100 
101   // Creates an intersection of two FieldMasks.
102   static void Intersect(const FieldMask& mask1, const FieldMask& mask2,
103                         FieldMask* out);
104 
105   // Returns true if path is covered by the given FieldMask. Note that path
106   // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc.
107   static bool IsPathInFieldMask(StringPiece path, const FieldMask& mask);
108 
109   class MergeOptions;
110   // Merges fields specified in a FieldMask into another message.
111   static void MergeMessageTo(const Message& source, const FieldMask& mask,
112                              const MergeOptions& options, Message* destination);
113 
114  private:
115   friend class SnakeCaseCamelCaseTest;
116   // Converts a field name from snake_case to camelCase:
117   //   1. Every character after "_" will be converted to uppercase.
118   //   2. All "_"s are removed.
119   // The conversion will fail if:
120   //   1. The field name contains uppercase letters.
121   //   2. Any character after a "_" is not a lowercase letter.
122   // If the conversion succeeds, it's guaranteed that the resulted
123   // camelCase name will yield the original snake_case name when
124   // converted using CamelCaseToSnakeCase().
125   //
126   // Note that the input can contain characters not allowed in C identifiers.
127   // For example, "foo_bar,baz_quz" will be converted to "fooBar,bazQuz"
128   // successfully.
129   static bool SnakeCaseToCamelCase(StringPiece input, string* output);
130   // Converts a field name from camelCase to snake_case:
131   //   1. Every uppercase letter is converted to lowercase with a additional
132   //      preceding "-".
133   // The conversion will fail if:
134   //   1. The field name contains "_"s.
135   // If the conversion succeeds, it's guaranteed that the resulted
136   // snake_case name will yield the original camelCase name when
137   // converted using SnakeCaseToCamelCase().
138   //
139   // Note that the input can contain characters not allowed in C identifiers.
140   // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz"
141   // successfully.
142   static bool CamelCaseToSnakeCase(StringPiece input, string* output);
143 
144   static bool InternalIsValidPath(const Descriptor* descriptor,
145                                   StringPiece path);
146 
147   static void InternalGetFieldMaskForAllFields(const Descriptor* descriptor,
148                                                FieldMask* out);
149 };
150 
151 class LIBPROTOBUF_EXPORT FieldMaskUtil::MergeOptions {
152  public:
MergeOptions()153   MergeOptions()
154       : replace_message_fields_(false), replace_repeated_fields_(false) {}
155   // When merging message fields, the default behavior is to merge the
156   // content of two message fields together. If you instead want to use
157   // the field from the source message to replace the corresponding field
158   // in the destination message, set this flag to true. When this flag is set,
159   // specified submessage fields that are missing in source will be cleared in
160   // destination.
set_replace_message_fields(bool value)161   void set_replace_message_fields(bool value) {
162     replace_message_fields_ = value;
163   }
replace_message_fields()164   bool replace_message_fields() const { return replace_message_fields_; }
165   // The default merging behavior will append entries from the source
166   // repeated field to the destination repeated field. If you only want
167   // to keep the entries from the source repeated field, set this flag
168   // to true.
set_replace_repeated_fields(bool value)169   void set_replace_repeated_fields(bool value) {
170     replace_repeated_fields_ = value;
171   }
replace_repeated_fields()172   bool replace_repeated_fields() const { return replace_repeated_fields_; }
173 
174  private:
175   bool replace_message_fields_;
176   bool replace_repeated_fields_;
177 };
178 
179 }  // namespace util
180 }  // namespace protobuf
181 
182 }  // namespace google
183 #endif  // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
184