Skip to content

Commit 2760063

Browse files
iahsmeta-codesync[bot]
authored andcommitted
Migrate Carbon wrappers to adapters
Summary: Add a generic Carbon-to-Thrift adapter and migrate aliases from `cpp.Type`. Teach the Carbon generator to preserve adapter annotations in generated service IDL files. Reviewed By: vitaut Differential Revision: D115467149 fbshipit-source-id: dd556ebff88e5cb3e531b4fd6047e43c48293b8a
1 parent b4bf245 commit 2760063

7 files changed

Lines changed: 111 additions & 74 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <utility>
11+
12+
namespace carbon::util {
13+
14+
template <class Carbon, class Thrift>
15+
struct CarbonThriftAdapter {
16+
static Carbon fromThrift(Thrift value) {
17+
Carbon result;
18+
static_cast<Thrift&>(result) = std::move(value);
19+
return result;
20+
}
21+
22+
static const Thrift& toThrift(const Carbon& value) {
23+
return value;
24+
}
25+
26+
static Thrift& toThrift(Carbon& value) {
27+
return value;
28+
}
29+
};
30+
31+
} // namespace carbon::util

mcrouter/lib/carbon/example/gen/HelloGoodbyeService.thrift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include "mcrouter/lib/carbon/example/gen/HelloGoodbye.thrift"
1919
include "common/fb303/if/fb303.thrift"
2020

2121
cpp_include "mcrouter/lib/carbon/example/gen/HelloGoodbyeMessages.h"
22+
cpp_include "mcrouter/lib/carbon/CarbonThriftAdapter.h"
2223

2324
namespace cpp2 hellogoodbye.thrift
2425
namespace py3 hellogoodbye.thrift
@@ -32,15 +33,15 @@ service HelloGoodbye extends fb303.FacebookService {
3233
Common_McVersionReply mcVersion(1: Common_McVersionRequest request)
3334
}
3435

35-
@cpp.Type{name = "hellogoodbye::GoodbyeReply"}
36+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<hellogoodbye::GoodbyeReply, ::hellogoodbye::thrift::GoodbyeReply>"}
3637
typedef HelloGoodbye.GoodbyeReply HelloGoodbye_GoodbyeReply
37-
@cpp.Type{name = "hellogoodbye::GoodbyeRequest"}
38+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<hellogoodbye::GoodbyeRequest, ::hellogoodbye::thrift::GoodbyeRequest>"}
3839
typedef HelloGoodbye.GoodbyeRequest HelloGoodbye_GoodbyeRequest
39-
@cpp.Type{name = "hellogoodbye::HelloReply"}
40+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<hellogoodbye::HelloReply, ::hellogoodbye::thrift::HelloReply>"}
4041
typedef HelloGoodbye.HelloReply HelloGoodbye_HelloReply
41-
@cpp.Type{name = "hellogoodbye::HelloRequest"}
42+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<hellogoodbye::HelloRequest, ::hellogoodbye::thrift::HelloRequest>"}
4243
typedef HelloGoodbye.HelloRequest HelloGoodbye_HelloRequest
43-
@cpp.Type{name = "facebook::memcache::McVersionReply"}
44+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionReply, ::facebook::memcache::thrift::McVersionReply>"}
4445
typedef Common.McVersionReply Common_McVersionReply
45-
@cpp.Type{name = "facebook::memcache::McVersionRequest"}
46-
typedef Common.McVersionRequest Common_McVersionRequest
46+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionRequest, ::facebook::memcache::thrift::McVersionRequest>"}
47+
typedef Common.McVersionRequest Common_McVersionRequest

mcrouter/lib/carbon/test/gen/AService.thrift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include "mcrouter/lib/carbon/test/gen/B.thrift"
1919
include "common/fb303/if/fb303.thrift"
2020

2121
cpp_include "mcrouter/lib/carbon/test/gen/AMessages.h"
22+
cpp_include "mcrouter/lib/carbon/CarbonThriftAdapter.h"
2223

