Skip to content

Commit e679b6c

Browse files
authored
Add iter_origin() methods to schema elements (#978)
1 parent bb08b89 commit e679b6c

File tree

1 file changed

+117
-63
lines changed
  • crates/apollo-compiler/src/schema

1 file changed

+117
-63
lines changed

crates/apollo-compiler/src/schema/mod.rs

Lines changed: 117 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -658,29 +658,26 @@ impl SchemaDefinition {
658658
.filter_map(|(ty, maybe_op)| maybe_op.as_ref().map(|op| (ty, op)))
659659
}
660660

661-
/// Collect `schema` extensions that contribute any component
661+
/// Iterate over the `origins` of all components
662662
///
663663
/// The order of the returned set is unspecified but deterministic
664664
/// for a given apollo-compiler version.
665-
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
665+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
666666
self.directives
667667
.iter()
668-
.flat_map(|dir| dir.origin.extension_id())
669-
.chain(
670-
self.query
671-
.as_ref()
672-
.and_then(|name| name.origin.extension_id()),
673-
)
674-
.chain(
675-
self.mutation
676-
.as_ref()
677-
.and_then(|name| name.origin.extension_id()),
678-
)
679-
.chain(
680-
self.subscription
681-
.as_ref()
682-
.and_then(|name| name.origin.extension_id()),
683-
)
668+
.map(|dir| &dir.origin)
669+
.chain(self.query.iter().map(|name| &name.origin))
670+
.chain(self.mutation.iter().map(|name| &name.origin))
671+
.chain(self.subscription.iter().map(|name| &name.origin))
672+
}
673+
674+
/// Collect `schema` extensions that contribute any component
675+
///
676+
/// The order of the returned set is unspecified but deterministic
677+
/// for a given apollo-compiler version.
678+
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
679+
self.iter_origins()
680+
.filter_map(|origin| origin.extension_id())
684681
.collect()
685682
}
686683
}
@@ -857,128 +854,185 @@ impl ExtendedType {
857854
}
858855
}
859856

857+
/// Iterate over the `origins` of all components
858+
///
859+
/// The order of the returned set is unspecified but deterministic
860+
/// for a given apollo-compiler version.
861+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
862+
match self {
863+
Self::Scalar(ty) => Box::new(ty.iter_origins()) as Box<dyn Iterator<Item = _>>,
864+
Self::Object(ty) => Box::new(ty.iter_origins()),
865+
Self::Interface(ty) => Box::new(ty.iter_origins()),
866+
Self::Union(ty) => Box::new(ty.iter_origins()),
867+
Self::Enum(ty) => Box::new(ty.iter_origins()),
868+
Self::InputObject(ty) => Box::new(ty.iter_origins()),
869+
}
870+
}
871+
872+
/// Collect `schema` extensions that contribute any component
873+
///
874+
/// The order of the returned set is unspecified but deterministic
875+
/// for a given apollo-compiler version.
876+
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
877+
self.iter_origins()
878+
.filter_map(|origin| origin.extension_id())
879+
.collect()
880+
}
881+
860882
serialize_method!();
861883
}
862884

863885
impl ScalarType {
886+
/// Iterate over the `origins` of all components
887+
///
888+
/// The order of the returned set is unspecified but deterministic
889+
/// for a given apollo-compiler version.
890+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
891+
self.directives.iter().map(|dir| &dir.origin)
892+
}
893+
864894
/// Collect scalar type extensions that contribute any component
865895
///
866896
/// The order of the returned set is unspecified but deterministic
867897
/// for a given apollo-compiler version.
868898
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
869-
self.directives
870-
.iter()
871-
.flat_map(|dir| dir.origin.extension_id())
899+
self.iter_origins()
900+
.filter_map(|origin| origin.extension_id())
872901
.collect()
873902
}
874903

875904
serialize_method!();
876905
}
877906

878907
impl ObjectType {
879-
/// Collect object type extensions that contribute any component
908+
/// Iterate over the `origins` of all components
880909
///
881910
/// The order of the returned set is unspecified but deterministic
882911
/// for a given apollo-compiler version.
883-
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
912+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
884913
self.directives
885914
.iter()
886-
.flat_map(|dir| dir.origin.extension_id())
915+
.map(|dir| &dir.origin)
887916
.chain(
888917
self.implements_interfaces
889918
.iter()
890-
.flat_map(|component| component.origin.extension_id()),
891-
)
892-
.chain(
893-
self.fields
894-
.values()
895-
.flat_map(|field| field.origin.extension_id()),
919+
.map(|component| &component.origin),
896920
)
921+
.chain(self.fields.values().map(|field| &field.origin))
922+
}
923+
924+
/// Collect object type extensions that contribute any component
925+
///
926+
/// The order of the returned set is unspecified but deterministic
927+
/// for a given apollo-compiler version.
928+
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
929+
self.iter_origins()
930+
.filter_map(|origin| origin.extension_id())
897931
.collect()
898932
}
899933

