Skip to content

Commit 3ca5830

Browse files
thedavekwonfacebook-github-bot
authored andcommitted
Support Annotation for SyntaxGraph::Enum::Value
Summary: Note, Enum::Value::operatator== doesn't consider annotations Reviewed By: pranavtbhat Differential Revision: D78527413 fbshipit-source-id: 044136e2ad65c09a4bec0fcac90e2cb099b6b304
1 parent cc73835 commit 3ca5830

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

thrift/lib/cpp2/schema/SyntaxGraph.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,17 @@ class EnumNode final : folly::MoveOnly,
698698
* A mapping of enum name to its i32 value.
699699
*/
700700
// TODO: these can also have annotations
701-
class Value : detail::WithName {
701+
class Value : detail::WithName, detail::WithAnnotations {
702702
public:
703-
Value(std::string_view name, std::int32_t i32)
704-
: WithName(name), i32_(i32) {}
705-
703+
Value(
704+
std::string_view name,
705+
std::int32_t i32,
706+
std::vector<Annotation>&& annotations)
707+
: detail::WithName(name),
708+
detail::WithAnnotations(std::move(annotations)),
709+
i32_(i32) {}
710+
711+
using detail::WithAnnotations::annotations;
706712
using detail::WithName::name;
707713
/**
708714
* All enums values in Thrift have underlying type of i32

thrift/lib/cpp2/schema/detail/SchemaBackedResolver.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ class SchemaIndex {
151151
const type::Field&,
152152
const type::Schema&);
153153

154-
EnumNode createEnum(const type::DefinitionKey&, const type::Enum&);
154+
EnumNode createEnum(
155+
const type::DefinitionKey&, const type::Enum&, const type::Schema&);
155156
TypedefNode createTypedef(const type::DefinitionKey&, const type::Typedef&);
156157
ConstantNode createConstant(const type::DefinitionKey&, const type::Const&);
157158

@@ -508,7 +509,7 @@ void SchemaIndex::updateDefinitionsByKey(
508509
return createException(definitionKey, exceptionDef, schema);
509510
},
510511
[&](const type::Enum& enumDef) -> DefinitionNode::Alternative {
511-
return createEnum(definitionKey, enumDef);
512+
return createEnum(definitionKey, enumDef, schema);
512513
},
513514
[&](const type::Typedef& typedefDef) -> DefinitionNode::Alternative {
514515
return createTypedef(definitionKey, typedefDef);
@@ -625,10 +626,15 @@ FieldNode SchemaIndex::createField(
625626
}
626627

627628
EnumNode SchemaIndex::createEnum(
628-
const type::DefinitionKey& definitionKey, const type::Enum& enumDef) {
629+
const type::DefinitionKey& definitionKey,
630+
const type::Enum& enumDef,
631+
const type::Schema& schema) {
629632
std::vector<EnumNode::Value> values;
630633
for (const type::EnumValue& enumValue : *enumDef.values()) {
631-
values.emplace_back(EnumNode::Value(*enumValue.name(), *enumValue.value()));
634+
values.emplace_back(
635+
*enumValue.name(),
636+
*enumValue.value(),
637+
createAnnotations(*enumValue.annotationsByKey(), schema));
632638
}
633639
return EnumNode(resolver_, definitionKey, *enumDef.uri(), std::move(values));
634640
}

thrift/lib/cpp2/schema/test/SyntaxGraphTest.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,18 @@ TEST_F(ServiceSchemaTest, Enum) {
163163
EXPECT_EQ(e.definition().annotations().size(), 1);
164164
EXPECT_EQ(e.definition().annotations()[0].value()["field1"], 3);
165165

166-
const std::vector<EnumNode::Value> expected = {
167-
{"UNSET", 0},
168-
{"VALUE_1", 1},
169-
{"VALUE_2", 2},
170-
};
171-
EXPECT_THAT(e.values(), testing::ElementsAreArray(expected));
166+
const auto& unset = e.values()[0];
167+
const auto& value1 = e.values()[1];
168+
const auto& value2 = e.values()[2];
169+
170+
EXPECT_EQ(unset.name(), "UNSET");
171+
EXPECT_EQ(unset.i32(), 0);
172+
EXPECT_EQ(unset.annotations().size(), 1);
173+
EXPECT_EQ(unset.annotations()[0].value()["field1"], 4);
174+
EXPECT_EQ(value1.name(), "VALUE_1");
175+
EXPECT_EQ(value1.i32(), 1);
176+
EXPECT_EQ(value2.name(), "VALUE_2");
177+
EXPECT_EQ(value2.i32(), 2);
172178

173179
EXPECT_EQ(
174180
e.toDebugString(),

thrift/lib/cpp2/schema/test/syntax_graph.thrift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct TestRecursiveStruct {
2929
@scope.Definition
3030
@scope.Field
3131
@scope.Enum
32+
@scope.EnumValue
3233
struct TestStructuredAnnotation {
3334
1: i64 field1;
3435
2: TestInnerStructuredAnnotation field2;
@@ -37,6 +38,7 @@ struct TestStructuredAnnotation {
3738
@thrift.Uri{value = "meta.com/thrift_test/TestEnum"}
3839
@TestStructuredAnnotation{field1 = 3}
3940
enum TestEnum {
41+
@TestStructuredAnnotation{field1 = 4}
4042
UNSET = 0,
4143
VALUE_1 = 1,
4244
VALUE_2 = 2,

0 commit comments

Comments
 (0)