Skip to content

Commit 2d5aac3

Browse files
authored
Merge pull request #667 from Dwolla/TraceableValue
add TraceableValue typeclass and base implementations
2 parents 9d74b8f + eb25672 commit 2d5aac3

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

modules/core/shared/src/main/scala/TraceValue.scala

+42-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,47 @@ object TraceValue {
1414
case class BooleanValue(value: Boolean) extends TraceValue
1515
case class NumberValue(value: Number) extends TraceValue
1616

17-
implicit def stringToTraceValue(value: String): TraceValue = StringValue(value)
18-
implicit def boolToTraceValue(value: Boolean): TraceValue = BooleanValue(value)
19-
implicit def intToTraceValue(value: Int): TraceValue = NumberValue(value)
20-
implicit def longToTraceValue(value: Long): TraceValue = NumberValue(value)
21-
implicit def floatToTraceValue(value: Float): TraceValue = NumberValue(value)
22-
implicit def doubleToTraceValue(value: Double): TraceValue = NumberValue(value)
17+
implicit def viaTraceableValue[A: TraceableValue](a: A): TraceValue =
18+
TraceableValue[A].toTraceValue(a)
2319

20+
@deprecated("use toTraceValue", "0.3.0")
21+
def stringToTraceValue(value: String): TraceValue = StringValue(value)
22+
@deprecated("use toTraceValue", "0.3.0")
23+
def boolToTraceValue(value: Boolean): TraceValue = BooleanValue(value)
24+
@deprecated("use toTraceValue", "0.3.0")
25+
def intToTraceValue(value: Int): TraceValue = NumberValue(value)
26+
@deprecated("use toTraceValue", "0.3.0")
27+
def longToTraceValue(value: Long): TraceValue = NumberValue(value)
28+
@deprecated("use toTraceValue", "0.3.0")
29+
def floatToTraceValue(value: Float): TraceValue = NumberValue(value)
30+
@deprecated("use toTraceValue", "0.3.0")
31+
def doubleToTraceValue(value: Double): TraceValue = NumberValue(value)
32+
}
33+
34+
/** A lawless typeclass responsible for converting a value of the type
35+
* parameter `A` to Natchez's `TraceValue`.
36+
*
37+
* You may want to use this to customize the formatting of a value
38+
* before attaching it to a span, or to support adding tracing as a
39+
* cross-cutting concern using aspect-oriented programming from a
40+
* library such as cats-tagless.
41+
*
42+
* @tparam A The type to be converted to `TraceValue`
43+
*/
44+
trait TraceableValue[A] { outer =>
45+
def toTraceValue(a: A): TraceValue
46+
47+
final def contramap[B](f: B => A): TraceableValue[B] =
48+
(b: B) => outer.toTraceValue(f(b))
49+
}
50+
51+
object TraceableValue {
52+
def apply[A: TraceableValue]: TraceableValue[A] = implicitly
53+
54+
implicit val stringToTraceValue: TraceableValue[String] = TraceValue.StringValue(_)
55+
implicit val booleanToTraceValue: TraceableValue[Boolean] = TraceValue.BooleanValue(_)
56+
implicit val intToTraceValue: TraceableValue[Int] = TraceValue.NumberValue(_)
57+
implicit val longToTraceValue: TraceableValue[Long] = TraceValue.NumberValue(_)
58+
implicit val doubleToTraceValue: TraceableValue[Double] = TraceValue.NumberValue(_)
59+
implicit val floatToTraceValue: TraceableValue[Float] = TraceValue.NumberValue(_)
2460
}

0 commit comments

Comments
 (0)