900934
serialize_method!();
901935
}
902936

903937
impl InterfaceType {
904-
/// Collect interface type extensions that contribute any component
938+
/// Iterate over the `origins` of all components
905939
///
906940
/// The order of the returned set is unspecified but deterministic
907941
/// for a given apollo-compiler version.
908-
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
942+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
909943
self.directives
910944
.iter()
911-
.flat_map(|dir| dir.origin.extension_id())
945+
.map(|dir| &dir.origin)
912946
.chain(
913947
self.implements_interfaces
914948
.iter()
915-
.flat_map(|component| component.origin.extension_id()),
916-
)
917-
.chain(
918-
self.fields
919-
.values()
920-
.flat_map(|field| field.origin.extension_id()),
949+
.map(|component| &component.origin),
921950
)
951+
.chain(self.fields.values().map(|field| &field.origin))
952+
}
953+
954+
/// Collect interface type extensions that contribute any component
955+
///
956+
/// The order of the returned set is unspecified but deterministic
957+
/// for a given apollo-compiler version.
958+
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
959+
self.iter_origins()
960+
.filter_map(|origin| origin.extension_id())
922961
.collect()
923962
}
924963

925964
serialize_method!();
926965
}
927966

928967
impl UnionType {
929-
/// Collect union type extensions that contribute any component
968+
/// Iterate over the `origins` of all components
930969
///
931970
/// The order of the returned set is unspecified but deterministic
932971
/// for a given apollo-compiler version.
933-
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
972+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
934973
self.directives
935974
.iter()
936-
.flat_map(|dir| dir.origin.extension_id())
937-
.chain(
938-
self.members
939-
.iter()
940-
.flat_map(|component| component.origin.extension_id()),
941-
)
975+
.map(|dir| &dir.origin)
976+
.chain(self.members.iter().map(|component| &component.origin))
977+
}
978+
979+
/// Collect union type extensions that contribute any component
980+
///
981+
/// The order of the returned set is unspecified but deterministic
982+
/// for a given apollo-compiler version.
983+
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
984+
self.iter_origins()
985+
.filter_map(|origin| origin.extension_id())
942986
.collect()
943987
}
944988

945989
serialize_method!();
946990
}
947991

948992
impl EnumType {
949-
/// Collect enum type extensions that contribute any component
993+
/// Iterate over the `origins` of all components
950994
///
951995
/// The order of the returned set is unspecified but deterministic
952996
/// for a given apollo-compiler version.
953-
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
997+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
954998
self.directives
955999
.iter()
956-
.flat_map(|dir| dir.origin.extension_id())
957-
.chain(
958-
self.values
959-
.values()
960-
.flat_map(|value| value.origin.extension_id()),
961-
)
1000+
.map(|dir| &dir.origin)
1001+
.chain(self.values.values().map(|value| &value.origin))
1002+
}
1003+
1004+
/// Collect enum type extensions that contribute any component
1005+
///
1006+
/// The order of the returned set is unspecified but deterministic
1007+
/// for a given apollo-compiler version.
1008+
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
1009+
self.iter_origins()
1010+
.filter_map(|origin| origin.extension_id())
9621011
.collect()
9631012
}
9641013

9651014
serialize_method!();
9661015
}
9671016

9681017
impl InputObjectType {
969-
/// Collect input object type extensions that contribute any component
1018+
/// Iterate over the `origins` of all components
9701019
///
9711020
/// The order of the returned set is unspecified but deterministic
9721021
/// for a given apollo-compiler version.
973-
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
1022+
fn iter_origins(&self) -> impl Iterator<Item = &ComponentOrigin> {
9741023
self.directives
9751024
.iter()
976-
.flat_map(|dir| dir.origin.extension_id())
977-
.chain(
978-
self.fields
979-
.values()
980-
.flat_map(|field| field.origin.extension_id()),
981-
)
1025+
.map(|dir| &dir.origin)
1026+
.chain(self.fields.values().map(|field| &field.origin))
1027+
}
1028+
1029+
/// Collect input object type extensions that contribute any component
1030+
///
1031+
/// The order of the returned set is unspecified but deterministic
1032+
/// for a given apollo-compiler version.
1033+
pub fn extensions(&self) -> IndexSet<&ExtensionId> {
1034+
self.iter_origins()
1035+
.filter_map(|origin| origin.extension_id())
9821036
.collect()
9831037
}
9841038

0 commit comments

Comments
 (0)