-
Notifications
You must be signed in to change notification settings - Fork 6
/
decoding.scala
167 lines (144 loc) · 4.93 KB
/
decoding.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2020 Fabio Labella
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dynosaur
package internal
import cats.{~>, Monoid, MonoidK}
import cats.syntax.all._
import alleycats.std.map._
import cats.free.FreeApplicative
import cats.data.{Chain, Kleisli}
import scodec.bits.ByteVector
import Schema.ReadError
import Schema.structure._
object decoding {
def fromSchema[A](s: Schema[A]): DynamoValue => Either[ReadError, A] =
s match {
case Identity => _.asRight
case Num => decodeNum
case Str => decodeString
case Bool => decodeBool
case Bytes => decodeBytes
case BytesSet => decodeBytesSet
case NumSet => decodeNumSet
case StrSet => decodeStrSet
case Nul => decodeNull
case Sequence(elem) => decodeSequence(elem, _)
case Dictionary(elem) => decodeDictionary(elem, _)
case Record(rec) => { value =>
// val here caches the traversal of the record
val cachedDecoder = decodeRecord(rec)
value.m
.toRight(ReadError(s"value ${value.toString()} is not a Dictionary"))
.flatMap(cachedDecoder)
}
case Sum(cases) => decodeSum(cases)
case Isos(iso) => decodeIsos(iso, _)
case Defer(schema) => schema().read
}
type Res[A] = Either[ReadError, A]
def decodeBool: DynamoValue => Res[Boolean] = { value =>
value.bool.toRight(ReadError(s"value ${value.toString()} is not a Boolean"))
}
def decodeNum: DynamoValue => Res[DynamoValue.Number] = { value =>
value.n.toRight(ReadError(s"value ${value.toString()} is not a Number"))
}
def decodeString: DynamoValue => Res[String] = { value =>
value.s.toRight(ReadError(s"value ${value.toString()} is not a String"))
}
def decodeBytes: DynamoValue => Res[ByteVector] = { value =>
value.b.toRight(ReadError(s"value ${value.toString()} is not a ByteVector"))
}
def decodeBytesSet: DynamoValue => Res[NonEmptySet[ByteVector]] = { value =>
value.bs.toRight(
ReadError(s"value ${value.toString()} is not a ByteVector Set")
)
}
def decodeNumSet: DynamoValue => Res[NonEmptySet[DynamoValue.Number]] = {
value =>
value.ns.toRight(
ReadError(s"value ${value.toString()} is not a Number Set")
)
}
def decodeStrSet: DynamoValue => Res[NonEmptySet[String]] = { value =>
value.ss.toRight(
ReadError(s"value ${value.toString()} is not a String Set")
)
}
def decodeNull: DynamoValue => Res[Unit] = { value =>
value.nul.toRight(
ReadError(s"value ${value.toString()} is not a Null")
)
}
def decodeSequence[V](
schema: Schema[V],
value: DynamoValue
): Res[List[V]] =
value.l
.toRight(ReadError(s"value ${value.toString()} is not a Sequence"))
.flatMap(_.traverse(schema.read))
def decodeDictionary[V](
schema: Schema[V],
value: DynamoValue
): Res[Map[String, V]] =
value.m
.toRight(ReadError(s"value ${value.toString()} is not a Dictionary"))
.flatMap(
_.map { case (k, v) => k -> v }
.traverse(schema.read)
)
def decodeRecord[R](
recordSchema: FreeApplicative[Field[R, *], R]
): Map[String, DynamoValue] => Res[R] = {
type Target[A] =
Kleisli[Either[ReadError, *], Map[String, DynamoValue], A]
recordSchema.foldMap {
new (Field[R, *] ~> Target) {
def apply[A](field: Field[R, A]) =
field match {
case Field.Required(name, elemSchema, _) =>
Kleisli { (v: Map[String, DynamoValue]) =>
v.get(name)
.toRight(
ReadError(s"required field $name does not contain a value")
)
.flatMap(v => elemSchema.read(v))
}
case Field.Optional(name, elemSchema, _) =>
Kleisli { (v: Map[String, DynamoValue]) =>
v
.get(name)
.traverse(v => elemSchema.read(v))
}
}
}
}.run
}
def decodeSum[A](cases: Chain[Alt[A]]): DynamoValue => Res[A] = {
implicit def orElse[T]: Monoid[Option[T]] =
MonoidK[Option].algebra
cases
.foldMap { alt => (v: DynamoValue) =>
alt.caseSchema.read(v).map(alt.prism.inject).toOption
}
.andThen(
_.toRight(ReadError("value doesn't match any of the alternatives"))
)
}
def decodeIsos[V](xmap: XMap[V], v: DynamoValue): Res[V] =
xmap.schema
.read(v)
.flatMap(xmap.r)
}