json_patch.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  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. #include <folly/json_patch.h>
  17. namespace {
  18. using folly::StringPiece;
  19. // JSON patch operation names
  20. constexpr StringPiece kOperationTest = "test";
  21. constexpr StringPiece kOperationRemove = "remove";
  22. constexpr StringPiece kOperationAdd = "add";
  23. constexpr StringPiece kOperationReplace = "replace";
  24. constexpr StringPiece kOperationMove = "move";
  25. constexpr StringPiece kOperationCopy = "copy";
  26. // field tags in JSON patch
  27. constexpr StringPiece kOpTag = "op";
  28. constexpr StringPiece kValueTag = "value";
  29. constexpr StringPiece kPathTag = "path";
  30. constexpr StringPiece kFromTag = "from";
  31. } // namespace
  32. namespace folly {
  33. // static
  34. Expected<json_patch, json_patch::parse_error> json_patch::try_parse(
  35. dynamic const& obj) noexcept {
  36. using err_code = parse_error_code;
  37. json_patch patch;
  38. if (!obj.isArray()) {
  39. return makeUnexpected(parse_error{err_code::invalid_shape, &obj});
  40. }
  41. for (auto const& elem : obj) {
  42. if (!elem.isObject()) {
  43. return makeUnexpected(parse_error{err_code::invalid_shape, &elem});
  44. }
  45. auto const* op_ptr = elem.get_ptr(kOpTag);
  46. if (!op_ptr) {
  47. return makeUnexpected(parse_error{err_code::missing_op, &elem});
  48. }
  49. if (!op_ptr->isString()) {
  50. return makeUnexpected(parse_error{err_code::malformed_op, &elem});
  51. }
  52. auto const op_str = op_ptr->asString();
  53. patch_operation op;
  54. // extract 'from' attribute
  55. {
  56. auto const* from_ptr = elem.get_ptr(kFromTag);
  57. if (from_ptr) {
  58. if (!from_ptr->isString()) {
  59. return makeUnexpected(parse_error{err_code::invalid_shape, &elem});
  60. }
  61. auto json_ptr = json_pointer::try_parse(from_ptr->asString());
  62. if (!json_ptr.hasValue()) {
  63. return makeUnexpected(
  64. parse_error{err_code::malformed_from_attr, &elem});
  65. }
  66. op.from = json_ptr.value();
  67. }
  68. }
  69. // extract 'path' attribute
  70. {
  71. auto const* path_ptr = elem.get_ptr(kPathTag);
  72. if (!path_ptr) {
  73. return makeUnexpected(parse_error{err_code::missing_path_attr, &elem});
  74. }
  75. if (!path_ptr->isString()) {
  76. return makeUnexpected(
  77. parse_error{err_code::malformed_path_attr, &elem});
  78. }
  79. auto const json_ptr = json_pointer::try_parse(path_ptr->asString());
  80. if (!json_ptr.hasValue()) {
  81. return makeUnexpected(
  82. parse_error{err_code::malformed_path_attr, &elem});
  83. }
  84. op.path = json_ptr.value();
  85. }
  86. // extract 'value' attribute
  87. {
  88. auto const* val_ptr = elem.get_ptr(kValueTag);
  89. if (val_ptr) {
  90. op.value = *val_ptr;
  91. }
  92. }
  93. // check mandatory attributes - different per operation
  94. // NOTE: per RFC, the surplus attributes (e.g. 'from' with 'add')
  95. // should be simply ignored
  96. using op_code = patch_operation_code;
  97. if (op_str == kOperationTest) {
  98. if (!op.value) {
  99. return makeUnexpected(parse_error{err_code::missing_value_attr, &elem});
  100. }
  101. op.op_code = op_code::test;
  102. } else if (op_str == kOperationRemove) {
  103. op.op_code = op_code::remove;
  104. } else if (op_str == kOperationAdd) {
  105. if (!op.value) {
  106. return makeUnexpected(parse_error{err_code::missing_value_attr, &elem});
  107. }
  108. op.op_code = op_code::add;
  109. } else if (op_str == kOperationReplace) {
  110. if (!op.value) {
  111. return makeUnexpected(parse_error{err_code::missing_value_attr, &elem});
  112. }
  113. op.op_code = op_code::replace;
  114. } else if (op_str == kOperationMove) {
  115. if (!op.from) {
  116. return makeUnexpected(parse_error{err_code::missing_from_attr, &elem});
  117. }
  118. // is from a proper prefix to path?
  119. if (op.from->is_prefix_of(op.path)) {
  120. return makeUnexpected(
  121. parse_error{err_code::overlapping_pointers, &elem});
  122. }
  123. op.op_code = op_code::move;
  124. } else if (op_str == kOperationCopy) {
  125. if (!op.from) {
  126. return makeUnexpected(parse_error{err_code::missing_from_attr, &elem});
  127. }
  128. op.op_code = op_code::copy;
  129. }
  130. if (op.op_code != op_code::invalid) {
  131. patch.ops_.emplace_back(std::move(op));
  132. } else {
  133. return makeUnexpected(parse_error{err_code::unknown_op, &elem});
  134. }
  135. }
  136. return patch;
  137. }
  138. std::vector<json_patch::patch_operation> const& json_patch::ops() const {
  139. return ops_;
  140. }
  141. } // namespace folly