Skip to content
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

Impl check descriptor for enum #288

Merged
merged 4 commits into from
Dec 11, 2023
Merged

Conversation

Dhghomon
Copy link
Contributor

@Dhghomon Dhghomon commented Dec 8, 2023

Was surprised today to see that a simple query like the following doesn't work:

Schema: module default { scalar type Country extending enum<Full, ReadOnly, None>; }

Rust:

fn main() {
let client = edgedb_tokio::create_client()
        .await
        .unwrap();
    let val: Value = client
        .query_required_single(
            "select <Country>$0",
            &(Value::Enum(EnumValue::from("None")),),
        )
        .await
        .unwrap();
}

The error is that it expected an enum, and got an...Enumeration.

called `Result::unwrap()` on an `Err` value: Error(Inner { code: 4278386176, messages: ["\nEdgeDB returned unexpected type Enumeration(EnumerationTypeDescriptor { id: 16e1e80c-94fe-11ee-96e2-dd6c21ea77c9, members: [\"Full\", \"ReadOnly\", \"None\"] })\nClient expected enum"], error: None, headers: {}, fields: {("descriptor", TypeId { t: 116367074156074466779343831034874852404 }): Any { .. }, ("capabilities", TypeId { t: 264000494568981768888535343960226937934 }): Any { .. }} })

Turns out that it's just that check_descriptor() simply isn't complete yet. A check to make sure that the value is found inside the members (which holds the possible enum values) should do the trick while the cast in the query itself (in this case a <Country>).

After that the error when an incorrect enum value was passed in was still the above, so changed it to output an InvalidReferenceError (that's what the REPL gives in the same case) with some context so now it looks like this:

called `Result::unwrap()` on an `Err` value: Error(Inner { code: 67305472, messages: ["Expected one of: 'Full', 'ReadOnly', 'None', while enum value 'RRRRRRRRRRRRRRRRRReadOnly' was provided"], error: None, headers: {}, fields: {("descriptor", TypeId { t: 254184924864600833047165177448722979326 }): Any { .. }, ("capabilities", TypeId { t: 267257778208590603416093306289546576982 }): Any { .. }} })

@Dhghomon Dhghomon requested review from elprans and fantix December 8, 2023 05:41
@Dhghomon Dhghomon marked this pull request as ready for review December 8, 2023 05:41
Copy link
Member

@fantix fantix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines 226 to 234
if members.iter().any(|c| c == val.deref()) {
Ok(())
} else {
let members = members
.into_iter()
.map(|c| format!("'{c}'"))
.collect::<Vec<_>>()
.join(", ");
let val = val.to_string();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both val.deref() and val.to_string() can be replaced by a local variable let val = val.deref().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, looks much nicer!

@Dhghomon Dhghomon merged commit 60309cf into master Dec 11, 2023
3 checks passed
@Dhghomon Dhghomon deleted the impl-check-descriptor-for-enum branch December 11, 2023 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants