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

Deriving types for QueryResult Records (and the Option of) #34

Open
tobyweston opened this issue Jun 15, 2018 · 2 comments
Open

Deriving types for QueryResult Records (and the Option of) #34

tobyweston opened this issue Jun 15, 2018 · 2 comments

Comments

@tobyweston
Copy link

tobyweston commented Jun 15, 2018

As the query results are Any, how do you suggest deriving types for the values?

For example:

val temperature: Double = record("temperature").asInstanceOf[BigDecimal].toDouble

but that seems nasty. Do you retain the type from the JSON wire format? Any fancy implicit encoders available?

Similarly, how do you advice handling values which may be not there, e.g.

val maybeTemperature: Option[Double] = record("maybeThere") ...

Would be nice, but as it stands I can get NoSuchElementException when I request something that isn't there.

I had a poke around but couldn't find anything obvious.

Thanks in advance

@tobyweston tobyweston changed the title Deriving types for QueryResult Deriving types for QueryResult Records (and the Option of) Jun 15, 2018
@paulgoldbaum
Copy link
Owner

@tobyweston, in the first case I'm not sure what a good solution could look like, as these types can be different for each row. Maybe we could add type parameters to the query for when you know the types in advance and just fail the query if one row doesn't match. What do you think?

And yes, records should return Options instead of throwing an exception. Right now you can only surround it with a try-catch.

@tobyweston
Copy link
Author

Yeah, that's the best I can think of, something like:

val temperature: Double = record("temperature")

Where Double should be enough for an implicit conversion method to kick in?

def toDouble(any: Record): Double = any.asInstance[BigDecimal].toDouble // with some error handling

or maybe

def toDouble(any: Record): Option[Double] = {
    Try(any.asInstance[BigDecimal].toDouble).toOption
}

to handle the error and absence of the value at the same time?

I don't know if there are other constraints within the codebase that make implicits not a good option but from a client API perspective, i'd certainly prefer not to have to care how types are pulled back. I haven't tried it yet so hypothetical ATM 👍

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