-
Notifications
You must be signed in to change notification settings - Fork 80
Generic AST #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic AST #19
Conversation
Note: I also want to add |
Hi! I'm okay with the changes. The question is: are you sure that using The other options are:
My doubts are because of:
Have you considered other options? |
@tailhook I initially wanted to implement a generic solution but then thought it was too cumbersome. But I revisited the code and I think I've found a reasonably decent structure that enables the AST to be used pretty seamlessly with any type (that implements the new It does unfortunately produce a bit of noise, because a new wrapper type is required. (Called The important changes are all on top of This would give users the ability to tailor the AST to their use case, and it doesn't introduce too much awkwardness in the codebase. ps: The implementation does require one The PR would need some cleanup and documentation changes, just want to get your view first. |
Also, the |
At a glance looks promising. Just quick question so far: why Text trait has a lifetime? Usually, generic types like |
Without it we can't introduce the constraint that eg: |
Okay. Makes sense. I'm okay with merging it when you are ready. (Frankly, I'm still not sure if generics are good in the long run, but I think this is the best to make it to the next release and see how it will be used) |
@davidpdrsn @tomhoule since this will affect both of you, do you see any problems with these changes? |
I don't see any real downsides since it just increases the flexibility. I think users can just use a defined type like I don't see any hurry in merging though, I will try to port juniper over to this branch and see if I notice any stumbling blocks. Also I'll submit some PRs for the spanning infrastructure, since the juniper parser currently has quite a bit more spanning going on and we'll need that for error reporting. |
I might have found a solution that does not require a wrapper type. Still fighting some type checker issues, but looks promising: trait Text<'a> {
type Value: 'a + AsRef<str> + From<&'a str> + PartialEq + Eq;
}
impl<'a> Text<'a> for &'a str {
type Value = Self;
}
impl Text<'static> for String {
type Value = Self;
}
struct Node<'a, T: Text<'a>> {
name: T::Value,
}
fn compare<'a, T: Text<'a>>(a: T::Out, b: T::Out) -> bool {
a == b
}
fn main() {
let a = Node::<&str>{ value: "blub" };
let b = Node::<&str>{ value: "blub" };
dbg!(a.value == b.value);
} |
Looks like a good solution to me 👍 Am I correct that users could write the following // Will use owned `String`s
parse_schema::<String>(&schema)
// Will borrow things from `schema`
parse_schema::<str>(&schema) For the last one, wouldn't that require fn parse_schema<'a, T: ?Sized>(_: &'a str) {}
// ^^^^^^ To allow for `str` to be used I might also be totally misunderstanding things 😜 |
@davidpdrsn I already added a let ast = parse_query::<&str>("query MyQuery { field1, field2 }")?; The definition of parse query just had to change to (with the first implementation with a wrapper type): pub fn parse_query<'a, S>(s: &'a str) -> Result<Document<'a, S>, ParseError>
where S: Text<'a>, |
Great 👍 I'll implement this change in juniper-from-schema as soon as it is merged. |
Not ready for merge yet, but I've pushed the new generic version without a wrapper type. Tests are passing so looks good. |
Looks good for me. |
How are we doing here? |
This is blocking graphql-rust/juniper#324, anything I can do to help push it forward? |
Hey @theduke, is there anything we can do to help getting this merged? |
Sorry, I hadn't seen the mention in a previous comment. I think this change is good for graphql-client, we already try to do zero-copy processing of the schema when parsing from JSON. So no worry from my part. |
If I remember correctly it's ready. I'll browse over the code to be sure. |
This commit introduces a Text trait that is used for representing text data in the AST. This enables using the ast with &str references only, with Cow, or just as now in an owned fashion (with String). To prevent a wrapper type, the Text trait has an associated type 'Value' that represents the actual value and has to be used in the AST nodes.
@tailhook I fixed some deprecation warnings and checked the code. I think this should be good to merge if you are agreed. |
Merged. Thanks! |
I will probably do a follow-up PR that adds one or two submodules with type aliases for all types, so you don't have to specify the generic argument everywhere. |
Refactor all the AST types to use Cow<'a, str> instead of String.
This allows using the Ast both in a borrowed style,
and a owned style with Cow<'static, str>
This alleviates performance concerns of juniper in switching to
this parser instead of the custom one.