user1187135
user1187135

Reputation:

How to inject quasi quotes array

I have an array of quasi quotes called definitions which I want to inject to the quasi quote tree. How do I go about doing this?

private def generateDaoComponent(files: Array[File]) = {
    val file = createNewFile(compDirectory)

    val definitions = files.map(f => {
      val daoName = f.getName.replace(".java", "")
      val daoType = TypeName(daoName)
      val daoTerm = TermName(daoName)

      q"""def $daoTerm = getValueOrInstantiate($daoName, () => new $daoType(configuration))


       """
    })

    val tree = q"""
        package database.dao {
          import org.jooq.SQLDialect
          import org.jooq.impl.DefaultConfiguration
          import utility.StrongHashMap

          trait $componentType extends StrongHashMap[String, Dao] {
            this: DaoManager =>

            private lazy val configuration = new DefaultConfiguration().set(connection).set(SQLDialect.POSTGRES_9_4)

            ${definitions.foreach(f => q"${f}")}
          }
        }"""

    writeToFile(file, tree)
  }

Upvotes: 3

Views: 152

Answers (1)

user1187135
user1187135

Reputation:

This is some crazy late night coding, but I found it thanks to this website

3 approaches to Scala code generation

I noticed when it passed the $params array into the quasi quote it used two .. in front of the class constructor like this:

    val params = schema.fields.map { field =>
      val fieldName = newTermName(field.name)
      val fieldType = newTypeName(field.valueType.fullName)
      q"val $fieldName: $fieldType"
    }

    val json = TypeSchema.toJson(schema)

    // rewrite the class definition
    c.Expr(
      q"""
        case class $className(..$params) {

          def schema = ${json}

        }
      """
    )

There are two steps to the code I posted in the question to make this working.

1) Convert $definitions.toList to List

2) Add the two .. in front

So the final code looks like this:

    val definitions = files.map(f => {
      val daoName = f.getName.replace(".java", "")
      val daoType = TypeName(daoName)
      val daoTerm = TermName(new StringBuilder("get").append(daoName).toString())

      q"""def $daoTerm = getValueOrInstantiate($daoName, () => new $daoType(configuration))"""
    }).toList <--- HERE!

    val tree = q"""
        package database.dao {
          import org.jooq.SQLDialect
          import org.jooq.impl.DefaultConfiguration
          import utility.StrongHashMap

          trait $componentType extends StrongHashMap[String, Dao] {
            this: DaoManager =>

            private lazy val configuration = new DefaultConfiguration().set(connection).set(SQLDialect.POSTGRES_9_4)

            ..$definitions <-- AND HERE!

          }
        }"""

Upvotes: 1

Related Questions