Skip to content

Commit

Permalink
add DeprecatedOptionalField structure
Browse files Browse the repository at this point in the history
Summary:
Currently if we have "optionals" in thrift_cpp2_options, we will use folly::Optional for optional fields. The motivation of this task is that we want to get rid of "optionals" in thrift_cpp2_options and migrate existing code to use optional_field_ref instead.

As Step #1, we will create new thrift::DeprecatedOptionalField that's compatible with folly::Optional, and can be implicitly casted to each other. After the first step it done, we can codemod the API of thrift::DeprecatedOptionalField to make it identical to optional_field_ref.

This diff created DeprecatedOptionalField struct. I will modify thrift compiler in following diff to generate optional field with this code.

Reviewed By: vitaut

Differential Revision: D19785312

fbshipit-source-id: b5fcaadc9aec8d0a6a223e69bbcb7b565a27210f
  • Loading branch information
Mizuchi authored and facebook-github-bot committed Feb 11, 2020
1 parent 587989f commit 717dbbe
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
116 changes: 116 additions & 0 deletions thrift/lib/cpp2/OptionalField.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <folly/Optional.h>
#include <folly/Portability.h>
#include <type_traits>

namespace apache {
namespace thrift {

/**
* DeprecatedOptionalField is used for thrift optional field with "optionals"
* turned on. It will be eventually replaced by thrift::optional_field_ref.
*/
template <typename T>
class DeprecatedOptionalField : public folly::Optional<T> {
private:
const folly::Optional<T>& toFolly() const {
return *this;
}

public:
using folly::Optional<T>::Optional;
using folly::Optional<T>::operator=;

template <typename L, typename R>
friend bool operator==(
const DeprecatedOptionalField<L>& lhs,
const DeprecatedOptionalField<R>& rhs);
template <typename L, typename R>
friend bool operator!=(
const DeprecatedOptionalField<L>& lhs,
const DeprecatedOptionalField<R>& rhs);
template <typename U>
friend bool operator==(
const DeprecatedOptionalField<U>& lhs,
const folly::Optional<U>& rhs);
template <typename U>
friend bool operator==(
const folly::Optional<U>& lhs,
const DeprecatedOptionalField<U>& rhs);
template <typename U>
friend bool operator!=(
const DeprecatedOptionalField<U>& lhs,
const folly::Optional<U>& rhs);
template <typename U>
friend bool operator!=(
const folly::Optional<U>& lhs,
const DeprecatedOptionalField<U>& rhs);
};

template <typename L, typename R>
bool operator==(
const DeprecatedOptionalField<L>& lhs,
const DeprecatedOptionalField<R>& rhs) {
return lhs.toFolly() == rhs.toFolly();
}
template <typename L, typename R>
bool operator!=(
const DeprecatedOptionalField<L>& lhs,
const DeprecatedOptionalField<R>& rhs) {
return lhs.toFolly() != rhs.toFolly();
}
template <typename U>
bool operator==(
const DeprecatedOptionalField<U>& lhs,
const folly::Optional<U>& rhs) {
return lhs.toFolly() == rhs;
}
template <typename U>
bool operator==(
const folly::Optional<U>& lhs,
const DeprecatedOptionalField<U>& rhs) {
return lhs == rhs.toFolly();
}
template <typename U>
bool operator!=(
const DeprecatedOptionalField<U>& lhs,
const folly::Optional<U>& rhs) {
return lhs.toFolly() != rhs;
}
template <typename U>
bool operator!=(
const folly::Optional<U>& lhs,
const DeprecatedOptionalField<U>& rhs) {
return lhs != rhs.toFolly();
}

template <class T>
folly::Optional<T> castToFolly(const DeprecatedOptionalField<T>& t) {
return t;
}

} // namespace thrift
} // namespace apache

FOLLY_NAMESPACE_STD_BEGIN
template <class T>
struct hash<apache::thrift::DeprecatedOptionalField<T>>
: hash<folly::Optional<T>> {};
FOLLY_NAMESPACE_STD_END
1 change: 1 addition & 0 deletions thrift/lib/cpp2/Thrift.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <thrift/lib/cpp/Thrift.h>
#include <thrift/lib/cpp/protocol/TType.h>
#include <thrift/lib/cpp2/FieldRef.h>
#include <thrift/lib/cpp2/OptionalField.h>
#include <thrift/lib/cpp2/TypeClass.h>

#include <initializer_list>
Expand Down

0 comments on commit 717dbbe

Please sign in to comment.