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

Support for common Scala types #216

Closed
dominikschmid opened this Issue Aug 19, 2012 · 9 comments

Comments

Projects
None yet
2 participants
@dominikschmid

dominikschmid commented Aug 19, 2012

I've written a small library to map common Scala types (e.g. Option, Enum, BigDecimal) with Hibernate user types. Now I'd like to use these types also with Querydsl. However the generated query models won't compile and there are some other issues:

@Entity
class SomeEntity extends SomeTrait {
  // Mapped implicitly via user type
  var someStringOption: Option[String] = _

  // Mapped implicitly via user type
  var someScalaEnum: SomeScalaEnum.Value = _

  // Normal Scala char. Mapped correctly out of the box.
  var someChar: Char = _
}

trait SomeTrait {
  // Normal Scala String. Mapped correctly out of the box.
  @Column(name = "somePrivateString")
  private var somePrivateString: String = _
}

Generated by Querydsl:

class QSomeEntity(cl: Class[_ <: SomeEntity], md: PathMetadata[_]) extends EntityPathImpl[SomeEntity](cl, md) {
  // ...
  val someStringOption = createSimple[scala.Option]("someStringOption")
  val someScalaEnum = createComparable[scala.Enumeration.Value]("someScalaEnum")
  val someChar = createComparable[Char]("someChar")
  val com$example$SomeTrait$$somePrivateString = createString("com$example$SomeTrait$$somePrivateString")
}

Compiler errors:

class Option takes type parameters 
  val someStringOption = createSimple[scala.Option]("someStringOption")

object Enumeration is not a member of package scala 
Note: class Enumeration exists, but it has no companion object. 
  val someScalaEnum = createComparable[scala.Enumeration.Value]("someScalaEnum")

type arguments [Char] do not conform to method createComparable's type parameter bounds [T <: java.lang.Comparable[_]]
  val someChar = createComparable[Char]("someChar")

Note how the private String that is mixed in with a trait ends up getting the weird fully qualified property "com$example$SomeTrait$$somePrivateString"

Any plans to support this or any pointers how I could try to implement this myself?

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Aug 19, 2012

Member

Just fixed it :

object QSomeEntity extends QSomeEntity("someEntity") {
  override def as(variable: String) = new QSomeEntity(variable)  
}

class QSomeEntity(cl: Class[_ <: SomeEntity], md: PathMetadata[_]) extends EntityPathImpl[SomeEntity](cl, md) {
  def this(variable: String) = this(classOf[SomeEntity], forVariable(variable))
  def this(parent: Path[_], variable: String) = this(classOf[SomeEntity], forProperty(parent, variable))
  val somePrivateString = createString("somePrivateString")
  val someChar = createComparable[Char]("someChar")
  val someScalaEnum = createComparable[scala.Enumeration.Value]("someScalaEnum")
  val someStringOption = createSimple[scala.Option[String]]("someStringOption")
}
Member

timowest commented Aug 19, 2012

Just fixed it :

object QSomeEntity extends QSomeEntity("someEntity") {
  override def as(variable: String) = new QSomeEntity(variable)  
}

class QSomeEntity(cl: Class[_ <: SomeEntity], md: PathMetadata[_]) extends EntityPathImpl[SomeEntity](cl, md) {
  def this(variable: String) = this(classOf[SomeEntity], forVariable(variable))
  def this(parent: Path[_], variable: String) = this(classOf[SomeEntity], forProperty(parent, variable))
  val somePrivateString = createString("somePrivateString")
  val someChar = createComparable[Char]("someChar")
  val someScalaEnum = createComparable[scala.Enumeration.Value]("someScalaEnum")
  val someStringOption = createSimple[scala.Option[String]]("someStringOption")
}
@dominikschmid

This comment has been minimized.

Show comment
Hide comment
@dominikschmid

dominikschmid Aug 19, 2012

Great! Thanks, that was quick.

dominikschmid commented Aug 19, 2012