2324
namespace cpp2 carbon.test.A.thrift
2425
namespace py3 carbon.test.A.thrift
@@ -30,11 +31,11 @@ service A extends fb303.FacebookService {
3031
Common_McVersionReply mcVersion(1: Common_McVersionRequest request)
3132
}
3233

33-
@cpp.Type{name = "carbon::test::A::TestAReply"}
34+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::A::TestAReply, ::carbon::test::A::thrift::TestAReply>"}
3435
typedef A.TestAReply A_TestAReply
35-
@cpp.Type{name = "carbon::test::A::TestARequest"}
36+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::A::TestARequest, ::carbon::test::A::thrift::TestARequest>"}
3637
typedef A.TestARequest A_TestARequest
37-
@cpp.Type{name = "facebook::memcache::McVersionReply"}
38+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionReply, ::facebook::memcache::thrift::McVersionReply>"}
3839
typedef Common.McVersionReply Common_McVersionReply
39-
@cpp.Type{name = "facebook::memcache::McVersionRequest"}
40-
typedef Common.McVersionRequest Common_McVersionRequest
40+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionRequest, ::facebook::memcache::thrift::McVersionRequest>"}
41+
typedef Common.McVersionRequest Common_McVersionRequest

mcrouter/lib/carbon/test/gen/BService.thrift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include "mcrouter/lib/carbon/test/gen/B.thrift"
1919
include "common/fb303/if/fb303.thrift"
2020

2121
cpp_include "mcrouter/lib/carbon/test/gen/BMessages.h"
22+
cpp_include "mcrouter/lib/carbon/CarbonThriftAdapter.h"
2223

2324
namespace cpp2 carbon.test.B.thrift
2425
namespace py3 carbon.test.B.thrift
@@ -30,11 +31,11 @@ service B extends fb303.FacebookService {
3031
Common_McVersionReply mcVersion(1: Common_McVersionRequest request)
3132
}
3233

33-
@cpp.Type{name = "carbon::test::B::TestBReply"}
34+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::B::TestBReply, ::carbon::test::B::thrift::TestBReply>"}
3435
typedef B.TestBReply B_TestBReply
35-
@cpp.Type{name = "carbon::test::B::TestBRequest"}
36+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::B::TestBRequest, ::carbon::test::B::thrift::TestBRequest>"}
3637
typedef B.TestBRequest B_TestBRequest
37-
@cpp.Type{name = "facebook::memcache::McVersionReply"}
38+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionReply, ::facebook::memcache::thrift::McVersionReply>"}
3839
typedef Common.McVersionReply Common_McVersionReply
39-
@cpp.Type{name = "facebook::memcache::McVersionRequest"}
40-
typedef Common.McVersionRequest Common_McVersionRequest
40+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionRequest, ::facebook::memcache::thrift::McVersionRequest>"}
41+
typedef Common.McVersionRequest Common_McVersionRequest

mcrouter/lib/carbon/test/gen/CarbonTestService.thrift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include "mcrouter/lib/carbon/test/gen/CarbonTest.thrift"
1919
include "common/fb303/if/fb303.thrift"
2020

2121
cpp_include "mcrouter/lib/carbon/test/gen/CarbonTestMessages.h"
22+
cpp_include "mcrouter/lib/carbon/CarbonThriftAdapter.h"
2223

2324
namespace cpp2 carbon.test.thrift
2425
namespace py3 carbon.test.thrift
@@ -32,15 +33,15 @@ service CarbonTest extends fb303.FacebookService {
3233
Common_McVersionReply mcVersion(1: Common_McVersionRequest request)
3334
}
3435

