DeriveToCaseClass.scala 3.7 KB
Newer Older
梦境迷离's avatar
fix  
梦境迷离 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) 2022 bitlap
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

梦境迷离's avatar
fix  
梦境迷离 已提交
22
package org.bitlap.csv.core.macros
梦境迷离's avatar
add csv  
梦境迷离 已提交
23 24 25 26 27 28 29 30
import scala.reflect.macros.blackbox

/**
 * @author 梦境迷离
 * @version 1.0,2022/4/29
 */
object DeriveToCaseClass {

梦境迷离's avatar
fix  
梦境迷离 已提交
31
  def apply[T <: Product](line: String, columnSeparator: Char): Option[T] = macro Macro.macroImpl[T]
梦境迷离's avatar
add csv  
梦境迷离 已提交
32 33

  class Macro(override val c: blackbox.Context) extends AbstractMacroProcessor(c) {
梦境迷离's avatar
fix  
梦境迷离 已提交
34 35 36 37
    def macroImpl[T <: Product: c.WeakTypeTag](
      line:            c.Expr[String],
      columnSeparator: c.Expr[Char]
    ): c.Expr[Option[T]] = {
梦境迷离's avatar
add csv  
梦境迷离 已提交
38 39
      import c.universe._
      val clazzName = c.weakTypeOf[T].typeSymbol.name
梦境迷离's avatar
fix  
梦境迷离 已提交
40
      val fields = checkCaseClassReturnConstructorParams[T](line, columnSeparator).map { idxType =>
梦境迷离's avatar
fix  
梦境迷离 已提交
41 42
        if (idxType._2 <:< typeOf[Option[_]]) {
          val genericType = c.typecheck(q"${idxType._2}", c.TYPEmode).tpe.typeArgs.head
梦境迷离's avatar
fix  
梦境迷离 已提交
43
          q"Converter[${genericType.typeSymbol.name.toTypeName}].toScala(${idxType._1})"
梦境迷离's avatar
add csv  
梦境迷离 已提交
44
        } else {
梦境迷离's avatar
fix  
梦境迷离 已提交
45
          idxType._2 match {
梦境迷离's avatar
fix  
梦境迷离 已提交
46
            case t if t =:= typeOf[Int] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
47
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse(0)"
梦境迷离's avatar
fix  
梦境迷离 已提交
48
            case t if t =:= typeOf[String] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
49
              q"""Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse("")"""
梦境迷离's avatar
fix  
梦境迷离 已提交
50
            case t if t =:= typeOf[Float] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
51
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse(0F)"
梦境迷离's avatar
fix  
梦境迷离 已提交
52
            case t if t =:= typeOf[Double] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
53
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse(0D)"
梦境迷离's avatar
fix  
梦境迷离 已提交
54
            case t if t =:= typeOf[Char] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
55
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse('?')"
梦境迷离's avatar
fix  
梦境迷离 已提交
56
            case t if t =:= typeOf[Byte] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
57
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse(0)"
梦境迷离's avatar
fix  
梦境迷离 已提交
58
            case t if t =:= typeOf[Short] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
59
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse(0)"
梦境迷离's avatar
fix  
梦境迷离 已提交
60
            case t if t =:= typeOf[Boolean] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
61
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse(false)"
梦境迷离's avatar
fix  
梦境迷离 已提交
62
            case t if t =:= typeOf[Long] =>
梦境迷离's avatar
fix  
梦境迷离 已提交
63
              q"Converter[${TypeName(idxType._2.typeSymbol.name.decodedName.toString)}].toScala(${idxType._1}).getOrElse(0L)"
梦境迷离's avatar
add csv  
梦境迷离 已提交
64 65 66 67 68 69 70
          }
        }
      }
      val tree =
        q"""
       Option(${TermName(clazzName.decodedName.toString)}(..$fields))
     """
梦境迷离's avatar
fix  
梦境迷离 已提交
71
      printTree[T](force = true, tree)
梦境迷离's avatar
add csv  
梦境迷离 已提交
72 73 74 75 76

    }.asInstanceOf[c.Expr[Option[T]]]

  }
}