@@ -34,7 +34,7 @@ sealed trait MongoFormat[T] {
3434 case union : MongoAdtFormat .UnionFormat [T ] => union
3535 case _ => throw new IllegalArgumentException (
3636 " Encountered a non-union MongoFormat for an union type (sealed hierarchy) -" +
37- " do you have any custom implicit MongoFormat for that type?"
37+ " do you have any custom implicit MongoFormat for that type?" ,
3838 )
3939 }
4040
@@ -44,51 +44,68 @@ sealed trait MongoFormat[T] {
4444 " Encountered a non-optional MongoFormat for an Option-like type - " +
4545 " do you have a custom implicit MongoFormat for that type?" )
4646 }
47+
48+ def assumeTransparent [R ]: MongoFormat .TransparentFormat [T , R ] = this match {
49+ case transparent : MongoFormat .TransparentFormat [T @ unchecked, R @ unchecked] => transparent
50+ case _ => throw new IllegalArgumentException (
51+ " Encountered a non-transparent MongoFormat for a transparent wrapper type - " +
52+ " do you have a custom implicit MongoFormat for that type?" )
53+ }
4754}
4855object MongoFormat extends MetadataCompanion [MongoFormat ] with MongoFormatLowPriority {
4956 final case class Opaque [T ](
50- codec : GenCodec [T ]
57+ codec : GenCodec [T ],
5158 ) extends MongoFormat [T ]
5259
5360 final case class CollectionFormat [C [X ] <: Iterable [X ], T ](
5461 codec : GenCodec [C [T ]],
55- elementFormat : MongoFormat [T ]
62+ elementFormat : MongoFormat [T ],
5663 ) extends MongoFormat [C [T ]]
5764
5865 final case class DictionaryFormat [M [X , Y ] <: BMap [X , Y ], K , V ](
5966 codec : GenCodec [M [K , V ]],
6067 keyCodec : GenKeyCodec [K ],
61- valueFormat : MongoFormat [V ]
68+ valueFormat : MongoFormat [V ],
6269 ) extends MongoFormat [M [K , V ]]
6370
6471 final case class TypedMapFormat [K [_]](
6572 codec : GenCodec [TypedMap [K ]],
6673 keyCodec : GenKeyCodec [K [_]],
67- valueFormats : MongoFormatMapping [K ]
74+ valueFormats : MongoFormatMapping [K ],
6875 ) extends MongoFormat [TypedMap [K ]]
6976
7077 final case class OptionalFormat [O , T ](
7178 codec : GenCodec [O ],
7279 optionLike : OptionLike .Aux [O , T ],
73- wrappedFormat : MongoFormat [T ]
80+ wrappedFormat : MongoFormat [T ],
7481 ) extends MongoFormat [O ]
7582
83+ final case class TransparentFormat [T , R ](
84+ codec : GenCodec [T ],
85+ wrapping : TransparentWrapping [R , T ],
86+ wrappedFormat : MongoFormat [R ],
87+ ) extends MongoFormat [T ]
88+
7689 implicit def collectionFormat [C [X ] <: Iterable [X ], T ](
77- implicit collectionCodec : GenCodec [C [T ]], elementFormat : MongoFormat [T ]
90+ implicit collectionCodec : GenCodec [C [T ]], elementFormat : MongoFormat [T ],
7891 ): MongoFormat [C [T ]] = CollectionFormat (collectionCodec, elementFormat)
7992
8093 implicit def dictionaryFormat [M [X , Y ] <: BMap [X , Y ], K , V ](
81- implicit mapCodec : GenCodec [M [K , V ]], keyCodec : GenKeyCodec [K ], valueFormat : MongoFormat [V ]
94+ implicit mapCodec : GenCodec [M [K , V ]], keyCodec : GenKeyCodec [K ], valueFormat : MongoFormat [V ],
8295 ): MongoFormat [M [K , V ]] = DictionaryFormat (mapCodec, keyCodec, valueFormat)
8396
8497 implicit def typedMapFormat [K [_]](
85- implicit keyCodec : GenKeyCodec [K [_]], valueFormats : MongoFormatMapping [K ]
98+ implicit keyCodec : GenKeyCodec [K [_]], valueFormats : MongoFormatMapping [K ],
8699 ): MongoFormat [TypedMap [K ]] = TypedMapFormat [K ](TypedMap .typedMapCodec, keyCodec, valueFormats)
87100
88101 implicit def optionalFormat [O , T ](
89- implicit optionLike : OptionLike .Aux [O , T ], optionCodec : GenCodec [O ], wrappedFormat : MongoFormat [T ]
102+ implicit optionLike : OptionLike .Aux [O , T ], optionCodec : GenCodec [O ], wrappedFormat : MongoFormat [T ],
90103 ): MongoFormat [O ] = OptionalFormat (optionCodec, optionLike, wrappedFormat)
91104
105+ implicit def transparentFormat [R , T ](
106+ implicit codec : GenCodec [T ], wrapping : TransparentWrapping [R , T ], wrappedFormat : MongoFormat [R ],
107+ ): MongoFormat [T ] = TransparentFormat (codec, wrapping, wrappedFormat)
108+
92109 implicit class collectionFormatOps [C [X ] <: Iterable [X ], T ](private val format : MongoFormat [C [T ]]) extends AnyVal {
93110 def assumeCollection : CollectionFormat [C , T ] = format match {
94111 case coll : CollectionFormat [C @ unchecked, T @ unchecked] => coll
@@ -134,7 +151,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
134151 @ infer val codec : GenObjectCodec [T ],
135152 @ infer val dataClassTag : ClassTag [T ],
136153 @ reifyAnnot val flattenAnnot : flatten,
137- @ multi @ adtCaseMetadata val cases : List [Case [_]]
154+ @ multi @ adtCaseMetadata val cases : List [Case [_]],
138155 ) extends MongoAdtFormat [T ] {
139156
140157 lazy val casesByClass : Map [Class [_], Case [_]] =
@@ -163,7 +180,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
163180 @ tailrec def loop (cases : List [Case [_]], rawName : Opt [String ]): Unit = cases match {
164181 case cse :: tail =>
165182 val field = cse.getField(scalaFieldName).getOrElse(throw new NoSuchElementException (
166- s " Field $scalaFieldName not found in at least one case class/object. "
183+ s " Field $scalaFieldName not found in at least one case class/object. " ,
167184 ))
168185 if (rawName.exists(_ != field.info.rawName)) {
169186 throw new IllegalArgumentException (s " Field $scalaFieldName has different raw name across case classes " )
@@ -207,7 +224,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
207224 @ positioned(positioned.here)
208225 final class RecordFormat [T ](
209226 @ composite val record : RecordCase [T ],
210- @ infer val codec : GenObjectCodec [T ]
227+ @ infer val codec : GenObjectCodec [T ],
211228 ) extends MongoAdtFormat [T ] {
212229 def dataClassTag : ClassTag [T ] = record.classTag
213230
@@ -218,7 +235,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
218235 @ positioned(positioned.here)
219236 final class SingletonFormat [T ](
220237 @ composite val singleton : SingletonCase [T ],
221- @ infer val codec : GenObjectCodec [T ]
238+ @ infer val codec : GenObjectCodec [T ],
222239 ) extends MongoAdtFormat [T ] {
223240 def dataClassTag : ClassTag [T ] = singleton.classTag
224241
@@ -242,7 +259,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
242259 @ composite val info : GenCaseInfo [T ],
243260 @ infer val classTag : ClassTag [T ],
244261 @ multi @ adtParamMetadata val fields : List [Field [_]],
245- @ multi @ adtCaseSealedParentMetadata val sealedParents : List [SealedParent [_]]
262+ @ multi @ adtCaseSealedParentMetadata val sealedParents : List [SealedParent [_]],
246263 ) extends Case [T ] {
247264 def asAdtFormat (codec : GenObjectCodec [T ]): MongoAdtFormat [T ] =
248265 new RecordFormat (this , codec)
@@ -258,7 +275,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
258275
259276 def fieldRefFor [E , T0 ](prefix : MongoRef [E , T ], scalaFieldName : String ): MongoPropertyRef [E , T0 ] = {
260277 val field = fieldsByScalaName.getOrElse(scalaFieldName,
261- throw new NoSuchElementException (s " Field $scalaFieldName not found " )
278+ throw new NoSuchElementException (s " Field $scalaFieldName not found " ),
262279 ).asInstanceOf [MongoAdtFormat .Field [T0 ]]
263280 prefix match {
264281 case fieldRef : MongoRef .FieldRef [E , _, T ] if transparentWrapper =>
@@ -274,7 +291,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
274291 @ composite val info : GenCaseInfo [T ],
275292 @ infer val classTag : ClassTag [T ],
276293 @ multi @ adtCaseSealedParentMetadata val sealedParents : List [SealedParent [_]],
277- @ infer @ checked val value : ValueOf [T ]
294+ @ infer @ checked val value : ValueOf [T ],
278295 ) extends Case [T ] {
279296 def asAdtFormat (codec : GenObjectCodec [T ]): MongoAdtFormat [T ] =
280297 new SingletonFormat (this , codec)
@@ -290,7 +307,7 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
290307 @ composite val info : GenParamInfo [T ],
291308 @ optional @ reifyDefaultValue defaultValue : Opt [DefaultValue [T ]],
292309 @ optional @ reifyAnnot whenAbsentAnnot : Opt [whenAbsent[T ]],
293- @ infer val format : MongoFormat .Lazy [T ]
310+ @ infer val format : MongoFormat .Lazy [T ],
294311 ) extends TypedMetadata [T ] {
295312 lazy val fallbackBson : Opt [BsonValue ] = {
296313 if (info.optional) Opt (BsonNull .VALUE )
@@ -301,13 +318,13 @@ object MongoAdtFormat extends AdtMetadataCompanion[MongoAdtFormat] {
301318
302319 final class SealedParent [T ](
303320 @ composite val info : GenUnionInfo [T ],
304- @ infer val classTag : ClassTag [T ]
321+ @ infer val classTag : ClassTag [T ],
305322 ) extends TypedMetadata [T ]
306323}
307324
308325final class MongoEntityMeta [E <: BaseMongoEntity ](
309326 @ infer val format : MongoAdtFormat [E ],
310- @ infer val idMode : EntityIdMode [E , E # IDType ]
327+ @ infer val idMode : EntityIdMode [E , E # IDType ],
311328) {
312329 def idRef : MongoPropertyRef [E , E # IDType ] = idMode.idRef(format)
313330}
0 commit comments