16
16
17
17
package org.bson.codecs
18
18
19
+ import org.bson.BsonArray
20
+ import org.bson.BsonDateTime
19
21
import org.bson.BsonDocument
20
22
import org.bson.BsonDocumentReader
21
23
import org.bson.BsonDocumentWriter
24
+ import org.bson.codecs.jsr310.Jsr310CodecProvider
22
25
import org.bson.types.Binary
23
26
import spock.lang.Specification
24
27
import spock.lang.Unroll
25
28
29
+ import java.lang.reflect.ParameterizedType
30
+ import java.time.Instant
31
+
26
32
import static org.bson.BsonDocument.parse
27
33
import static org.bson.UuidRepresentation.C_SHARP_LEGACY
28
34
import static org.bson.UuidRepresentation.JAVA_LEGACY
@@ -36,7 +42,8 @@ import static org.bson.codecs.configuration.CodecRegistries.fromRegistries
36
42
class IterableCodecSpecification extends Specification {
37
43
38
44
static final REGISTRY = fromRegistries(fromCodecs(new UuidCodec (JAVA_LEGACY )),
39
- fromProviders(new ValueCodecProvider (), new DocumentCodecProvider (), new BsonValueCodecProvider (), new IterableCodecProvider ()))
45
+ fromProviders(new ValueCodecProvider (), new DocumentCodecProvider (), new BsonValueCodecProvider (),
46
+ new IterableCodecProvider (), new MapCodecProvider ()))
40
47
41
48
def ' should have Iterable encoding class' () {
42
49
given :
@@ -159,4 +166,44 @@ class IterableCodecSpecification extends Specification {
159
166
PYTHON_LEGACY | [new Binary ((byte ) 4 , [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ] as byte [])] | ' {"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
160
167
UNSPECIFIED | [new Binary ((byte ) 4 , [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ] as byte [])] | ' {"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
161
168
}
169
+
170
+ def ' should parameterize' () {
171
+ given :
172
+ def codec = new IterableCodec (REGISTRY , new BsonTypeClassMap ())
173
+ def writer = new BsonDocumentWriter (new BsonDocument ())
174
+ def reader = new BsonDocumentReader (writer. getDocument())
175
+ def instants = [
176
+ [' firstMap' : [Instant . ofEpochMilli(1 ), Instant . ofEpochMilli(2 )]],
177
+ [' secondMap' : [Instant . ofEpochMilli(3 ), Instant . ofEpochMilli(4 )]]]
178
+ when :
179
+ codec = codec. parameterize(fromProviders(new Jsr310CodecProvider (), REGISTRY ),
180
+ Arrays . asList(((ParameterizedType ) Container . getMethod(' getInstants' ). genericReturnType). actualTypeArguments))
181
+ writer. writeStartDocument()
182
+ writer. writeName(' instants' )
183
+ codec. encode(writer, instants, EncoderContext . builder(). build())
184
+ writer. writeEndDocument()
185
+
186
+ then :
187
+ writer. getDocument() == new BsonDocument ()
188
+ .append(' instants' , new BsonArray (List . of(
189
+ new BsonDocument (' firstMap' , new BsonArray (List . of(new BsonDateTime (1 ), new BsonDateTime (2 )))),
190
+ new BsonDocument (' secondMap' , new BsonArray (List . of(new BsonDateTime (3 ), new BsonDateTime (4 )))))))
191
+
192
+ when :
193
+ reader. readStartDocument()
194
+ reader. readName(' instants' )
195
+ def decodedInstants = codec. decode(reader, DecoderContext . builder(). build())
196
+
197
+ then :
198
+ decodedInstants == instants
199
+ }
200
+
201
+ @SuppressWarnings (' unused' )
202
+ static class Container {
203
+ private final List<Map<String , List<Instant > > > instants = []
204
+
205
+ List<Map<String , List<Instant > > > getInstants () {
206
+ instants
207
+ }
208
+ }
162
209
}
0 commit comments