-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathDataClassCodec.kt
More file actions
276 lines (252 loc) · 11.5 KB
/
DataClassCodec.kt
File metadata and controls
276 lines (252 loc) · 11.5 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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 org.bson.codecs.kotlin
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import kotlin.reflect.KClass
import kotlin.reflect.KClassifier
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.KProperty1
import kotlin.reflect.KTypeParameter
import kotlin.reflect.KTypeProjection
import kotlin.reflect.full.createType
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.findAnnotations
import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.jvmErasure
import org.bson.BsonReader
import org.bson.BsonType
import org.bson.BsonWriter
import org.bson.codecs.Codec
import org.bson.codecs.DecoderContext
import org.bson.codecs.EncoderContext
import org.bson.codecs.RepresentationConfigurable
import org.bson.codecs.configuration.CodecConfigurationException
import org.bson.codecs.configuration.CodecRegistry
import org.bson.codecs.pojo.annotations.BsonCreator
import org.bson.codecs.pojo.annotations.BsonDiscriminator
import org.bson.codecs.pojo.annotations.BsonExtraElements
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.codecs.pojo.annotations.BsonIgnore
import org.bson.codecs.pojo.annotations.BsonProperty
import org.bson.codecs.pojo.annotations.BsonRepresentation
import org.bson.diagnostics.Loggers
internal data class DataClassCodec<T : Any>(
private val kClass: KClass<T>,
private val primaryConstructor: KFunction<T>,
private val propertyModels: List<PropertyModel>,
) : Codec<T> {
private val fieldNamePropertyModelMap = propertyModels.associateBy { it.fieldName }
private val propertyModelId: PropertyModel? = fieldNamePropertyModelMap[idFieldName]
data class PropertyModel(val param: KParameter, val fieldName: String, val codec: Codec<Any>)
override fun encode(writer: BsonWriter, value: T, encoderContext: EncoderContext) {
writer.writeStartDocument()
if (propertyModelId != null) {
encodeProperty(propertyModelId, value, writer, encoderContext)
}
propertyModels
.filter { it != propertyModelId }
.forEach { propertyModel -> encodeProperty(propertyModel, value, writer, encoderContext) }
writer.writeEndDocument()
}
override fun getEncoderClass(): Class<T> = kClass.java
@Suppress("TooGenericExceptionCaught")
override fun decode(reader: BsonReader, decoderContext: DecoderContext): T {
val args: MutableMap<KParameter, Any?> = mutableMapOf()
reader.readStartDocument()
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
val fieldName = reader.readName()
val propertyModel = fieldNamePropertyModelMap[fieldName]
if (propertyModel == null) {
reader.skipValue()
if (logger.isTraceEnabled) {
logger.trace("Found property not present in the DataClass: $fieldName")
}
} else if (propertyModel.param.type.isMarkedNullable && reader.currentBsonType == BsonType.NULL) {
reader.readNull()
args[propertyModel.param] = null
} else {
try {
args[propertyModel.param] = decoderContext.decodeWithChildContext(propertyModel.codec, reader)
} catch (e: Exception) {
throw CodecConfigurationException(
"Unable to decode $fieldName for ${kClass.simpleName} data class.", e)
}
}
}
reader.readEndDocument()
// For non-optional parameters missing from the document, fail with a clear message
// if non-nullable, or pass null explicitly if nullable.
// Optional parameters (with defaults) are left absent so callBy uses the default value.
fieldNamePropertyModelMap.values.forEach {
if (it.param !in args && !it.param.isOptional) {
if (!it.param.type.isMarkedNullable && it.param.type.classifier is KClass<*>) {
throw CodecConfigurationException(
"Required field '${it.fieldName}' is missing from the document for ${kClass.simpleName} data class")
}
args[it.param] = null
}
}
try {
return primaryConstructor.callBy(args)
} catch (e: Exception) {
throw CodecConfigurationException(
"Unable to invoke primary constructor of ${kClass.simpleName} data class", e)
}
}
@Suppress("UNCHECKED_CAST")
private fun <T : Any> encodeProperty(
propertyModel: PropertyModel,
value: T,
writer: BsonWriter,
encoderContext: EncoderContext
) {
value::class
.members
.firstOrNull { it.name == propertyModel.param.name }
?.let {
val propertyValue = (it as KProperty1<Any, *>).get(value)
propertyValue?.let { pValue ->
writer.writeName(propertyModel.fieldName)
encoderContext.encodeWithChildContext(propertyModel.codec, writer, pValue)
}
}
}
companion object {
internal val logger = Loggers.getLogger("DataClassCodec")
private const val idFieldName = "_id"
internal fun <R : Any> create(
kClass: KClass<R>,
codecRegistry: CodecRegistry,
types: List<Type> = emptyList()
): Codec<R>? {
return if (kClass.isData) {
validateAnnotations(kClass)
val primaryConstructor =
kClass.primaryConstructor ?: throw CodecConfigurationException("No primary constructor for $kClass")
val typeMap =
types
.mapIndexed { i, k -> primaryConstructor.typeParameters[i].createType().classifier!! to k }
.toMap()
val propertyModels =
primaryConstructor.parameters.map { kParameter ->
PropertyModel(
kParameter, computeFieldName(kParameter), getCodec(kParameter, typeMap, codecRegistry))
}
return DataClassCodec(kClass, primaryConstructor, propertyModels)
} else {
null
}
}
private fun <R : Any> validateAnnotations(kClass: KClass<R>) {
codecConfigurationRequires(kClass.findAnnotation<BsonDiscriminator>() == null) {
"""Annotation 'BsonDiscriminator' is not supported on kotlin data classes,
| but found on ${kClass.simpleName}."""
.trimMargin()
}
codecConfigurationRequires(kClass.constructors.all { it.findAnnotations<BsonCreator>().isEmpty() }) {
"""Annotation 'BsonCreator' is not supported on kotlin data classes,
| but found in ${kClass.simpleName}."""
.trimMargin()
}
kClass.primaryConstructor?.parameters?.map { param ->
codecConfigurationRequires(param.findAnnotations<BsonIgnore>().isEmpty()) {
"""Annotation 'BsonIgnore' is not supported in kotlin data classes,
| found on the parameter for ${param.name}."""
.trimMargin()
}
codecConfigurationRequires(param.findAnnotations<BsonExtraElements>().isEmpty()) {
"""Annotation 'BsonExtraElements' is not supported in kotlin data classes,
| found on the parameter for ${param.name}."""
.trimMargin()
}
}
}
private fun computeFieldName(parameter: KParameter): String {
return if (parameter.hasAnnotation<BsonId>()) {
idFieldName
} else {
parameter.findAnnotation<BsonProperty>()?.value ?: requireNotNull(parameter.name)
}
}
@Suppress("UNCHECKED_CAST")
private fun getCodec(
kParameter: KParameter,
typeMap: Map<KClassifier, Type>,
codecRegistry: CodecRegistry
): Codec<Any> {
return when (kParameter.type.classifier) {
is KClass<*> -> {
codecRegistry.getCodec(
kParameter,
(kParameter.type.classifier as KClass<Any>).javaObjectType,
kParameter.type.arguments
.mapNotNull { typeMap[it.type?.classifier] ?: computeJavaType(it) }
.toList())
}
is KTypeParameter -> {
when (val pType = typeMap[kParameter.type.classifier] ?: kParameter.type.javaType) {
is Class<*> ->
codecRegistry.getCodec(kParameter, (pType as Class<Any>).kotlin.java, emptyList())
is ParameterizedType ->
codecRegistry.getCodec(
kParameter,
(pType.rawType as Class<Any>).kotlin.javaObjectType,
pType.actualTypeArguments.toList())
else -> null
}
}
else -> null
}
?: throw CodecConfigurationException(
"Could not find codec for ${kParameter.name} with type ${kParameter.type}")
}
private fun computeJavaType(kTypeProjection: KTypeProjection): Type? {
val javaType: Type = kTypeProjection.type?.javaType!!
return if (javaType == Any::class.java) {
kTypeProjection.type?.jvmErasure?.javaObjectType
} else javaType
}
@Suppress("UNCHECKED_CAST")
private fun CodecRegistry.getCodec(kParameter: KParameter, clazz: Class<Any>, types: List<Type>): Codec<Any> {
val codec =
if (clazz.isArray) {
ArrayCodec.create(clazz.kotlin, types, this)
} else if (types.isEmpty()) {
this.get(clazz)
} else {
this.get(clazz, types)
}
return kParameter.findAnnotation<BsonRepresentation>()?.let {
if (codec !is RepresentationConfigurable<*>) {
throw CodecConfigurationException(
"Codec for `${kParameter.name}` must implement RepresentationConfigurable" +
" to supportBsonRepresentation")
}
codec.withRepresentation(it.value) as Codec<Any>
}
?: codec
}
private fun codecConfigurationRequires(value: Boolean, lazyMessage: () -> String) {
if (!value) {
throw CodecConfigurationException(lazyMessage.invoke())
}
}
}
}