patch_mutation.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FIRESTORE_CORE_SRC_MODEL_PATCH_MUTATION_H_
  17. #define FIRESTORE_CORE_SRC_MODEL_PATCH_MUTATION_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "Firestore/core/src/model/field_mask.h"
  24. #include "Firestore/core/src/model/model_fwd.h"
  25. #include "Firestore/core/src/model/mutation.h"
  26. namespace firebase {
  27. namespace firestore {
  28. namespace model {
  29. /**
  30. * A mutation that modifies fields of the document at the given key with the
  31. * given values. The values are applied through a field mask:
  32. *
  33. * - When a field is in both the mask and the values, the corresponding field is
  34. * updated.
  35. * - When a field is in neither the mask nor the values, the corresponding field
  36. * is unmodified.
  37. * - When a field is in the mask but not in the values, the corresponding field
  38. * is deleted.
  39. * - When a field is not in the mask but is in the values, the values map is
  40. * ignored.
  41. */
  42. class PatchMutation : public Mutation {
  43. public:
  44. PatchMutation(DocumentKey key,
  45. ObjectValue value,
  46. FieldMask mask,
  47. Precondition precondition);
  48. PatchMutation(DocumentKey key,
  49. ObjectValue value,
  50. FieldMask mask,
  51. Precondition precondition,
  52. std::vector<FieldTransform> field_transforms);
  53. /**
  54. * Casts a Mutation to a PatchMutation. This is a checked operation that will
  55. * assert if the type of the Mutation isn't actually Type::Patch.
  56. */
  57. explicit PatchMutation(const Mutation& mutation);
  58. /** Creates an invalid PatchMutation instance. */
  59. PatchMutation() = default;
  60. /**
  61. * Returns the fields and associated values to use when patching the document.
  62. */
  63. const ObjectValue& value() const {
  64. return patch_rep().value();
  65. }
  66. private:
  67. class Rep : public Mutation::Rep {
  68. public:
  69. Rep(DocumentKey&& key,
  70. ObjectValue&& value,
  71. FieldMask&& mask,
  72. Precondition&& precondition,
  73. std::vector<FieldTransform>&& field_transforms);
  74. Type type() const override {
  75. return Type::Patch;
  76. }
  77. const ObjectValue& value() const {
  78. return value_;
  79. }
  80. /**
  81. * Returns this patch mutation as a list of field paths to values (or
  82. * nullopt for deletes).
  83. */
  84. TransformMap GetPatch() const;
  85. void ApplyToRemoteDocument(
  86. MutableDocument& document,
  87. const MutationResult& mutation_result) const override;
  88. absl::optional<FieldMask> ApplyToLocalView(
  89. MutableDocument& document,
  90. absl::optional<FieldMask> previous_mask,
  91. const Timestamp& local_write_time) const override;
  92. bool Equals(const Mutation::Rep& other) const override;
  93. size_t Hash() const override;
  94. std::string ToString() const override;
  95. private:
  96. ObjectValue value_;
  97. };
  98. const Rep& patch_rep() const {
  99. return static_cast<const Rep&>(rep());
  100. }
  101. };
  102. } // namespace model
  103. } // namespace firestore
  104. } // namespace firebase
  105. #endif // FIRESTORE_CORE_SRC_MODEL_PATCH_MUTATION_H_