zio / zio-quill

Compile-time Language Integrated Queries for Scala
https://zio.dev/zio-quill
Apache License 2.0
2.15k stars 348 forks source link

How to can I implement custom NamingStrategy working at compile time? #1100

Closed pismute closed 6 years ago

pismute commented 6 years ago

How can I implement custom NamingStrategy working at compile time.

Version: 0.4.3-SNAPSHOT Module: quill-async-postgres Database: postgres

I created a custom NamingStrategy because postgres-async doesn't support current schema concept, which is used by quill-async-postgres:

trait MySchema extends NamingStrategy {
  override def table(s: String): String = s"my.$s"
  override def default(s: String): String = s
}
object MySchema extends MySchema

It works at runtime only, not at compile time.

And I have put the NamingStrategy into quill and built my quill version. It works at compile time as I expected.

How can I use my custom NamingStrategy as static?

jilen commented 6 years ago

Try define it inside another sub project

pismute commented 6 years ago

@jilen Thank you so much.

It's just 4 lines. I don't want to create another sub product. Is there another option?

I think io.getquill.util.LoadObject can't load MySchema object. It seems Class.forName("xxx.MySchema$") statement is failing:

package io.getquill.util

import scala.reflect.macros.whitebox.Context
import scala.util.Try

object LoadObject {

  def apply[T](c: Context)(tpe: c.Type): Try[T] =
    Try {
      val cls = Class.forName(tpe.typeSymbol.fullName + "$")
      val field = cls.getField("MODULE$")
      field.get(cls).asInstanceOf[T]
    }
}

BTW, How can I debug Scala macros?

jilen commented 6 years ago

@pismute The current LoadObject implementation requires that. Otherwise the custom NamingStrategy might not available to be loaded.

Because Class.forName requires the target class available on classpath

pismute commented 6 years ago

Thank you a lot