Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:04

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Defines utilities for the FieldMask well known type.
0009 
0010 #ifndef GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
0011 #define GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__
0012 
0013 #include <cstdint>
0014 #include <string>
0015 #include <vector>
0016 
0017 #include "google/protobuf/field_mask.pb.h"
0018 #include "absl/log/absl_check.h"
0019 #include "absl/strings/string_view.h"
0020 #include "google/protobuf/descriptor.h"
0021 
0022 // Must be included last.
0023 #include "google/protobuf/port_def.inc"
0024 
0025 namespace google {
0026 namespace protobuf {
0027 namespace util {
0028 
0029 class PROTOBUF_EXPORT FieldMaskUtil {
0030   typedef google::protobuf::FieldMask FieldMask;
0031 
0032  public:
0033   // Converts FieldMask to/from string, formatted by separating each path
0034   // with a comma (e.g., "foo_bar,baz.quz").
0035   static std::string ToString(const FieldMask& mask);
0036   static void FromString(absl::string_view str, FieldMask* out);
0037 
0038   // Populates the FieldMask with the paths corresponding to the fields with the
0039   // given numbers, after checking that all field numbers are valid.
0040   template <typename T>
0041   static void FromFieldNumbers(const std::vector<int64_t>& field_numbers,
0042                                FieldMask* out) {
0043     for (const auto field_number : field_numbers) {
0044       const FieldDescriptor* field_desc =
0045           T::descriptor()->FindFieldByNumber(field_number);
0046       ABSL_CHECK(field_desc != nullptr)
0047           << "Invalid field number for " << T::descriptor()->full_name() << ": "
0048           << field_number;
0049       AddPathToFieldMask<T>(field_desc->lowercase_name(), out);
0050     }
0051   }
0052 
0053   // Converts FieldMask to/from string, formatted according to proto3 JSON
0054   // spec for FieldMask (e.g., "fooBar,baz.quz"). If the field name is not
0055   // style conforming (i.e., not snake_case when converted to string, or not
0056   // camelCase when converted from string), the conversion will fail.
0057   static bool ToJsonString(const FieldMask& mask, std::string* out);
0058   static bool FromJsonString(absl::string_view str, FieldMask* out);
0059 
0060   // Get the descriptors of the fields which the given path from the message
0061   // descriptor traverses, if field_descriptors is not null.
0062   // Return false if the path is not valid, and the content of field_descriptors
0063   // is unspecified.
0064   static bool GetFieldDescriptors(
0065       const Descriptor* descriptor, absl::string_view path,
0066       std::vector<const FieldDescriptor*>* field_descriptors);
0067 
0068   // Checks whether the given path is valid for type T.
0069   template <typename T>
0070   static bool IsValidPath(absl::string_view path) {
0071     return GetFieldDescriptors(T::descriptor(), path, nullptr);
0072   }
0073 
0074   // Checks whether the given FieldMask is valid for type T.
0075   template <typename T>
0076   static bool IsValidFieldMask(const FieldMask& mask) {
0077     for (int i = 0; i < mask.paths_size(); ++i) {
0078       if (!GetFieldDescriptors(T::descriptor(), mask.paths(i), nullptr)) {
0079         return false;
0080       }
0081     }
0082     return true;
0083   }
0084 
0085   // Adds a path to FieldMask after checking whether the given path is valid.
0086   // This method check-fails if the path is not a valid path for type T.
0087   template <typename T>
0088   static void AddPathToFieldMask(absl::string_view path, FieldMask* mask) {
0089     ABSL_CHECK(IsValidPath<T>(path)) << path;
0090     mask->add_paths(std::string(path));
0091   }
0092 
0093   // Creates a FieldMask with all fields of type T. This FieldMask only
0094   // contains fields of T but not any sub-message fields.
0095   template <typename T>
0096   static FieldMask GetFieldMaskForAllFields() {
0097     FieldMask out;
0098     GetFieldMaskForAllFields(T::descriptor(), &out);
0099     return out;
0100   }
0101   template <typename T>
0102   [[deprecated("Use *out = GetFieldMaskForAllFields() instead")]] static void
0103   GetFieldMaskForAllFields(FieldMask* out) {
0104     GetFieldMaskForAllFields(T::descriptor(), out);
0105   }
0106   // This flavor takes the protobuf type descriptor as an argument.
0107   // Useful when the type is not known at compile time.
0108   static void GetFieldMaskForAllFields(const Descriptor* descriptor,
0109                                        FieldMask* out);
0110 
0111   // Converts a FieldMask to the canonical form. It will:
0112   //   1. Remove paths that are covered by another path. For example,
0113   //      "foo.bar" is covered by "foo" and will be removed if "foo"
0114   //      is also in the FieldMask.
0115   //   2. Sort all paths in alphabetical order.
0116   static void ToCanonicalForm(const FieldMask& mask, FieldMask* out);
0117 
0118   // Creates an union of two FieldMasks.
0119   static void Union(const FieldMask& mask1, const FieldMask& mask2,
0120                     FieldMask* out);
0121 
0122   // Creates an intersection of two FieldMasks.
0123   static void Intersect(const FieldMask& mask1, const FieldMask& mask2,
0124                         FieldMask* out);
0125 
0126   // Subtracts mask2 from mask1 base of type T.
0127   template <typename T>
0128   static void Subtract(const FieldMask& mask1, const FieldMask& mask2,
0129                        FieldMask* out) {
0130     Subtract(T::descriptor(), mask1, mask2, out);
0131   }
0132   // This flavor takes the protobuf type descriptor as an argument.
0133   // Useful when the type is not known at compile time.
0134   static void Subtract(const Descriptor* descriptor, const FieldMask& mask1,
0135                        const FieldMask& mask2, FieldMask* out);
0136 
0137   // Returns true if path is covered by the given FieldMask. Note that path
0138   // "foo.bar" covers all paths like "foo.bar.baz", "foo.bar.quz.x", etc.
0139   // Also note that parent paths are not covered by explicit child path, i.e.
0140   // "foo.bar" does NOT cover "foo", even if "bar" is the only child.
0141   static bool IsPathInFieldMask(absl::string_view path, const FieldMask& mask);
0142 
0143   class MergeOptions;
0144   // Merges fields specified in a FieldMask into another message.
0145   static void MergeMessageTo(const Message& source, const FieldMask& mask,
0146                              const MergeOptions& options, Message* destination);
0147 
0148   class TrimOptions;
0149   // Removes from 'message' any field that is not represented in the given
0150   // FieldMask. If the FieldMask is empty, does nothing.
0151   // Returns true if the message is modified.
0152   static bool TrimMessage(const FieldMask& mask, Message* message);
0153 
0154   // Removes from 'message' any field that is not represented in the given
0155   // FieldMask with customized TrimOptions.
0156   // If the FieldMask is empty, does nothing.
0157   // Returns true if the message is modified.
0158   static bool TrimMessage(const FieldMask& mask, Message* message,
0159                           const TrimOptions& options);
0160 
0161  private:
0162   friend class SnakeCaseCamelCaseTest;
0163   // Converts a field name from snake_case to camelCase:
0164   //   1. Every character after "_" will be converted to uppercase.
0165   //   2. All "_"s are removed.
0166   // The conversion will fail if:
0167   //   1. The field name contains uppercase letters.
0168   //   2. Any character after a "_" is not a lowercase letter.
0169   // If the conversion succeeds, it's guaranteed that the resulted
0170   // camelCase name will yield the original snake_case name when
0171   // converted using CamelCaseToSnakeCase().
0172   //
0173   // Note that the input can contain characters not allowed in C identifiers.
0174   // For example, "foo_bar,baz_quz" will be converted to "fooBar,bazQuz"
0175   // successfully.
0176   static bool SnakeCaseToCamelCase(absl::string_view input,
0177                                    std::string* output);
0178   // Converts a field name from camelCase to snake_case:
0179   //   1. Every uppercase letter is converted to lowercase with an additional
0180   //      preceding "_".
0181   // The conversion will fail if:
0182   //   1. The field name contains "_"s.
0183   // If the conversion succeeds, it's guaranteed that the resulted
0184   // snake_case name will yield the original camelCase name when
0185   // converted using SnakeCaseToCamelCase().
0186   //
0187   // Note that the input can contain characters not allowed in C identifiers.
0188   // For example, "fooBar,bazQuz" will be converted to "foo_bar,baz_quz"
0189   // successfully.
0190   static bool CamelCaseToSnakeCase(absl::string_view input,
0191                                    std::string* output);
0192 };
0193 
0194 class PROTOBUF_EXPORT FieldMaskUtil::MergeOptions {
0195  public:
0196   MergeOptions()
0197       : replace_message_fields_(false), replace_repeated_fields_(false) {}
0198   // When merging message fields, the default behavior is to merge the
0199   // content of two message fields together. If you instead want to use
0200   // the field from the source message to replace the corresponding field
0201   // in the destination message, set this flag to true. When this flag is set,
0202   // specified submessage fields that are missing in source will be cleared in
0203   // destination.
0204   void set_replace_message_fields(bool value) {
0205     replace_message_fields_ = value;
0206   }
0207   bool replace_message_fields() const { return replace_message_fields_; }
0208   // The default merging behavior will append entries from the source
0209   // repeated field to the destination repeated field. If you only want
0210   // to keep the entries from the source repeated field, set this flag
0211   // to true.
0212   void set_replace_repeated_fields(bool value) {
0213     replace_repeated_fields_ = value;
0214   }
0215   bool replace_repeated_fields() const { return replace_repeated_fields_; }
0216 
0217  private:
0218   bool replace_message_fields_;
0219   bool replace_repeated_fields_;
0220 };
0221 
0222 class PROTOBUF_EXPORT FieldMaskUtil::TrimOptions {
0223  public:
0224   TrimOptions() : keep_required_fields_(false) {}
0225   // When trimming message fields, the default behavior is to trim required
0226   // fields of the present message if they are not specified in the field mask.
0227   // If you instead want to keep required fields of the present message even
0228   // when they are not specified in the field mask, set this flag to true.
0229   void set_keep_required_fields(bool value) { keep_required_fields_ = value; }
0230   bool keep_required_fields() const { return keep_required_fields_; }
0231 
0232  private:
0233   bool keep_required_fields_;
0234 };
0235 
0236 }  // namespace util
0237 }  // namespace protobuf
0238 }  // namespace google
0239 
0240 #include "google/protobuf/port_undef.inc"
0241 
0242 #endif  // GOOGLE_PROTOBUF_UTIL_FIELD_MASK_UTIL_H__