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

Some information on filter #18

Closed
tavurth opened this issue Nov 4, 2017 · 3 comments
Closed

Some information on filter #18

tavurth opened this issue Nov 4, 2017 · 3 comments

Comments

@tavurth
Copy link
Contributor

tavurth commented Nov 4, 2017

So I'm trying to write a simple filter to expand the changelog example so that it notes only changes which match our TestItem implementation, however I can't seem to get any filters working:

let query =
        r.db("test")
        .table("test")
        .filter(|doc| {
            doc.has_fields("test").and(doc.test.type_of("NUMBER"))
        })
        .changes()

Gives me:

error[E0619]: the type of this value must be known in this context
 |
 | .filter(|doc| { doc.has_fields("test").and(doc.test.type_of("NUMBER")) })
 |                 ^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `[closure@src/main.rs:55:17: 55:81]: reql::IntoArg` is not satisfied
 |
 |.filter(|doc| { doc.has_fields("test").and(doc.test.type_of("NUMBER")) })
 | ^^^^^^ the trait `reql::IntoArg` is not implemented for `[closure@src/main.rs:55:17: 55:81]`

Any advice on the filter implementation would be appreciated, I'm rather new to Rust at this point.

@rushmorem
Copy link
Member

rushmorem commented Nov 4, 2017

ReQL closures can take multiple arguments. We can't reasonably implement all the possibilities using Rust's type system. Combined with the fact that ReQL has so many commands, I decided to generate the commands automatically straight from the ReQL documentation. To make this possible, I made the following design choices:-

  • Any command which takes zero or more arguments is generated as taking no arguments. If you need to pass in the optional arguments use the with_args method.
  • Any command which takes at least one argument is generated as taking just 1 argument. Use the args macro to pass in multiple arguments.

The argument that the command takes must implement reql::IntoArg. As you can see from the second error message, we don't implement it for closures as this is not practical. Instead we use the args macro to pass in closures. So try this instead:-

let query =
        r.db("test")
        .table("test")
        .filter(args!(|doc| {
            doc.has_fields("test").and(doc.get_field("test").type_of().eq("NUMBER"))
        }))
        .changes()

@tavurth
Copy link
Contributor Author

tavurth commented Nov 4, 2017

I see, that makes a lot more sense now, thank you!

I've updated the example code to reflect the filtered change.

@rushmorem
Copy link
Member

I see, that makes a lot more sense now, thank you!

My pleasure :)

I've updated the example code to reflect the filtered change.

Awesome! Let me take a look. Thanks.

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

No branches or pull requests

2 participants