|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <thrift/lib/cpp2/op/detail/AssignPatch.h> |
| 20 | + |
| 21 | +namespace apache::thrift::op::detail { |
| 22 | + |
| 23 | +template <typename Patch> |
| 24 | +void AssignPatch<Patch>::apply(T& val) const { |
| 25 | + if (dynPatch_) { |
| 26 | + auto value = protocol::asValueStruct<Tag>(val); |
| 27 | + dynPatch_->apply(value); |
| 28 | + val = protocol::fromValueStruct<Tag>(value); |
| 29 | + } else if (auto p = data_.assign()) { |
| 30 | + val = data_.assign().value(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +template <typename Patch> |
| 35 | +void AssignPatch<Patch>::merge(AssignPatch other) { |
| 36 | + if (dynPatch_ && other.dynPatch_) { |
| 37 | + XLOG_EVERY_MS(CRITICAL, 10000) |
| 38 | + << "Merging dynamic patch is not implemented. " |
| 39 | + "The merged result will be incorrect.\n" |
| 40 | + "First Patch = " |
| 41 | + << folly::toPrettyJson( |
| 42 | + apache::thrift::protocol::toDynamic(dynPatch_->toObject())) |
| 43 | + << "\nSecond Patch = " |
| 44 | + << folly::toPrettyJson( |
| 45 | + apache::thrift::protocol::toDynamic( |
| 46 | + other.dynPatch_->toObject())); |
| 47 | + |
| 48 | + // Do nothing, which is the old behavior |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (!other.dynPatch_) { |
| 53 | + if (auto p = other.data_.assign()) { |
| 54 | + assign(std::move(*p)); |
| 55 | + } |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (auto p = data_.assign()) { |
| 60 | + other.apply(*p); |
| 61 | + } else { |
| 62 | + dynPatch_ = std::move(other.dynPatch_); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +} // namespace apache::thrift::op::detail |
0 commit comments