-
Notifications
You must be signed in to change notification settings - Fork 155
/
generic.scala
128 lines (112 loc) · 4.09 KB
/
generic.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package eu.timepit.refined
import eu.timepit.refined.api.{Inference, Validate}
import eu.timepit.refined.api.Inference.==>
import eu.timepit.refined.generic._
import eu.timepit.refined.internal.WitnessAs
import shapeless._
import shapeless.ops.coproduct.ToHList
import shapeless.ops.hlist.ToList
import shapeless.ops.record.Keys
/** Module for generic predicates. */
object generic extends GenericInference {
/** Predicate that checks if a value is equal to `U`. */
final case class Equal[U](u: U)
@deprecated(
"Deprecated because ConstructorNames operates on types and not values and refined focuses on refining values.",
"0.9.0"
)
final case class ConstructorNames[P](p: P)
@deprecated(
"Deprecated because FieldNames operates on types and not values and refined focuses on refining values.",
"0.9.0"
)
final case class FieldNames[P](p: P)
@deprecated(
"The Subtype predicate is deprecated without replacement because it is lacking practical relevance.",
"0.9.0"
)
final case class Subtype[U]()
@deprecated(
"The Supertype predicate is deprecated without replacement because it is lacking practical relevance.",
"0.9.0"
)
final case class Supertype[U]()
object Equal {
implicit def equalValidate[T, U](implicit
wu: WitnessAs[U, T]
): Validate.Plain[T, Equal[U]] =
Validate.fromPredicate(_ == wu.snd, t => s"($t == ${wu.snd})", Equal(wu.fst))
}
@deprecated(
"Deprecated because ConstructorNames operates on types and not values and refined focuses on refining values.",
"0.9.0"
)
object ConstructorNames {
@deprecated(
"Deprecated because ConstructorNames operates on types and not values and refined focuses on refining values.",
"0.9.0"
)
implicit def ctorNamesValidate[T, R0 <: Coproduct, R1 <: HList, K <: HList, NP, NR](implicit
lg: LabelledGeneric.Aux[T, R0],
cthl: ToHList.Aux[R0, R1],
keys: Keys.Aux[R1, K],
ktl: ToList[K, Symbol],
v: Validate.Aux[List[String], NP, NR]
): Validate.Aux[T, ConstructorNames[NP], ConstructorNames[v.Res]] = {
val ctorNames = keys().toList.map(_.name)
val rn = v.validate(ctorNames)
Validate.constant(rn.as(ConstructorNames(rn)), v.showExpr(ctorNames))
}
}
@deprecated(
"Deprecated because FieldNames operates on types and not values and refined focuses on refining values.",
"0.9.0"
)
object FieldNames {
@deprecated(
"Deprecated because FieldNames operates on types and not values and refined focuses on refining values.",
"0.9.0"
)
implicit def fieldNamesValidate[T, R <: HList, K <: HList, NP, NR](implicit
lg: LabelledGeneric.Aux[T, R],
keys: Keys.Aux[R, K],
ktl: ToList[K, Symbol],
v: Validate.Aux[List[String], NP, NR]
): Validate.Aux[T, FieldNames[NP], FieldNames[v.Res]] = {
val fieldNames = keys().toList.map(_.name)
val rn = v.validate(fieldNames)
Validate.constant(rn.as(FieldNames(rn)), v.showExpr(fieldNames))
}
}
@deprecated(
"The Subtype predicate is deprecated without replacement because it is lacking practical relevance.",
"0.9.0"
)
object Subtype {
@deprecated(
"The Subtype predicate is deprecated without replacement because it is lacking practical relevance.",
"0.9.0"
)
implicit def subtypeValidate[T, U >: T]: Validate.Plain[T, Subtype[U]] =
Validate.alwaysPassed(Subtype())
}
@deprecated(
"The Supertype predicate is deprecated without replacement because it is lacking practical relevance.",
"0.9.0"
)
object Supertype {
@deprecated(
"The Supertype predicate is deprecated without replacement because it is lacking practical relevance.",
"0.9.0"
)
implicit def supertypeValidate[T, U <: T]: Validate.Plain[T, Supertype[U]] =
Validate.alwaysPassed(Supertype())
}
}
private[refined] trait GenericInference {
implicit def equalValidateInference[T, U, P](implicit
v: Validate[T, P],
wu: WitnessAs[U, T]
): Equal[U] ==> P =
Inference(v.isValid(wu.snd), s"equalValidateInference(${v.showExpr(wu.snd)})")
}