Skip to content

Commit 0b53da7

Browse files
committed
Temp
1 parent f0b5c3a commit 0b53da7

33 files changed

+1344
-395
lines changed

crates/apollo-smith/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ arbitrary = { version = "1.3.0", features = ["derive"] }
2828
indexmap = "2.0.0"
2929
once_cell = "1.9.0"
3030
thiserror = "1.0.37"
31+
paste = "1.0.0"
32+
uuid = { version = "1.7.0", features = ["v4"] }
3133

3234
[dev-dependencies]
3335
expect-test = "1.4"

crates/apollo-smith/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) mod input_object;
1111
pub(crate) mod input_value;
1212
pub(crate) mod interface;
1313
pub(crate) mod name;
14-
mod next;
14+
pub mod next;
1515
pub(crate) mod object;
1616
pub(crate) mod operation;
1717
pub(crate) mod scalar;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use apollo_compiler::ast::{Definition, InputObjectTypeDefinition, Name, Type};
2+
use apollo_compiler::schema::ExtendedType;
3+
use arbitrary::Unstructured;
4+
5+
pub(crate) trait DefinitionExt {
6+
fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type>;
7+
}
8+
9+
impl DefinitionExt for Definition {
10+
fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type> {
11+
let name = self.name().expect("definition must have a name").clone();
12+
Ok(ty(u, name)?)
13+
}
14+
}
15+
16+
impl DefinitionExt for InputObjectTypeDefinition {
17+
fn ty(&self, u: &mut Unstructured) -> arbitrary::Result<Type> {
18+
Ok(ty(u, self.name.clone())?)
19+
}
20+
}
21+
22+
fn ty(u: &mut Unstructured, name: Name) -> arbitrary::Result<Type> {
23+
let mut ty = if u.arbitrary()? {
24+
Type::Named(name)
25+
} else {
26+
Type::NonNullNamed(name)
27+
};
28+
29+
for _ in 0..u.int_in_range(0..=5)? {
30+
if u.arbitrary()? {
31+
ty = Type::List(Box::new(ty))
32+
} else {
33+
ty = Type::NonNullList(Box::new(ty))
34+
};
35+
}
36+
Ok(ty)
37+
}
38+
39+
#[derive(Debug)]
40+
pub(crate) enum DefinitionKind {
41+
OperationDefinition,
42+
FragmentDefinition,
43+
DirectiveDefinition,
44+
SchemaDefinition,
45+
ScalarTypeDefinition,
46+
ObjectTypeDefinition,
47+
InterfaceTypeDefinition,
48+
UnionTypeDefinition,
49+
EnumTypeDefinition,
50+
InputObjectTypeDefinition,
51+
SchemaExtension,
52+
ScalarTypeExtension,
53+
ObjectTypeExtension,
54+
InterfaceTypeExtension,
55+
UnionTypeExtension,
56+
EnumTypeExtension,
57+
InputObjectTypeExtension,
58+
}
59+
60+
impl DefinitionKind {
61+
pub(crate) fn matches(&self, definition: &Definition) -> bool {
62+
match (self, definition) {
63+
(Self::OperationDefinition, Definition::OperationDefinition(_)) => true,
64+
(Self::FragmentDefinition, Definition::FragmentDefinition(_)) => true,
65+
(Self::DirectiveDefinition, Definition::DirectiveDefinition(_)) => true,
66+
(Self::SchemaDefinition, Definition::SchemaDefinition(_)) => true,
67+
(Self::ScalarTypeDefinition, Definition::ScalarTypeDefinition(_)) => true,
68+
(Self::ObjectTypeDefinition, Definition::ObjectTypeDefinition(_)) => true,
69+
(Self::InterfaceTypeDefinition, Definition::InterfaceTypeDefinition(_)) => true,
70+
(Self::UnionTypeDefinition, Definition::UnionTypeDefinition(_)) => true,
71+
(Self::EnumTypeDefinition, Definition::EnumTypeDefinition(_)) => true,
72+
(Self::InputObjectTypeDefinition, Definition::InputObjectTypeDefinition(_)) => true,
73+
(Self::SchemaExtension, Definition::SchemaExtension(_)) => true,
74+
(Self::ScalarTypeExtension, Definition::ScalarTypeExtension(_)) => true,
75+
(Self::ObjectTypeExtension, Definition::ObjectTypeExtension(_)) => true,
76+
(Self::InterfaceTypeExtension, Definition::InterfaceTypeExtension(_)) => true,
77+
(Self::UnionTypeExtension, Definition::UnionTypeExtension(_)) => true,
78+
(Self::EnumTypeExtension, Definition::EnumTypeExtension(_)) => true,
79+
(Self::InputObjectTypeExtension, Definition::InputObjectTypeExtension(_)) => true,
80+
_ => false,
81+
}
82+
}
83+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use super::document::DocumentExt;
2+
use apollo_compiler::ast::{
3+
Argument, Directive, DirectiveDefinition, DirectiveList, DirectiveLocation, Document,
4+
};
5+
use apollo_compiler::{Node, Schema};
6+
use arbitrary::Unstructured;
7+
use std::ops::Deref;
8+
9+
pub(crate) struct LocationFilter<I>(I, DirectiveLocation);
10+
11+
impl<'a, T> Iterator for LocationFilter<T>
12+
where
13+
T: Iterator<Item = &'a Node<DirectiveDefinition>>,
14+
{
15+
type Item = &'a Node<DirectiveDefinition>;
16+
17+
fn next(&mut self) -> Option<Self::Item> {
18+
self.0.find(|d| d.locations.contains(&self.1))
19+
}
20+
}
21+
22+
pub(crate) trait DirectiveDefinitionIterExt {
23+
fn with_location<'a>(self, location: DirectiveLocation) -> LocationFilter<Self>
24+
where
25+
Self: Iterator<Item = &'a Node<DirectiveDefinition>> + Sized;
26+
27+
fn try_collect<'a>(
28+
self,
29+
u: &mut Unstructured,
30+
doc: &Document,
31+
schema: &Schema,
32+
) -> arbitrary::Result<DirectiveList>
33+
where
34+
Self: Iterator<Item = &'a Node<DirectiveDefinition>> + Sized;
35+
}
36+
37+
impl<I: ?Sized> DirectiveDefinitionIterExt for I {
38+
fn with_location<'a>(self, location: DirectiveLocation) -> LocationFilter<Self>
39+
where
40+
I: Iterator<Item = &'a Node<DirectiveDefinition>>,
41+
Self: Sized,
42+
{
43+
LocationFilter(self, location)
44+
}
45+
46+
fn try_collect<'a>(
47+
mut self,
48+
u: &mut Unstructured,
49+
doc: &Document,
50+
schema: &Schema,
51+
) -> arbitrary::Result<DirectiveList>
52+
where
53+
Self: Iterator<Item = &'a Node<DirectiveDefinition>> + Sized,
54+
{
55+
let mut directives = DirectiveList::new();
56+
while let Some(d) = self.next() {
57+
let mut arguments = Vec::new();
58+
for arg in &d.arguments {
59+
if arg.is_required() || u.arbitrary()? {
60+
arguments.push(Node::new(Argument {
61+
name: arg.name.clone(),
62+
value: Node::new(doc.arbitrary_value(u, arg.ty.deref(), schema)?),
63+
}))
64+
}
65+
}
66+
67+
directives.push(Node::new(Directive {
68+
name: d.name.clone(),
69+
arguments,
70+
}))
71+
}
72+
Ok(directives)
73+
}
74+
}

0 commit comments

Comments
 (0)