Skip to content

Commit

Permalink
Much renaming (#115)
Browse files Browse the repository at this point in the history
* fmt

* Add StringToNameImplicits back to LoggingBase

* Rename FieldTypeClass and NameTypeClass

* move

* scala3 fixes
  • Loading branch information
wsargent committed Apr 27, 2024
1 parent a1a026f commit fa63a8f
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.tersesystems.echopraxia.plusscala.api
/**
* This trait resolves `Either` directly to the relevant value.
*/
trait EitherValueTypes { self: ValueTypeClasses =>
trait EitherToValueImplicits { self: ValueTypeClasses =>
implicit def eitherToValue[L: ToValue, R: ToValue]: ToValue[Either[L, R]] = {
case Left(left) => ToValue(left)
case Right(right) => ToValue(right)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.tersesystems.echopraxia.plusscala.api

import com.tersesystems.echopraxia.api.Attributes
import com.tersesystems.echopraxia.api.Value

/**
* This trait resolves `Either` directly to the relevant value.
*/
trait EitherValueTypes { self: ValueTypeClasses =>
trait EitherToValueImplicits { self: ValueTypeClasses =>
implicit def eitherToValue[L: ToValue, R: ToValue]: ToValue[Either[L, R]] = {
case Left(left) => ToValue(left)
case Right(right) => ToValue(right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait HasFieldClass {
protected def fieldClass: Class[_ <: FieldType]
}

trait KeyValueFieldBuilder extends ValueTypeClasses with NameTypeClass with HasFieldClass {
trait KeyValueFieldBuilder extends ValueTypeClasses with NameTypeClasses with HasFieldClass {
// ------------------------------------------------------------------
// keyValue

Expand All @@ -30,7 +30,7 @@ trait KeyValueFieldBuilder extends ValueTypeClasses with NameTypeClass with HasF
def value[F: ToName: ToValue](field: F): FieldType
}

trait ExceptionFieldBuilder extends ValueTypeClasses with NameTypeClass with HasFieldClass {
trait ExceptionFieldBuilder extends ValueTypeClasses with NameTypeClasses with HasFieldClass {
def exception[N: ToName](tuple: (N, Throwable)): FieldType
def exception[N: ToName](name: N, ex: Throwable): FieldType
def exception(ex: Throwable): FieldType
Expand All @@ -39,7 +39,7 @@ trait ExceptionFieldBuilder extends ValueTypeClasses with NameTypeClass with Has
/**
* A field builder that is enhanced with .
*/
trait ArrayObjFieldBuilder extends ValueTypeClasses with NameTypeClass with HasFieldClass {
trait ArrayObjFieldBuilder extends ValueTypeClasses with NameTypeClasses with HasFieldClass {
// ------------------------------------------------------------------
// array

Expand All @@ -53,7 +53,7 @@ trait ArrayObjFieldBuilder extends ValueTypeClasses with NameTypeClass with HasF
def obj[N: ToName, OV: ToObjectValue](tuple: (N, OV)): FieldType
}

trait PrimitiveFieldBuilder extends ValueTypeClasses with NameTypeClass with HasFieldClass {
trait PrimitiveFieldBuilder extends ValueTypeClasses with NameTypeClasses with HasFieldClass {
// ------------------------------------------------------------------
// string

Expand All @@ -73,7 +73,7 @@ trait PrimitiveFieldBuilder extends ValueTypeClasses with NameTypeClass with Has
def bool[N: ToName](tuple: (N, Boolean)): FieldType
}

trait NullFieldBuilder extends ValueTypeClasses with NameTypeClass with HasFieldClass {
trait NullFieldBuilder extends ValueTypeClasses with NameTypeClasses with HasFieldClass {
def nullField[N: ToName](name: N): FieldType
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.tersesystems.echopraxia.api.Attributes
import com.tersesystems.echopraxia.api.Field
import com.tersesystems.echopraxia.plusscala.spi.Utils

trait FieldConversionImplicits { self: ValueTypeClasses with NameTypeClass =>
trait FieldConversionImplicits { self: ValueTypeClasses with NameTypeClasses =>

// Convert a tuple into a field. This does most of the heavy lifting.
// i.e logger.info("foo" -> foo) becomes logger.info(Field.keyValue("foo", ToValue(foo)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.tersesystems.echopraxia.plusscala.api

import com.tersesystems.echopraxia.api.Value

trait ToFieldTypes { self: ValueTypeClasses with NameTypeClass =>
trait FieldTypeClasses { self: ValueTypeClasses with NameTypeClasses =>
// Provides easier packaging for ToName and ToValue
trait ToField[-TF] {
def toName: ToName[TF]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.concurrent.Future
import scala.util.Failure
import scala.util.Success

trait FutureValueTypes { self: ValueTypeClasses =>
trait FutureToValueImplicits { self: ValueTypeClasses =>
implicit def futureToValue[T: ToValue]: ToValue[Future[T]] = { f =>
f.value match {
case Some(value) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.tersesystems.echopraxia.plusscala.api
/**
* This is a trait that should be extended by domain logging traits.
*
* This trait recognizes strings as meaningful names. If this is not what you want (for example you want enums or refined types), you can create your
* own trait extending `LoggingTypeClasses with LoggingToValueImplicits` with your own `ToName` implicits.
*
* {{{
* trait Logging extends LoggingBase {
* implicit val currencyToField: ToField[Currency] = ToField(_ => "currency", currency => ToValue(currency.getCurrencyCode))
Expand All @@ -26,12 +29,23 @@ package com.tersesystems.echopraxia.plusscala.api
* }
* }}}
*/
trait LoggingBase
extends ValueTypeClasses
with NameTypeClass
with FieldConversionImplicits
with OptionValueTypes
with EitherValueTypes
with FutureValueTypes
with ToFieldTypes
with StringToNameImplicits
trait LoggingBase extends LoggingTypeClasses with FieldConversionImplicits with LoggingToValueImplicits with LoggingToNameImplicits

/**
* This trait aggregates the ToValue, ToName, and ToField type classes together for convenience.
*/
trait LoggingTypeClasses extends ValueTypeClasses with NameTypeClasses with FieldTypeClasses

/**
* This trait aggregates ToValue implicits for convenience.
*/
trait LoggingToValueImplicits extends OptionToValueImplicits with EitherToValueImplicits with FutureToValueImplicits {
this: ValueTypeClasses =>
}

/**
* This trait aggregates ToName implicits for convenience.
*/
trait LoggingToNameImplicits extends StringToNameImplicits with OptionToNameImplicits with TryToNameImplicits with EitherToNameImplicits {
this: NameTypeClasses =>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package com.tersesystems.echopraxia.plusscala.api
import com.tersesystems.echopraxia.spi.FieldConstants

import scala.annotation.implicitNotFound

import scala.util.Try
import scala.util.Success
import scala.util.Failure
import scala.util.Success
import scala.util.Try

/**
* Add this trait to get access to the ToName type class.
*/
trait NameTypeClass {
trait NameTypeClasses {
// this needs to be a dependent type because implicit type resolution only works on a
// field builder if it's dependent to the type itself.

Expand All @@ -29,30 +28,32 @@ trait NameTypeClass {
}
}

trait StringToNameImplicits extends NameTypeClass {
trait StringToNameImplicits { this: NameTypeClasses =>
implicit val stringToName: ToName[String] = _.orNull
}

trait OptionToNameImplicits extends NameTypeClass {
trait OptionToNameImplicits { this: NameTypeClasses =>
implicit def optionToName[T: ToName]: ToName[Option[T]] = (t: Option[Option[T]]) => implicitly[ToName[T]].toName(t.flatten)
}

trait EitherToNameImplicits extends NameTypeClass {
trait EitherToNameImplicits { this: NameTypeClasses =>
implicit def eitherToName[L: ToName, R: ToName]: ToName[Either[L, R]] = {
case Some(either) => either match {
case Left(l: L) => ToName(l)
case Right(r: R) => ToName(r)
}
case Some(either) =>
either match {
case Left(l) => ToName(l)
case Right(r) => ToName(r)
}
case None => null
}
}

trait TryToNameImplicits extends NameTypeClass {
trait TryToNameImplicits { this: NameTypeClasses =>
implicit def tryToName[T: ToName]: ToName[Try[T]] = {
case Some(t) => t match {
case Success(v: T) => ToName(v)
case Failure(e) => ToName(e)
}
case Some(t) =>
t match {
case Success(v) => ToName(v)
case Failure(e) => ToName(e)
}
case None => implicitly[ToName[T]].toName(None)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.tersesystems.echopraxia.api.Value
/**
* This trait resolves options to either the value, or nullValue if `None`.
*/
trait OptionValueTypes { self: ValueTypeClasses =>
trait OptionToValueImplicits { self: ValueTypeClasses =>
implicit def optionToValue[V: ToValue]: ToValue[Option[V]] = {
case Some(v) => ToValue(v)
case None => Value.nullValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.tersesystems.echopraxia.plusscala.api
import java.time.Instant

// This trait should be extended for domain model classes
trait Logging extends LoggingBase with OptionToNameImplicits with TryToNameImplicits with EitherToNameImplicits {
trait Logging extends LoggingBase {
implicit val instantToValue: ToValue[Instant] = v => ToValue(v.toString)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.tersesystems.echopraxia.benchmarks
import com.tersesystems.echopraxia.plusscala.LoggerFactory
import com.tersesystems.echopraxia.plusscala.api.Condition
import com.tersesystems.echopraxia.plusscala.api.LoggingBase
import com.tersesystems.echopraxia.plusscala.api.StringToNameImplicits
import org.openjdk.jmh.annotations._

import java.util.concurrent.TimeUnit
Expand Down
4 changes: 2 additions & 2 deletions generic/src/test/scala-3/DerivationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class DerivationSpec extends AnyFunSpec with BeforeAndAfterEach with Matchers {

// trait ValueOnly extends FieldBuilder with SemiAutoDerivation with ValueCaseClassDerivation

trait ShortEither extends FieldBuilder with AutoDerivation with EitherValueTypes
trait ShortEither extends FieldBuilder with AutoDerivation with EitherToValueImplicits

trait ShortOption extends FieldBuilder with AutoDerivation with OptionValueTypes
trait ShortOption extends FieldBuilder with AutoDerivation with OptionToValueImplicits

private val autoLogger = LoggerFactory.getLogger(getClass, AutoFieldBuilder)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.tersesystems.echopraxia.plusscala

import com.tersesystems.echopraxia.api.Field
import com.tersesystems.echopraxia.plusscala.api.EitherToNameImplicits
import com.tersesystems.echopraxia.plusscala.api.HeterogeneousFieldSupport
import com.tersesystems.echopraxia.plusscala.api.OptionToNameImplicits
import com.tersesystems.echopraxia.plusscala.api.TryToNameImplicits
import com.tersesystems.echopraxia.plusscala.api.EitherToNameImplicits
import com.tersesystems.echopraxia.plusscala.api.PresentationFieldBuilder

import scala.util.Try
import com.tersesystems.echopraxia.plusscala.api.TryToNameImplicits

import java.util.Currency
import java.util.UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ trait DefaultTraceFieldBuilder extends FieldBuilder with TraceFieldBuilder {
new DefaultSourceFields(SourceCode(line, file, enc), args)
}

object DefaultTraceFieldBuilder extends DefaultTraceFieldBuilder with StringToNameImplicits {
object DefaultTraceFieldBuilder extends DefaultTraceFieldBuilder {
val TraceTag: String = "traceTag"
val Entry: String = "entry"
val Exit: String = "exit"
Expand Down

0 comments on commit fa63a8f

Please sign in to comment.