35-
@cpp.Type{name = "carbon::test::TestReply"}
36+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::TestReply, ::carbon::test::thrift::TestReply>"}
3637
typedef CarbonTest.TestReply CarbonTest_TestReply
37-
@cpp.Type{name = "carbon::test::TestRequest"}
38+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::TestRequest, ::carbon::test::thrift::TestRequest>"}
3839
typedef CarbonTest.TestRequest CarbonTest_TestRequest
39-
@cpp.Type{name = "carbon::test::TestReplyStringKey"}
40+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::TestReplyStringKey, ::carbon::test::thrift::TestReplyStringKey>"}
4041
typedef CarbonTest.TestReplyStringKey CarbonTest_TestReplyStringKey
41-
@cpp.Type{name = "carbon::test::TestRequestStringKey"}
42+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::TestRequestStringKey, ::carbon::test::thrift::TestRequestStringKey>"}
4243
typedef CarbonTest.TestRequestStringKey CarbonTest_TestRequestStringKey
43-
@cpp.Type{name = "facebook::memcache::McVersionReply"}
44+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionReply, ::facebook::memcache::thrift::McVersionReply>"}
4445
typedef Common.McVersionReply Common_McVersionReply
45-
@cpp.Type{name = "facebook::memcache::McVersionRequest"}
46-
typedef Common.McVersionRequest Common_McVersionRequest
46+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionRequest, ::facebook::memcache::thrift::McVersionRequest>"}
47+
typedef Common.McVersionRequest Common_McVersionRequest

mcrouter/lib/carbon/test/gen/CarbonThriftTestService.thrift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include "mcrouter/lib/carbon/test/gen/CarbonThriftTest.thrift"
1919
include "common/fb303/if/fb303.thrift"
2020

2121
cpp_include "mcrouter/lib/carbon/test/gen/CarbonThriftTestMessages.h"
22+
cpp_include "mcrouter/lib/carbon/CarbonThriftAdapter.h"
2223

2324
namespace cpp2 carbon.test.thrift
2425
namespace py3 carbon.test.thrift
@@ -30,19 +31,19 @@ service CarbonThriftTest extends fb303.FacebookService {
3031
Common_McVersionReply mcVersion(1: Common_McVersionRequest request)
3132
}
3233

33-
@cpp.Type{name = "carbon::test::CustomReply"}
34+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::CustomReply, ::carbon::test::thrift::CustomReply>"}
3435
typedef CarbonThriftTest.CustomReply CarbonThriftTest_CustomReply
35-
@cpp.Type{name = "carbon::test::CustomRequest"}
36+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::CustomRequest, ::carbon::test::thrift::CustomRequest>"}
3637
typedef CarbonThriftTest.CustomRequest CarbonThriftTest_CustomRequest
37-
@cpp.Type{name = "carbon::test::DummyThriftReply"}
38+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::DummyThriftReply, ::carbon::test::thrift::DummyThriftReply>"}
3839
typedef CarbonThriftTest.DummyThriftReply CarbonThriftTest_DummyThriftReply
39-
@cpp.Type{name = "carbon::test::DummyThriftRequest"}
40+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::DummyThriftRequest, ::carbon::test::thrift::DummyThriftRequest>"}
4041
typedef CarbonThriftTest.DummyThriftRequest CarbonThriftTest_DummyThriftRequest
41-
@cpp.Type{name = "carbon::test::ThriftTestReply"}
42+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::ThriftTestReply, ::carbon::test::thrift::ThriftTestReply>"}
4243
typedef CarbonThriftTest.ThriftTestReply CarbonThriftTest_ThriftTestReply
43-
@cpp.Type{name = "carbon::test::ThriftTestRequest"}
44+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<carbon::test::ThriftTestRequest, ::carbon::test::thrift::ThriftTestRequest>"}
4445
typedef CarbonThriftTest.ThriftTestRequest CarbonThriftTest_ThriftTestRequest
45-
@cpp.Type{name = "facebook::memcache::McVersionReply"}
46+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionReply, ::facebook::memcache::thrift::McVersionReply>"}
4647
typedef Common.McVersionReply Common_McVersionReply
47-
@cpp.Type{name = "facebook::memcache::McVersionRequest"}
48-
typedef Common.McVersionRequest Common_McVersionRequest
48+
@cpp.Adapter{name = "::carbon::util::CarbonThriftAdapter<facebook::memcache::McVersionRequest, ::facebook::memcache::thrift::McVersionRequest>"}
49+
typedef Common.McVersionRequest Common_McVersionRequest

0 commit comments

Comments
 (0)