diff options
author | Sergey Nastich <nastich@users.noreply.github.com> | 2018-09-12 16:10:17 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-12 16:10:17 -0400 |
commit | 8fef53d44a57008dea411b882b12bc3d5d1ca2e0 (patch) | |
tree | d2304188d61bdd39ee4c494f3e309c209908592a /src/main/scala/xyz/driver/core/tagging | |
parent | ccd6c4281fb0ddd0a74b79aa24f85834f5c9647a (diff) | |
download | driver-core-8fef53d44a57008dea411b882b12bc3d5d1ca2e0.tar.gz driver-core-8fef53d44a57008dea411b882b12bc3d5d1ca2e0.tar.bz2 driver-core-8fef53d44a57008dea411b882b12bc3d5d1ca2e0.zip |
Add `Trimmed` tag and its logic (revisited) (#215)v1.14.2
* Add option and iterable converters for transparent `@@ Trimmed` creation.
* Move tagging stuff to a separate package - relieve `core.scala` from some extra code.
* Add Tagging stuff and publishing section to README.md
Diffstat (limited to 'src/main/scala/xyz/driver/core/tagging')
-rw-r--r-- | src/main/scala/xyz/driver/core/tagging/tagging.scala | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/tagging/tagging.scala b/src/main/scala/xyz/driver/core/tagging/tagging.scala new file mode 100644 index 0000000..5b6599e --- /dev/null +++ b/src/main/scala/xyz/driver/core/tagging/tagging.scala @@ -0,0 +1,62 @@ +package xyz.driver.core + +import scala.collection.generic.CanBuildFrom +import scala.language.{higherKinds, implicitConversions} + +/** + * @author sergey + * @since 9/11/18 + */ +package object tagging { + + implicit class Taggable[V <: Any](val v: V) extends AnyVal { + def tagged[Tag]: V @@ Tag = v.asInstanceOf[V @@ Tag] + } + +} + +package tagging { + + sealed trait Tagged[+V, +Tag] + + object Tagged { + implicit class TaggedOps[V, Tag](val v: V @@ Tag) extends AnyVal { + def tagless: V = v + } + + implicit def orderingMagnet[V, Tag](implicit ord: Ordering[V]): Ordering[V @@ Tag] = + ord.asInstanceOf[Ordering[V @@ Tag]] + + } + + sealed trait Trimmed + + object Trimmed { + + implicit def apply[V](trimmable: V)(implicit ev: CanBeTrimmed[V]): V @@ Trimmed = { + ev.trim(trimmable).tagged[Trimmed] + } + + sealed trait CanBeTrimmed[T] { + def trim(trimmable: T): T + } + + implicit object StringCanBeTrimmed extends CanBeTrimmed[String] { + def trim(str: String): String = str.trim() + } + + implicit def nameCanBeTrimmed[T]: CanBeTrimmed[Name[T]] = new CanBeTrimmed[Name[T]] { + def trim(name: Name[T]): Name[T] = Name[T](name.value.trim()) + } + + implicit def option2Trimmed[V: CanBeTrimmed](option: Option[V]): Option[V @@ Trimmed] = + option.map(Trimmed(_)) + + implicit def coll2Trimmed[T, C[_] <: Traversable[_]](coll: C[T])( + implicit ev: C[T] <:< Traversable[T], + tr: CanBeTrimmed[T], + bf: CanBuildFrom[Nothing, T @@ Trimmed, C[T @@ Trimmed]]): C[T @@ Trimmed] = + ev(coll).map(Trimmed(_)(tr)).to[C] + } + +} |