@@ -699,6 +699,106 @@ void SyntaxGraph::printTo(
699
699
}
700
700
701
701
namespace {
702
+
703
+ type_system::SerializableRecord toTypeSystemAnnotation (
704
+ const TypeRef& annotationType, const folly::dynamic& data);
705
+
706
+ type_system::SerializableRecord toTypeSystemAnnotation (
707
+ const StructuredNode& node, const folly::dynamic& data) {
708
+ type_system::SerializableRecord::FieldSet result;
709
+ for (const auto & [fieldName, fieldData] : data.items ()) {
710
+ const FieldNode& field = node.at (fieldName.asString ());
711
+ result.emplace (field.id (), toTypeSystemAnnotation (field.type (), fieldData));
712
+ }
713
+ return type_system::SerializableRecord (std::move (result));
714
+ }
715
+
716
+ type_system::SerializableRecord toTypeSystemAnnotation (
717
+ const TypeRef& annotationType, const folly::dynamic& data) {
718
+ return annotationType.visit (
719
+ [&](const StructuredNode& node) {
720
+ return toTypeSystemAnnotation (node, data);
721
+ },
722
+ [&](const EnumNode&) {
723
+ auto result = type_system::SerializableRecord::Int32 (
724
+ static_cast <int32_t >(data.asInt ()));
725
+ return type_system::SerializableRecord{std::move (result)};
726
+ },
727
+ [&](const TypedefNode& node) {
728
+ return toTypeSystemAnnotation (node.targetType (), data);
729
+ },
730
+ [&](const List& node) {
731
+ type_system::SerializableRecord::List l;
732
+ l.reserve (data.size ());
733
+ for (const auto & element : data) {
734
+ l.push_back (toTypeSystemAnnotation (node.elementType (), element));
735
+ }
736
+ return type_system::SerializableRecord{std::move (l)};
737
+ },
738
+ [&](const Set& node) {
739
+ type_system::SerializableRecord::Set s;
740
+ s.reserve (data.size ());
741
+ for (const auto & element : data) {
742
+ s.insert (toTypeSystemAnnotation (node.elementType (), element));
743
+ }
744
+ return type_system::SerializableRecord{std::move (s)};
745
+ },
746
+ [&](const Map& node) {
747
+ type_system::SerializableRecord::Map m;
748
+ m.reserve (data.size ());
749
+ for (const auto & [key, value] : data.items ()) {
750
+ m.emplace (
751
+ toTypeSystemAnnotation (node.keyType (), key),
752
+ toTypeSystemAnnotation (node.valueType (), value));
753
+ }
754
+ return type_system::SerializableRecord{std::move (m)};
755
+ },
756
+ [&](const Primitive& node) -> type_system::SerializableRecord {
757
+ switch (node) {
758
+ case Primitive::BOOL:
759
+ return {type_system::SerializableRecord::Bool (data.asBool ())};
760
+ case Primitive::BYTE:
761
+ return {type_system::SerializableRecord::Int8 (
762
+ static_cast <int8_t >(data.asInt ()))};
763
+ case Primitive::I16:
764
+ return {type_system::SerializableRecord::Int16 (
765
+ static_cast <int16_t >(data.asInt ()))};
766
+ case Primitive::I32:
767
+ return {type_system::SerializableRecord::Int32 (
768
+ static_cast <int32_t >(data.asInt ()))};
769
+ case Primitive::I64:
770
+ return {type_system::SerializableRecord::Int64 (
771
+ static_cast <int8_t >(data.asInt ()))};
772
+ case Primitive::DOUBLE:
773
+ return {type_system::SerializableRecord::Float32 (
774
+ static_cast <float >(data.asDouble ()))};
775
+ case Primitive::FLOAT:
776
+ return {type_system::SerializableRecord::Float64 (data.asDouble ())};
777
+ break ;
778
+ case Primitive::STRING:
779
+ return {type_system::SerializableRecord::Text (data.asString ())};
780
+ case Primitive::BINARY:
781
+ return {type_system::SerializableRecord::ByteArray (
782
+ folly::IOBuf::fromString (
783
+ std::make_unique<std::string>(data.asString ())))};
784
+ }
785
+ folly::assume_unreachable ();
786
+ });
787
+ }
788
+
789
+ type_system::AnnotationsMap toTypeSystemAnnotations (
790
+ folly::span<const Annotation> annotations) {
791
+ type_system::AnnotationsMap annotationsMap;
792
+ annotationsMap.reserve (annotations.size ());
793
+ // TODO(dokwon): only preserve annotations with @thrift.RuntimeAnnotation
794
+ for (const Annotation& annotation : annotations) {
795
+ annotationsMap.emplace (
796
+ annotation.type ().asStruct ().uri (),
797
+ toTypeSystemAnnotation (annotation.type (), annotation.value ()));
798
+ }
799
+ return annotationsMap;
800
+ }
801
+
702
802
class TypeSystemFacade final : public type_system::TypeSystem {
703
803
// Thrift files (and therefore SyntaxGraph) cannot define opaque alias types.
704
804
// Therefore, they are not necessary for the TypeSystem for SyntaxGraph.
@@ -845,17 +945,17 @@ class TypeSystemFacade final : public type_system::TypeSystem {
845
945
std::vector<type_system::EnumNode::Value> values;
846
946
values.reserve (e.values ().size ());
847
947
for (const auto & value : e.values ()) {
848
- // TODO: annotations
849
948
values.emplace_back (type_system::EnumNode::Value{
850
949
std::string (value.name ()),
851
950
value.i32 (),
852
- type_system::AnnotationsMap{} });
951
+ toTypeSystemAnnotations (value. annotations ()) });
853
952
}
854
- // TODO: annotations
855
953
auto [entry, _] = cache_.emplace (
856
954
sgDef,
857
955
type_system::EnumNode{
858
- type_system::Uri (e.uri ()), std::move (values), {}});
956
+ type_system::Uri (e.uri ()),
957
+ std::move (values),
958
+ toTypeSystemAnnotations (e.definition ().annotations ())});
859
959
reverseCache_.emplace (
860
960
&std::get<type_system::EnumNode>(entry->second ), sgDef);
861
961
},
@@ -891,10 +991,7 @@ class TypeSystemFacade final : public type_system::TypeSystem {
891
991
convertType (field.type ()),
892
992
// TODO: default value
893
993
std::nullopt,
894
- // TODO: annotations
895
- type_system::AnnotationsMap{}
896
-
897
- );
994
+ toTypeSystemAnnotations (field.annotations ()));
898
995
}
899
996
return fields;
900
997
};
@@ -904,14 +1001,14 @@ class TypeSystemFacade final : public type_system::TypeSystem {
904
1001
type_system::Uri (s.uri ()),
905
1002
makeFields (s),
906
1003
false ,
907
- type_system::AnnotationsMap{} };
1004
+ toTypeSystemAnnotations (s. definition (). annotations ()) };
908
1005
},
909
1006
[&](const UnionNode& s) {
910
1007
std::get<type_system::UnionNode>(tsDef) = type_system::UnionNode{
911
1008
type_system::Uri (s.uri ()),
912
1009
makeFields (s),
913
1010
false ,
914
- type_system::AnnotationsMap{} };
1011
+ toTypeSystemAnnotations (s. definition (). annotations ()) };
915
1012
},
916
1013
[](const auto & n) {
917
1014
folly::throw_exception<std::logic_error>(fmt::format (
0 commit comments