From 12fe81373c12b4aed4fb5202e69bd51aa65fa4d9 Mon Sep 17 00:00:00 2001 From: Sigaeva Marina Vadimovna Date: Mon, 10 Apr 2017 12:28:52 +0300 Subject: [PATCH] Make wrappers more concize --- .../converters/BinWrapper.scala | 262 +++++++----------- .../converters/KeyWrapper.scala | 20 +- .../aerospikemacro/converters/Utils.scala | 103 ++++++- src/main/resources/reference.conf | 2 +- .../ru/tinkoff/aerospike/dsl/Spike.scala | 6 +- .../ru/tinkoff/aerospike/dsl/SpikeImpl.scala | 6 +- .../dsl/batchread/BatchReadWrapper.scala | 2 + .../converters/BatchReadWrapperTest.scala | 2 + 8 files changed, 206 insertions(+), 197 deletions(-) diff --git a/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/BinWrapper.scala b/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/BinWrapper.scala index 4aa1fc7..a1c8a85 100644 --- a/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/BinWrapper.scala +++ b/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/BinWrapper.scala @@ -16,12 +16,11 @@ package ru.tinkoff.aerospikemacro.converters -import com.aerospike.client.Value -import com.aerospike.client.Value.{FloatValue, ListValue, MapValue, StringValue} import ru.tinkoff.aerospikescala.domain.{ByteSegment, MBin, SingleBin} import scala.language.experimental.macros import scala.reflect.macros.blackbox +import Utils._ /** @@ -36,8 +35,7 @@ trait BinWrapper[BT] { import shapeless.{HList, _} import scala.collection.JavaConversions._ - import scala.collection.immutable.{List, ListMap, Map} - import scala.reflect.ClassTag + import scala.collection.immutable.{List, Map} import scala.reflect.runtime.universe._ import scala.util.{Failure, Success} @@ -92,41 +90,6 @@ trait BinWrapper[BT] { } } - def typed[T](x: T)(implicit tag: WeakTypeTag[T]): T = x.asInstanceOf[T] - - def tupleMapped[TPL <: Any with Product](tpl: TPL): Map[String, Any] = { - val i = tpl.productArity - val m = optTuple(tpl).map(mapify(i, _)).getOrElse(Map.empty) - ListMap(m.toSeq.sortBy(_._1): _*).view.map { - case (k, v) => k.toString -> v - }.toMap - } - - def mapify[H <: Any with Product](i: Int, t: H): Map[Int, Any] = { - (for (e <- 0 until i) yield (e, t.productElement(e))) (collection.breakOut) - } - - def fromHList[L <: HList](hList: L, i: Int, maxIndex: Int): Map[String, Any] = { - val h0 = Map(i.toString -> typed(hList.productElement(0))) - hList match { - case head :: tail if i < maxIndex => h0 ++ fromHList(tail, i + 1, maxIndex) - case _ => h0 - } - } - - def defaultToValue[T](x: T): Map[String, Any] = { - val clazz = weakTypeOf[T].getClass - val classTag = ClassTag[T](clazz) - val rm = scala.reflect.runtime.currentMirror - val accessors = rm.classSymbol(x.getClass).toType.decls.sorted.view.collect { - case m: MethodSymbol if m.isGetter && m.isPublic => m - } - val instanceMirror = rm.reflect(x)(classTag) - (for (acc <- accessors.view) yield { - acc.name.toString -> instanceMirror.reflectMethod(acc).apply() - }).toMap - } - def apply(r: Record): Out = { val outValue: Map[String, Option[BT]] = { val jMap = r.bins.view collect { @@ -166,33 +129,6 @@ trait BinWrapper[BT] { val all = s.view(1, s.length - 1).mkString.split("\\), ", s.length).view all.dropRight(1).toArray ++ Array(all.last.dropRight(1)) } - - def isTuple[T](x: T) = x.getClass.getSimpleName.contains("Tuple") - - def optTuple[TPL <: Any with Product](tpl: TPL) = tpl match { - case t2: Tuple2[_, _] => Option(t2) - case t3: Tuple3[_, _, _] => Option(t3) - case t4: Tuple4[_, _, _, _] => Option(t4) - case t5: Tuple5[_, _, _, _, _] => Option(t5) - case t6: Tuple6[_, _, _, _, _, _] => Option(t6) - case t7: Tuple7[_, _, _, _, _, _, _] => Option(t7) - case t8: Tuple8[_, _, _, _, _, _, _, _] => Option(t8) - case t9: Tuple9[_, _, _, _, _, _, _, _, _] => Option(t9) - case t10: Tuple10[_, _, _, _, _, _, _, _, _, _] => Option(t10) - case t11: Tuple11[_, _, _, _, _, _, _, _, _, _, _] => Option(t11) - case t12: Tuple12[_, _, _, _, _, _, _, _, _, _, _, _] => Option(t12) - case t13: Tuple13[_, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t13) - case t14: Tuple14[_, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t14) - case t15: Tuple15[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t15) - case t16: Tuple16[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t16) - case t17: Tuple17[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t17) - case t18: Tuple18[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t18) - case t19: Tuple19[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t19) - case t20: Tuple20[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t20) - case t21: Tuple21[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t21) - case t22: Tuple22[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t22) - case _ => None - } } @@ -212,69 +148,64 @@ object BinWrapper { def mp(add: Tree) = q"""override def fetch(any: Any): Option[$tpe] = any match { - case v: $tpe => Option(v) - case any: Any => scala.util.Try{$add}.toOption - case oth => None - } - """ + case v: $tpe => Option(v) + case any: Any => scala.util.Try{$add}.toOption + case oth => None + }""" def tupleFetch(ts: List[String]) = - q""" - override def fetch(any: Any): Option[$tpe] = Value.getFromRecordObject(any) match { - case m: MapValue => m.getObject match { - case ms: java.util.Map[Any @unchecked, Any @unchecked] => - val res = castTuple(ms.asScala.toMap, $ts) - res.collect { case t: $tpe => t } - case _ => None - } - case _ => None - }""" + q"""override def fetch(any: Any): Option[$tpe] = Value.getFromRecordObject(any) match { + case m: MapValue => m.getObject match { + case ms: java.util.Map[Any @unchecked, Any @unchecked] => + val res = castTuple(ms.asScala.toMap, $ts) + res.collect { case t: $tpe => t } + case _ => None + } + case _ => None + }""" val mh = q"""override def fetch(any: Any): Option[$tpe] = Value.getFromRecordObject(any) match { - case m: MapValue => m.getObject match { - case ms: java.util.Map[Any @unchecked, Any @unchecked] => - val newList = castHListElements(ms.asScala.values.toList, $tpeSt) - newList.toHList[$tpe] - case _ => None - } - case _ => None - } - """ - - def typedList(pType: Type): Tree = + case m: MapValue => m.getObject match { + case ms: java.util.Map[Any @unchecked, Any @unchecked] => + val newList = castHListElements(ms.asScala.values.toList, $tpeSt) + newList.toHList[$tpe] + case _ => None + } + case _ => None + }""" + + def typedList(pType: Tree): Tree = q"""override def fetch(any: Any): Option[$tpe] = Try{Value.getFromRecordObject(any) match { - case lv: ListValue => lv.getObject match { - case ls: java.util.List[$pType @unchecked] => ls.toList - } - } - }.toOption """ - - def typedArray(pType: Type): Tree = - q"""override def fetch(any: Any): Option[$tpe] = Try { - Value.getFromRecordObject(any) match { - case lv: ListValue => lv.getObject match { - case ls: java.util.List[$pType @unchecked] => ls.toStream.toArray - } - } - }.toOption """ - - def streamedArray(pType: Type, to: Tree): Tree = - q"""override def fetch(any: Any): Option[$tpe] = Try { - Value.getFromRecordObject(any) match { - case lv: ListValue => lv.getObject match { - case ls: java.util.List[$pType @unchecked] => ls.toStream.map($to).toArray - } - } - }.toOption """ - - def typedMap(k: Type, v: Type): Tree = - q""" override def fetch(any: Any): Option[$tpe] = Try{Value.getFromRecordObject(any) match { - case m: MapValue => m.getObject match { - case ms: java.util.Map[$k @unchecked, $v @unchecked] => ms.toMap - } - } - }.toOption""" + case lv: ListValue => lv.getObject match { + case ls: java.util.List[$pType @unchecked] => ls.toList + } + } + }.toOption """ + + def typedArray(pType: Tree): Tree = + q"""override def fetch(any: Any): Option[$tpe] = Try { Value.getFromRecordObject(any) match { + case lv: ListValue => lv.getObject match { + case ls: java.util.List[$pType @unchecked] => ls.toStream.toArray + } + } + }.toOption """ + + def streamedArray(pType: Tree, to: Tree): Tree = + q"""override def fetch(any: Any): Option[$tpe] = Try { Value.getFromRecordObject(any) match { + case lv: ListValue => lv.getObject match { + case ls: java.util.List[$pType @unchecked] => ls.toStream.map($to).toArray + } + } + }.toOption """ + + def typedMap(k: Tree, v: Tree): Tree = + q"""override def fetch(any: Any): Option[$tpe] = Try{Value.getFromRecordObject(any) match { + case m: MapValue => m.getObject match { + case ms: java.util.Map[$k @unchecked, $v @unchecked] => ms.toMap + } + } + }.toOption""" def tupleArity(tpe: Type): Int = { val rex = "Tuple(\\d{1,2})".r @@ -284,58 +215,51 @@ object BinWrapper { } } + + def typedPlain(pType: Tree, cast: Tree): Tree = + q"""override def fetch(any: Any): Option[$tpe] = any match { + case v: $pType => Option($cast) + case _ => None + }""" + val fetchValue = tpe match { case t if t.toString.contains("HNil") || t.toString.contains("HList") => mh case t if t =:= weakTypeOf[String] => q"""override def fetch(any: Any): Option[$tpe] = - Try(Value.getFromRecordObject(any).getObject.toString).toOption""" - - case t if t =:= weakTypeOf[Boolean] => q"""override def fetch(any: Any): Option[$tpe] = any match { - case v: java.lang.Long => Option(v == 1) - case _ => None - } """ - case t if t =:= weakTypeOf[Float] => q"""override def fetch(any: Any): Option[$tpe] = any match { - case v: java.lang.Double => Option(v.toFloat) - case _ => None - } """ - case t if t =:= weakTypeOf[Char] => q"""override def fetch(any: Any): Option[$tpe] = any match { - case v: String => v.toString.toCharArray.headOption - case _ => None - } """ - case t if t =:= weakTypeOf[Int] => q"""override def fetch(any: Any): Option[$tpe] = any match { - case v: java.lang.Long => Option(v.toInt) - case _ => None - } """ - case t if t =:= weakTypeOf[Short] => q"""override def fetch(any: Any): Option[$tpe] = any match { - case v: java.lang.Long => Option(v.toShort) - case _ => None - } """ - case t if t =:= weakTypeOf[Byte] => q"""override def fetch(any: Any): Option[$tpe] = any match { - case v: java.lang.Long => Option(v.toByte) - case _ => None - }""" - case t if t =:= weakTypeOf[List[String]] => typedList(weakTypeOf[String]) - case t if t =:= weakTypeOf[List[Int]] => typedList(weakTypeOf[Int]) - case t if t =:= weakTypeOf[List[Long]] => typedList(weakTypeOf[Long]) - case t if t =:= weakTypeOf[List[Float]] => typedList(weakTypeOf[Float]) - case t if t =:= weakTypeOf[List[Double]] => typedList(weakTypeOf[Double]) - case t if t =:= weakTypeOf[List[Boolean]] => typedList(weakTypeOf[Boolean]) - case t if t =:= weakTypeOf[Array[String]] => typedArray(weakTypeOf[String]) - case t if t =:= weakTypeOf[Array[Int]] => streamedArray(weakTypeOf[Long], q"_.toInt") - case t if t =:= weakTypeOf[Array[Long]] => typedArray(weakTypeOf[Long]) - case t if t =:= weakTypeOf[Array[Float]] => streamedArray(weakTypeOf[Double], q"_.toFloat") - case t if t =:= weakTypeOf[Array[Double]] => typedArray(weakTypeOf[Double]) - case t if t =:= weakTypeOf[Array[Boolean]] => typedArray(weakTypeOf[Boolean]) - case t if t =:= weakTypeOf[Map[Int, String]] => typedMap(weakTypeOf[Int], weakTypeOf[String]) - case t if t =:= weakTypeOf[Map[String, String]] => typedMap(weakTypeOf[String], weakTypeOf[String]) - case t if t =:= weakTypeOf[Map[String, Int]] => typedMap(weakTypeOf[String], weakTypeOf[Int]) - case t if t =:= weakTypeOf[Map[String, Long]] => typedMap(weakTypeOf[String], weakTypeOf[Long]) - case t if t =:= weakTypeOf[Map[String, Float]] => typedMap(weakTypeOf[String], weakTypeOf[Float]) - case t if t =:= weakTypeOf[Map[String, Double]] => typedMap(weakTypeOf[String], weakTypeOf[Double]) + Try(Value.getFromRecordObject(any).getObject.toString).toOption""" + + case t if t =:= weakTypeOf[Boolean] => typedPlain(tq"java.lang.Long", q"v == 1") + case t if t =:= weakTypeOf[Float] => typedPlain(tq"java.lang.Double", q"v.toFloat") + case t if t =:= weakTypeOf[Char] => + q"""override def fetch(any: Any): Option[$tpe] = any match { + case v: String => v.toString.toCharArray.headOption + case _ => None + } """ + case t if t =:= weakTypeOf[Int] => typedPlain(tq"java.lang.Long", q"v.toInt") + case t if t =:= weakTypeOf[Short] => typedPlain(tq"java.lang.Long", q"v.toShort") + case t if t =:= weakTypeOf[Byte] => typedPlain(tq"java.lang.Long", q"v.toByte") + case t if t =:= weakTypeOf[List[String]] => typedList(tq"String") + case t if t =:= weakTypeOf[List[Int]] => typedList(tq"Int") + case t if t =:= weakTypeOf[List[Long]] => typedList(tq"Long") + case t if t =:= weakTypeOf[List[Float]] => typedList(tq"Float") + case t if t =:= weakTypeOf[List[Double]] => typedList(tq"Double") + case t if t =:= weakTypeOf[List[Boolean]] => typedList(tq"Boolean") + case t if t =:= weakTypeOf[Array[String]] => typedArray(tq"String") + case t if t =:= weakTypeOf[Array[Int]] => streamedArray(tq"Long", q"_.toInt") + case t if t =:= weakTypeOf[Array[Long]] => typedArray(tq"Long") + case t if t =:= weakTypeOf[Array[Float]] => streamedArray(tq"Double", q"_.toFloat") + case t if t =:= weakTypeOf[Array[Double]] => typedArray(tq"Double") + case t if t =:= weakTypeOf[Array[Boolean]] => typedArray(tq"Boolean") + case t if t =:= weakTypeOf[Map[Int, String]] => typedMap(tq"Int", tq"String") + case t if t =:= weakTypeOf[Map[String, String]] => typedMap(tq"String", tq"String") + case t if t =:= weakTypeOf[Map[String, Int]] => typedMap(tq"String", tq"Int") + case t if t =:= weakTypeOf[Map[String, Long]] => typedMap(tq"String", tq"Long") + case t if t =:= weakTypeOf[Map[String, Float]] => typedMap(tq"String", tq"Float") + case t if t =:= weakTypeOf[Map[String, Double]] => typedMap(tq"String", tq"Double") case t if t =:= weakTypeOf[Map[String, List[Int]]] => mp(q"""toKVmap[String, List[Int]](any, coll)(_.toString, toLs(_)(_.toInt))""") case t if t =:= weakTypeOf[Map[String, List[String]]] => mp(q"""toKVmap[String, List[String]](any, coll)(_.toString, toLs(_))""") - case t if t =:= weakTypeOf[Map[String, Any]] => typedMap(weakTypeOf[String], weakTypeOf[Any]) + case t if t =:= weakTypeOf[Map[String, Any]] => typedMap(tq"String", tq"Any") case t if tupleArity(t) != 0 => val tplArty = tupleArity(t) if (tplArty > 0 && tplArty < 23) tupleFetch(t.typeArgs.map(_.toString)) else q"""None""" @@ -347,7 +271,7 @@ object BinWrapper { import java.util.{List => JList, Map => JMap} import com.aerospike.client.{Bin, Record, Value} - import com.aerospike.client.Value.{BlobValue, ListValue, MapValue, ValueArray} + import com.aerospike.client.Value._ import scala.collection.JavaConversions._ import scala.collection.JavaConverters._ import scala.collection.mutable.{Seq => mSeq} diff --git a/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/KeyWrapper.scala b/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/KeyWrapper.scala index 7a3c1e4..133e874 100644 --- a/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/KeyWrapper.scala +++ b/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/KeyWrapper.scala @@ -18,14 +18,10 @@ package ru.tinkoff.aerospikemacro.converters import com.aerospike.client.Value._ import com.aerospike.client.{Key, Value} -import com.typesafe.config.{Config, ConfigFactory} import ru.tinkoff.aerospikemacro.domain.{DBCredentials, WrapperException} -import ru.tinkoff.aerospikescala.domain.ByteSegment - -import scala.collection.JavaConversions._ import scala.language.experimental.macros -import scala.reflect.macros.blackbox.Context -import scala.util.{Failure, Success, Try} +import scala.reflect.macros.blackbox +import Utils._ /** @@ -47,11 +43,10 @@ trait KeyWrapper[KT] { object KeyWrapper { - import Utils._ implicit def materializeK[T](implicit dbc: DBCredentials): KeyWrapper[T] = macro implK[T] - def implK[T: c.WeakTypeTag](c: Context)(dbc: c.Expr[DBCredentials]): c.Expr[KeyWrapper[T]] = { + def implK[T: c.WeakTypeTag](c: blackbox.Context)(dbc: c.Expr[DBCredentials]): c.Expr[KeyWrapper[T]] = { import c.universe._ val tpe = weakTypeOf[T] @@ -59,11 +54,7 @@ object KeyWrapper { val tableName = reify(dbc.splice.setname) val tpeName = q"${tpe.typeSymbol.fullName}" - val err = - q"""throw new IllegalArgumentException( - "You need to write your own toValue function in KeyWrapper implicit for type " + $tpeName) """ - - val toDBVlue = pickValue(c) + val toDBValue = pickValue(c, "KeyWrapper") c.Expr[KeyWrapper[T]] { q""" @@ -73,11 +64,12 @@ object KeyWrapper { import scala.collection.immutable.Seq import ru.tinkoff.aerospikescala.domain.ByteSegment import scala.util.{Failure, Success, Try} + import ru.tinkoff.aerospikemacro.converters.Utils.defaultToValue new KeyWrapper[$tpe] { override val dbName = $db override val tableName = $tableName - override def toValue(v: $tpe): Value = $toDBVlue + override def toValue(v: $tpe): Value = $toDBValue } """ } diff --git a/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/Utils.scala b/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/Utils.scala index 55fb682..0fee10b 100644 --- a/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/Utils.scala +++ b/aerospike-scala-macros/src/main/scala/ru/tinkoff/aerospikemacro/converters/Utils.scala @@ -1,22 +1,102 @@ package ru.tinkoff.aerospikemacro.converters +import com.aerospike.client.Value +import com.aerospike.client.Value._ import ru.tinkoff.aerospikescala.domain.ByteSegment +import shapeless.{::, HList} + +import scala.collection.immutable.{List, ListMap, Map} +import scala.reflect.ClassTag +import scala.reflect.macros.blackbox +import scala.util.{Failure, Success} +import scala.reflect.runtime.universe._ +import scala.collection.JavaConverters._ -import scala.reflect.macros.blackbox.Context /** * @author MarinaSigaeva * @since 04.04.17 */ object Utils { - def pickValue[T: c.WeakTypeTag](c: Context): c.universe.Tree = { + + def isTuple[T](x: T) = x.getClass.getSimpleName.contains("Tuple") + + def defaultToValue[T](x: T): Map[String, Any] = { + val clazz = weakTypeOf[T].getClass + val classTag = ClassTag[T](clazz) + val rm = scala.reflect.runtime.currentMirror + val accessors = rm.classSymbol(x.getClass).toType.decls.sorted.view.collect { + case m: MethodSymbol if m.isGetter && m.isPublic => m + } + val instanceMirror = rm.reflect(x)(classTag) + (for (acc <- accessors.view) yield { + acc.name.toString -> instanceMirror.reflectMethod(acc).apply() + }).toMap + } + + def typed[T](x: T)(implicit tag: WeakTypeTag[T]): T = x.asInstanceOf[T] + + def fromHList[L <: HList](hList: L, i: Int, maxIndex: Int): Map[String, Any] = { + val h0 = Map(i.toString -> typed(hList.productElement(0))) + hList match { + case head :: tail if i < maxIndex => h0 ++ fromHList(tail, i + 1, maxIndex) + case _ => h0 + } + } + + def tupleMapped[TPL <: Any with Product](tpl: TPL): Map[String, Any] = { + val i = tpl.productArity + val m = optTuple(tpl).map(mapify(i, _)).getOrElse(Map.empty) + ListMap(m.toSeq.sortBy(_._1): _*).view.map { + case (k, v) => k.toString -> v + }.toMap + } + + def mapify[H <: Any with Product](i: Int, t: H): Map[Int, Any] = { + (for (e <- 0 until i) yield (e, t.productElement(e))) (collection.breakOut) + } + + def optTuple[TPL <: Any with Product](tpl: TPL) = tpl match { + case t2: Tuple2[_, _] => Option(t2) + case t3: Tuple3[_, _, _] => Option(t3) + case t4: Tuple4[_, _, _, _] => Option(t4) + case t5: Tuple5[_, _, _, _, _] => Option(t5) + case t6: Tuple6[_, _, _, _, _, _] => Option(t6) + case t7: Tuple7[_, _, _, _, _, _, _] => Option(t7) + case t8: Tuple8[_, _, _, _, _, _, _, _] => Option(t8) + case t9: Tuple9[_, _, _, _, _, _, _, _, _] => Option(t9) + case t10: Tuple10[_, _, _, _, _, _, _, _, _, _] => Option(t10) + case t11: Tuple11[_, _, _, _, _, _, _, _, _, _, _] => Option(t11) + case t12: Tuple12[_, _, _, _, _, _, _, _, _, _, _, _] => Option(t12) + case t13: Tuple13[_, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t13) + case t14: Tuple14[_, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t14) + case t15: Tuple15[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t15) + case t16: Tuple16[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t16) + case t17: Tuple17[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t17) + case t18: Tuple18[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t18) + case t19: Tuple19[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t19) + case t20: Tuple20[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t20) + case t21: Tuple21[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t21) + case t22: Tuple22[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Option(t22) + case _ => None + } + + + def throwIllegal(w: String, t: String): Tree = { + val tpeName = q"$t" + + q"""throw new IllegalArgumentException( + "You need to write your own toValue function in " + $w + " implicit for type " + $tpeName) """ + } + + def pickValue[T: c.WeakTypeTag](c: blackbox.Context, wrapper: String): c.universe.Tree = { import c.universe._ val tpe = weakTypeOf[T] val tpeName = q"${tpe.typeSymbol.fullName}" val err = q"""throw new IllegalArgumentException( - "You need to write your own toValue function in KeyWrapper implicit for type " + $tpeName) """ + "You need to write your own toValue function in " + $wrapper + " implicit for type " + $tpeName)""" tpe match { case t if t =:= weakTypeOf[ByteSegment] => q"""v match { @@ -24,18 +104,27 @@ object Utils { case _ => $err }""" case t if t =:= weakTypeOf[Int] => q"""new IntegerValue(v)""" + case t if t =:= weakTypeOf[Short] => q"""new IntegerValue(v)""" + case t if t =:= weakTypeOf[Byte] => q"""new IntegerValue(v)""" case t if t =:= weakTypeOf[Long] => q"""new LongValue(v)""" case t if t =:= weakTypeOf[String] => q"""new StringValue(v)""" + case t if t =:= weakTypeOf[Char] => q"""new StringValue(v.toString)""" case t if t =:= weakTypeOf[Boolean] => q"""new BooleanValue(v)""" case t if t =:= weakTypeOf[Float] => q"""new FloatValue(v)""" case t if t =:= weakTypeOf[Double] => q"""new DoubleValue(v)""" + case t if t =:= weakTypeOf[java.util.List[_]] => q"""new ListValue(v)""" + case t if t =:= weakTypeOf[List[_]] => q"""new ListValue(v)""" + case t if t =:= weakTypeOf[Array[_]] => q"""new ListValue(v.toList)""" + case t if t =:= weakTypeOf[java.util.Map[_, _]] => q"""new MapValue(v)""" case t if t =:= weakTypeOf[Array[Byte]] => q"""new BytesValue(v)""" case t if t =:= weakTypeOf[scala.collection.immutable.Seq[_]] => q"""new ListValue(v)""" case t if t =:= weakTypeOf[Map[_, _]] => q"""new MapValue(v)""" - case _ => q"""Try(Value.get(v)) match { - case Failure(th) => $err - case Success(s) => s - }""" + case t: Any with Product if isTuple(t) => q"""new MapValue(tupleMapped($tpe))""" + case yourCaseClass => q""" Value.get(v) match { + case n: NullValue => $err + case other => other + }""" + } } } \ No newline at end of file diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index df2a981..28be13a 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -2,6 +2,6 @@ ru-tinkoff-aerospike-dsl { keyWrapper-namespace = "test" keyWrapper-setName = "test33" - example-host = "host" + example-host = "127.0.0.1" example-port = 3000 } \ No newline at end of file diff --git a/src/main/scala/ru/tinkoff/aerospike/dsl/Spike.scala b/src/main/scala/ru/tinkoff/aerospike/dsl/Spike.scala index 5cb0f6d..1d22fb5 100644 --- a/src/main/scala/ru/tinkoff/aerospike/dsl/Spike.scala +++ b/src/main/scala/ru/tinkoff/aerospike/dsl/Spike.scala @@ -17,7 +17,7 @@ package ru.tinkoff.aerospike.dsl import com.aerospike.client.policy._ -import ru.tinkoff.aerospike.dsl.batchread.BatchReadWrapper +//import ru.tinkoff.aerospike.dsl.batchread.BatchReadWrapper import ru.tinkoff.aerospikemacro.converters.KeyWrapper._ import ru.tinkoff.aerospikemacro.converters.{BinWrapper, KeyWrapper} import ru.tinkoff.aerospikescala.domain.ABin @@ -80,9 +80,9 @@ trait Spike { optBP: Option[BatchPolicy] = None): Future[Unit] //note, if you will not change namespace, setName parameters in BatchReadWrapper - default values from application.conf will be used - def getByKeysWithBatchListener[L](kws: List[BatchReadWrapper], +/* def getByKeysWithBatchListener[L](kws: List[BatchReadWrapper], listener: Option[L] = None)( - implicit optBP: Option[BatchPolicy] = None): Future[Unit] + implicit optBP: Option[BatchPolicy] = None): Future[Unit]*/ def deleteK[K](k: K)(implicit kC: KeyWrapper[K], pw: Option[WritePolicy] = None, diff --git a/src/main/scala/ru/tinkoff/aerospike/dsl/SpikeImpl.scala b/src/main/scala/ru/tinkoff/aerospike/dsl/SpikeImpl.scala index 3a7d25a..3c26830 100644 --- a/src/main/scala/ru/tinkoff/aerospike/dsl/SpikeImpl.scala +++ b/src/main/scala/ru/tinkoff/aerospike/dsl/SpikeImpl.scala @@ -21,7 +21,7 @@ import com.aerospike.client.async._ import com.aerospike.client.listener._ import com.aerospike.client.policy._ import com.aerospike.client.query.Statement -import ru.tinkoff.aerospike.dsl.batchread.BatchReadWrapper +//import ru.tinkoff.aerospike.dsl.batchread.BatchReadWrapper import ru.tinkoff.aerospikemacro.converters.{BinWrapper, KeyWrapper} import ru.tinkoff.aerospike.dsl.errors.AerospikeDSLError import ru.tinkoff.aerospikescala.domain.{ABin, MBin, SingleBin} @@ -229,7 +229,7 @@ class SpikeImpl(spikeClient: IAsyncClient)(implicit val ec: ExecutionContext) ex } } - def getByKeysWithBatchListener[L](kws: List[BatchReadWrapper], listener: Option[L] = None)(implicit optBP: Option[BatchPolicy] = None): Future[Unit] = { +/* def getByKeysWithBatchListener[L](kws: List[BatchReadWrapper], listener: Option[L] = None)(implicit optBP: Option[BatchPolicy] = None): Future[Unit] = { val policy = optBP.getOrElse(new BatchPolicy) val records: java.util.List[BatchRead] = kws.view.map(e => e.apply).toList @@ -239,7 +239,7 @@ class SpikeImpl(spikeClient: IAsyncClient)(implicit val ec: ExecutionContext) ex case None => get(policy, records) case _ => throw AerospikeDSLError(s"Unsupported listener type $listener") } - } + }*/ } diff --git a/src/main/scala/ru/tinkoff/aerospike/dsl/batchread/BatchReadWrapper.scala b/src/main/scala/ru/tinkoff/aerospike/dsl/batchread/BatchReadWrapper.scala index 81a5639..5aa46d2 100644 --- a/src/main/scala/ru/tinkoff/aerospike/dsl/batchread/BatchReadWrapper.scala +++ b/src/main/scala/ru/tinkoff/aerospike/dsl/batchread/BatchReadWrapper.scala @@ -28,6 +28,7 @@ import scala.language.experimental.macros * @author MarinaSigaeva * @since 21.09.16 */ +/* trait BatchReadWrapper { val keyValue: Any val binNames: Array[String] @@ -36,3 +37,4 @@ trait BatchReadWrapper { def applyO[S <: Any](k: S)(implicit kW: KeyWrapper[S]): BatchRead = new BatchRead(kW(k), binNames) def apply = applyO(keyValue) } +*/ diff --git a/src/test/scala/ru/tinkoff/aerospikemacro/converters/BatchReadWrapperTest.scala b/src/test/scala/ru/tinkoff/aerospikemacro/converters/BatchReadWrapperTest.scala index c08c600..70fea16 100644 --- a/src/test/scala/ru/tinkoff/aerospikemacro/converters/BatchReadWrapperTest.scala +++ b/src/test/scala/ru/tinkoff/aerospikemacro/converters/BatchReadWrapperTest.scala @@ -1,3 +1,4 @@ +/* /* * Copyright (c) 2016 Tinkoff * @@ -60,3 +61,4 @@ class BatchReadWrapperTest extends FlatSpec with Matchers { } } +*/