Skip to content

Commit efe0662

Browse files
committed
feat: migration to context parameters
- Upgraded to mongokt-0.2.0-beta.0 ### module-ped-core - Deleted class CodecClass - Deleted interface NullableCodec and all its extensions and overloads - Deleted interface NullableFieldCodec and all its extensions and overloads - deprecated function tryInlineCodecAny - deprecated function tryInlineCodecAnyCatching - deprecated function inlineCodecAny - deprecated function inlineCodecAnyCatching - deprecated function inlineCodec - deprecated function inlineCodecCatching - deprecated function encodeAny - deprecated function encodeAnyCatching - deprecated function decodeAny - deprecated function decodeAnyCatching - deprecated function tryEncodeAny - deprecated functions encodeAny - deprecated function tryDecodeAny - deprecated functions decodeAny - moved class EnumCodec from ped-bson to ped-core - added function Throwable.toCodecException - deprecated functions lang - deprecated function dot ### module-ped-bson - Changed interface BsonFieldCodec into typealias and removed useless extensions - Deleted interface BsonNullableFieldCodec and its extensions - Added typealias BsonEnumCodec - added function MutableBsonDocumentLike.set(...) - added function BsonFieldCodec.by(...)
1 parent 8e716d6 commit efe0662

19 files changed

Lines changed: 200 additions & 1026 deletions

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kotlin = "2.2.0"
33
kotlinx-serialization = "1.8.1"
44

55
mongodb = "5.5.1"
6-
mongokt = "0.1.6"
6+
mongokt = "0.2.0-beta.0"
77

88
[libraries]
99
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }

module-ped-bson/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77

88
kotlin {
99
compilerOptions {
10+
freeCompilerArgs.add("-Xcontext-parameters")
1011
optIn.add("kotlin.time.ExperimentalTime")
1112
}
1213
jvm()

module-ped-bson/src/commonMain/kotlin/BsonCodecs.kt

Lines changed: 48 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import kotlin.time.Instant
2323
/* ============= ------------------ ============= */
2424

2525
typealias 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
*/
8080
class 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
*/
131133
object 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
*/
150145
object 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
*/
169157
object 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
*/
188169
object 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
*/
207181
object 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
*/
226193
object 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
*/
243203
object 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
*/
262215
object 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
*/
281227
object 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
*/
303242
object 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-
/* ============= ------------------ ============= */

module-ped-bson/src/commonMain/kotlin/BsonExtensions.kt

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,29 @@ package org.cufy.ped
1717

1818
import org.cufy.bson.*
1919

20-
/**
21-
* Return a field codec derived from this one with
22-
* its name tagged with the given language [tag].
23-
*/
24-
@PEDMarker3
25-
@DeprecatedWithContextParameters
26-
infix fun <I> BsonFieldCodec<I>.lang(tag: String): BsonFieldCodec<I> {
27-
if (tag.isEmpty()) return this
28-
return FieldCodec("$name#$tag", this)
29-
}
30-
31-
/**
32-
* Return a field codec derived from this one with
33-
* its name tagged with the given language [tag].
34-
*/
35-
@PEDMarker3
36-
@DeprecatedWithContextParameters
37-
infix fun <I> BsonNullableFieldCodec<I>.lang(tag: String): BsonNullableFieldCodec<I> {
38-
if (tag.isEmpty()) return this
39-
return FieldCodec("$name#$tag", this)
40-
}
41-
4220
/**
4321
* Create an instance [I] from first constructing a [BsonDocument] with
4422
* the given [block] then decoding it with [this] codec.
4523
*/
4624
inline operator fun <I> BsonCodec<I>.invoke(block: BsonDocumentBlock): I {
47-
return BsonDocument(block) decode this
25+
return decode(BsonDocument(block)).getOrElse { throw it.toCodecException() }
4826
}
4927

5028
/**
5129
* Get the value of the field with the name of the
5230
* given [codec] and decode it using the given [codec].
5331
*/
54-
operator fun <I> BsonDocumentLike.get(codec: FieldCodec<I, BsonElement>): I {
55-
return this[codec.name] decodeAny codec
32+
operator fun <I> BsonDocumentLike.get(codec: BsonFieldCodec<I>): I {
33+
val element = this[codec.name]
34+
return codec.decode(element).getOrElse { throw it.toCodecException() }
35+
}
36+
37+
operator fun <I> MutableBsonDocumentLike.set(codec: BsonFieldCodec<I>, value: I) {
38+
val element = codec.encode(value).getOrElse { throw it.toCodecException() }
39+
put(codec.name, element)
40+
}
41+
42+
context(builder: BsonDocumentBuilder)
43+
infix fun <I> BsonFieldCodec<I>.by(value: I) {
44+
builder[this] = value
5645
}

0 commit comments

Comments
 (0)