@@ -23,6 +23,8 @@ import kotlin.time.Instant
2323/* ============= ------------------ ============= */
2424
2525typealias BsonCodec <I > = Codec <I , BsonElement >
26+ typealias BsonFieldCodec <I > = FieldCodec <I , BsonElement >
27+ typealias BsonEnumCodec <I > = EnumCodec <I , BsonElement >
2628
2729/* ============= ------------------ ============= */
2830
@@ -36,18 +38,16 @@ typealias BsonCodec<I> = Codec<I, BsonElement>
3638 * @author LSafer
3739 * @since 2.0.0
3840 */
39- class BsonNullableCodec <I >(val codec : BsonCodec <I >) : NullableCodec<I, BsonElement> {
40- override fun encode (value : Any? ) =
41- when (value) {
42- null -> success(BsonNull )
43- else -> codec.encode(value)
44- }
41+ class BsonNullableCodec <I >(val codec : BsonCodec <I >) : Codec<I?, BsonElement> {
42+ override fun encode (value : Any? ) = when (value) {
43+ null -> success(BsonNull )
44+ else -> codec.encode(value)
45+ }
4546
46- override fun decode (value : Any? ) =
47- when (value) {
48- null , BsonNull , BsonUndefined -> success(null )
49- else -> codec.decode(value)
50- }
47+ override fun decode (value : Any? ) = when (value) {
48+ null , BsonNull , BsonUndefined -> success(null )
49+ else -> codec.decode(value)
50+ }
5151}
5252
5353/* *
@@ -67,7 +67,7 @@ val <I> BsonCodec<I>.Nullable: BsonNullableCodec<I>
6767 *
6868 * Nullish values includes `null`, [BsonNull] and [BsonUndefined]
6969 */
70- val <I > FieldCodec <I , BsonElement >.Nullable : BsonNullableFieldCodec < I >
70+ val <I > FieldCodec <I , BsonElement >.Nullable : BsonFieldCodec < I ? >
7171 get() = FieldCodec (name, (this as BsonCodec <I >).Nullable )
7272
7373/* ============= ------------------ ============= */
@@ -78,21 +78,23 @@ val <I> FieldCodec<I, BsonElement>.Nullable: BsonNullableFieldCodec<I>
7878 * individual item.
7979 */
8080class BsonArrayCodec <I >(val codec : BsonCodec <I >) : BsonCodec<List<I>> {
81- override fun encode (value : Any? ) =
82- tryInlineCodec(value) { it : List < * > ->
83- success( BsonArray {
84- it.mapTo( this ) {
85- encodeAny(it, codec )
81+ override fun encode (value : Any? ) = tryInlineCodec(value) { it : List < * > ->
82+ success( BsonArray {
83+ it.mapTo(contextOf()) {
84+ codec.encode(it).getOrElse { e ->
85+ return @tryInlineCodec failure(e )
8686 }
87- })
88- }
87+ }
88+ })
89+ }
8990
90- override fun decode (value : Any? ) =
91- tryInlineCodec(value) { it: BsonArray ->
92- success(it.map {
93- decodeAny(it, codec)
94- })
95- }
91+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonArray ->
92+ success(it.map {
93+ codec.decode(it).getOrElse { e ->
94+ return @tryInlineCodec failure(e)
95+ }
96+ })
97+ }
9698}
9799
98100/* *
@@ -129,15 +131,8 @@ object BsonDocumentCodec : BsonCodec<BsonDocument> {
129131 * @since 2.0.0
130132 */
131133object BsonStringCodec : BsonCodec<String> {
132- override fun encode (value : Any? ) =
133- tryInlineCodec(value) { it: String ->
134- success(BsonString (it))
135- }
136-
137- override fun decode (value : Any? ) =
138- tryInlineCodec(value) { it: BsonString ->
139- success(it.value)
140- }
134+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: String -> success(BsonString (it)) }
135+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonString -> success(it.value) }
141136}
142137
143138/* ============= ------------------ ============= */
@@ -148,15 +143,8 @@ object BsonStringCodec : BsonCodec<String> {
148143 * @since 2.0.0
149144 */
150145object BsonBooleanCodec : BsonCodec<Boolean> {
151- override fun encode (value : Any? ) =
152- tryInlineCodec(value) { it: Boolean ->
153- success(BsonBoolean (it))
154- }
155-
156- override fun decode (value : Any? ) =
157- tryInlineCodec(value) { it: BsonBoolean ->
158- success(it.value)
159- }
146+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: Boolean -> success(BsonBoolean (it)) }
147+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonBoolean -> success(it.value) }
160148}
161149
162150/* ============= ------------------ ============= */
@@ -167,15 +155,8 @@ object BsonBooleanCodec : BsonCodec<Boolean> {
167155 * @since 2.0.0
168156 */
169157object BsonInt32Codec : BsonCodec<Int> {
170- override fun encode (value : Any? ) =
171- tryInlineCodec(value) { it: Int ->
172- success(BsonInt32 (it))
173- }
174-
175- override fun decode (value : Any? ) =
176- tryInlineCodec(value) { it: BsonNumber ->
177- success(it.toInt())
178- }
158+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: Int -> success(BsonInt32 (it)) }
159+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonNumber -> success(it.toInt()) }
179160}
180161
181162/* ============= ------------------ ============= */
@@ -186,15 +167,8 @@ object BsonInt32Codec : BsonCodec<Int> {
186167 * @since 2.0.0
187168 */
188169object BsonInt64Codec : BsonCodec<Long> {
189- override fun encode (value : Any? ) =
190- tryInlineCodec(value) { it: Long ->
191- success(BsonInt64 (it))
192- }
193-
194- override fun decode (value : Any? ) =
195- tryInlineCodec(value) { it: BsonNumber ->
196- success(it.toLong())
197- }
170+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: Long -> success(BsonInt64 (it)) }
171+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonNumber -> success(it.toLong()) }
198172}
199173
200174/* ============= ------------------ ============= */
@@ -205,15 +179,8 @@ object BsonInt64Codec : BsonCodec<Long> {
205179 * @since 2.0.0
206180 */
207181object BsonDoubleCodec : BsonCodec<Double> {
208- override fun encode (value : Any? ) =
209- tryInlineCodec(value) { it: Double ->
210- success(BsonDouble (it))
211- }
212-
213- override fun decode (value : Any? ) =
214- tryInlineCodec(value) { it: BsonNumber ->
215- success(it.toDouble())
216- }
182+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: Double -> success(BsonDouble (it)) }
183+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonNumber -> success(it.toDouble()) }
217184}
218185
219186/* ============= ------------------ ============= */
@@ -224,15 +191,8 @@ object BsonDoubleCodec : BsonCodec<Double> {
224191 * @since 2.0.0
225192 */
226193object BsonDecimal128Codec : BsonCodec<Decimal128> {
227- override fun encode (value : Any? ) =
228- tryInlineCodec(value) { it: Decimal128 ->
229- success(BsonDecimal128 (it))
230- }
231-
232- override fun decode (value : Any? ) =
233- tryInlineCodec(value) { it: BsonNumber ->
234- success(it.toDecimal128())
235- }
194+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: Decimal128 -> success(BsonDecimal128 (it)) }
195+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonNumber -> success(it.toDecimal128()) }
236196}
237197
238198/* *
@@ -241,15 +201,8 @@ object BsonDecimal128Codec : BsonCodec<Decimal128> {
241201 * @since 2.0.0
242202 */
243203object BsonDateTimeCodec : BsonCodec<Long> {
244- override fun encode (value : Any? ) =
245- tryInlineCodec(value) { it: Long ->
246- success(BsonDateTime (it))
247- }
248-
249- override fun decode (value : Any? ) =
250- tryInlineCodec(value) { it: BsonDateTime ->
251- success(it.value)
252- }
204+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: Long -> success(BsonDateTime (it)) }
205+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonDateTime -> success(it.value) }
253206}
254207
255208/* ============= ------------------ ============= */
@@ -260,15 +213,8 @@ object BsonDateTimeCodec : BsonCodec<Long> {
260213 * @since 2.0.0
261214 */
262215object BsonInstantCodec : BsonCodec<Instant> {
263- override fun encode (value : Any? ) =
264- tryInlineCodec(value) { it: Instant ->
265- success(BsonDateTime (it))
266- }
267-
268- override fun decode (value : Any? ) =
269- tryInlineCodec(value) { it: BsonDateTime ->
270- success(Instant .fromEpochMilliseconds(it.value))
271- }
216+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: Instant -> success(BsonDateTime (it)) }
217+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonDateTime -> success(it.toInstant()) }
272218}
273219
274220/* ============= ------------------ ============= */
@@ -279,15 +225,8 @@ object BsonInstantCodec : BsonCodec<Instant> {
279225 * @since 2.0.0
280226 */
281227object BsonObjectIdCodec : BsonCodec<ObjectId> {
282- override fun encode (value : Any? ) =
283- tryInlineCodec(value) { it: ObjectId ->
284- success(BsonObjectId (it))
285- }
286-
287- override fun decode (value : Any? ) =
288- tryInlineCodec(value) { it: BsonObjectId ->
289- success(it.value)
290- }
228+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: ObjectId -> success(BsonObjectId (it)) }
229+ override fun decode (value : Any? ) = tryInlineCodec(value) { it: BsonObjectId -> success(it.value) }
291230}
292231
293232/* ============= ------------------ ============= */
@@ -301,16 +240,12 @@ typealias BsonIdCodec = BsonIDCodec
301240 * @since 2.0.0
302241 */
303242object BsonIDCodec : BsonCodec<ID<Any?>> {
304- override fun encode (value : Any? ) =
305- tryInlineCodec(value) { it: ID <Any ?> ->
306- success(it.bson)
307- }
308-
243+ override fun encode (value : Any? ) = tryInlineCodec(value) { it: ID <Any ?> -> success(it.bson) }
309244 override fun decode (value : Any? ) =
310245 tryInlineCodec(value) { it: BsonElement ->
311246 when (it) {
312- is BsonObjectId -> success(ID < Any ?> (it.value))
313- is BsonString -> success(ID < Any ?> (it.value))
247+ is BsonObjectId -> success(AnyID (it.value))
248+ is BsonString -> success(AnyID (it.value))
314249 else -> failure(
315250 CodecException (
316251 " Cannot decode ${it::class } ; expected either " +
@@ -331,30 +266,3 @@ object BsonIDCodec : BsonCodec<ID<Any?>> {
331266}
332267
333268/* ============= ------------------ ============= */
334-
335- /* *
336- * A codec simplifying enum encoding.
337- */
338- class EnumCodec <I , O >(private val pairs : List <Pair <I , O >>) : Codec<I, O> {
339- constructor (vararg pairs: Pair <I , O >) : this (pairs.asList())
340-
341- override fun encode (value : Any? ): Result <O > {
342- return pairs.firstOrNull { it.first == value }.let {
343- when (it) {
344- null -> failure(CodecException (" Enum mismatch: $value " ))
345- else -> success(it.second)
346- }
347- }
348- }
349-
350- override fun decode (value : Any? ): Result <I > {
351- return pairs.firstOrNull { it.second == value }.let {
352- when (it) {
353- null -> failure(CodecException (" Enum mismatch: $value " ))
354- else -> success(it.first)
355- }
356- }
357- }
358- }
359-
360- /* ============= ------------------ ============= */
0 commit comments