Great! Thanks, that was quick.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Sep 10, 2012

Member

Released in 2.7.3

Member

timowest commented Sep 10, 2012

Released in 2.7.3

@timowest timowest closed this Sep 10, 2012

@dominikschmid

This comment has been minimized.

Show comment
Hide comment
@dominikschmid

dominikschmid Sep 10, 2012

Thanks, I just noticed there is still a problem with Char and Enumeration. I think the generated code should be as follows:

import scala.runtime.RichChar
import com.example.package.containing.some.scala.enum.object

class QSomeEntity(cl: Class[_ <: SomeEntity], md: PathMetadata[_]) extends EntityPathImpl[SomeEntity](cl, md) {
  // ...

  // RichChar implements Comparable and there's an implicit conversion from Char to RichChar
  val someChar = createComparable[RichChar]("someChar")

  val someScalaEnum = createComparable[SomeScalaEnum.Value]("someScalaEnum")
}

This will compile properly.

dominikschmid commented Sep 10, 2012

Thanks, I just noticed there is still a problem with Char and Enumeration. I think the generated code should be as follows:

import scala.runtime.RichChar
import com.example.package.containing.some.scala.enum.object

class QSomeEntity(cl: Class[_ <: SomeEntity], md: PathMetadata[_]) extends EntityPathImpl[SomeEntity](cl, md) {
  // ...

  // RichChar implements Comparable and there's an implicit conversion from Char to RichChar
  val someChar = createComparable[RichChar]("someChar")

  val someScalaEnum = createComparable[SomeScalaEnum.Value]("someScalaEnum")
}

This will compile properly.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Sep 11, 2012

Member

RichChar extends Number, so it's actually correct that it's rendered as a number. You won't miss anything in the Querydsl API in the current form.

I think if you use the Enumeration.Value class directly, then the rendered type is also correct, if you subclass it, you will get a custom class. Correct me if I am wrong.

Member

timowest commented Sep 11, 2012

RichChar extends Number, so it's actually correct that it's rendered as a number. You won't miss anything in the Querydsl API in the current form.

I think if you use the Enumeration.Value class directly, then the rendered type is also correct, if you subclass it, you will get a custom class. Correct me if I am wrong.

@dominikschmid

This comment has been minimized.

Show comment
Hide comment
@dominikschmid

dominikschmid Sep 13, 2012

The problem is the generated code of QSomeEntity still won't compile:

type arguments [Char] do not conform to method createComparable's type parameter bounds [T <: java.lang.Comparable[_]]
  val someChar = createComparable[Char]("someChar")

object Enumeration is not a member of package scala 
Note: class Enumeration exists, but it has no companion object. 
  val someScalaEnum = createComparable[scala.Enumeration.Value]("someScalaEnum")

dominikschmid commented Sep 13, 2012

The problem is the generated code of QSomeEntity still won't compile:

type arguments [Char] do not conform to method createComparable's type parameter bounds [T <: java.lang.Comparable[_]]
  val someChar = createComparable[Char]("someChar")

object Enumeration is not a member of package scala 
Note: class Enumeration exists, but it has no companion object. 
  val someScalaEnum = createComparable[scala.Enumeration.Value]("someScalaEnum")

@timowest timowest reopened this Sep 14, 2012

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Sep 14, 2012

Member

fixed those as well now

Member

timowest commented Sep 14, 2012

fixed those as well now

@dominikschmid

This comment has been minimized.

Show comment
Hide comment
@dominikschmid

dominikschmid Sep 14, 2012

Thanks, if there's a snapshot somewhere I'd be happy to help test it. The snapshot repository doesn't seem to be up to date.

dominikschmid commented Sep 14, 2012

Thanks, if there's a snapshot somewhere I'd be happy to help test it. The snapshot repository doesn't seem to be up to date.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Sep 29, 2012

Member

Released in 2.8.0

Member

timowest commented Sep 29, 2012

Released in 2.8.0

@timowest timowest closed this Sep 29, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment