@@ -4,7 +4,7 @@ package rest
44import com .avsystem .commons .meta .MacroInstances
55import com .avsystem .commons .misc .{AbstractValueEnumCompanion , ValueEnum , ValueOf }
66import com .avsystem .commons .rpc .{AsRaw , AsReal }
7- import com .avsystem .commons .serialization .{GenCodec , TransparentWrapperCompanion }
7+ import com .avsystem .commons .serialization .{GenCodec , TransparentWrapperCompanion , TransparentWrapping }
88import io .udash .rest .openapi .*
99import io .udash .rest .openapi .RestStructure .NameAndAdjusters
1010import io .udash .rest .raw .{HttpBody , JsonValue , PlainValue , RestResponse , StreamedBody , StreamedRestResponse }
@@ -47,28 +47,18 @@ abstract class RestDataCompanionWithDeps[D, T](implicit
4747) extends AbstractRestDataCompanion [(DefaultRestImplicits , D ), T ]((DefaultRestImplicits , deps.value))
4848
4949/**
50- * Base class for companion objects of wrappers over other data types (i.e. case classes with single field).
51- * This companion ensures instances of all the REST typeclasses (serialization, schema, etc.) for wrapping type
52- * assuming that these instances are available for the wrapped type.
53- *
54- * Using this base companion class makes the wrapper class effectively "transparent", i.e. as if it was annotated with
55- * [[com.avsystem.commons.serialization.transparent transparent ]] annotation.
50+ * These implicits must be specialized for every raw type (PlainValue, JsonValue, etc.) because
51+ * it lifts their priority. Unfortunately, controlling implicit priority is not pretty.
52+ * Also, it's probably good that we explicitly enable derivation only for REST-related raw types
53+ * and not for all raw types - this avoids possible interference with other features using RPC.
5654 *
57- * @example
58- * {{{
59- * case class UserId(id: String) extends AnyVal
60- * object UserId extends RestDataWrapperCompanion[String, UserId]
61- * }}}
55+ * Seperated from [[RestDataWrapperCompanion ]] to allow creating custom companion wrappers.
6256 */
63- abstract class RestDataWrapperCompanion [Wrapped , T ](implicit
64- instances : MacroInstances [DefaultRestImplicits , () => NameAndAdjusters [T ]]
65- ) extends TransparentWrapperCompanion [Wrapped , T ] {
66- private def nameAndAdjusters : NameAndAdjusters [T ] = instances(DefaultRestImplicits , this ).apply()
57+ trait RestDataWrapperImplicits [Wrapped , T ] {
58+ protected def nameAndAdjusters : NameAndAdjusters [T ]
59+ protected def wrapping : TransparentWrapping [Wrapped , T ]
6760
68- // These implicits must be specialized for every raw type (PlainValue, JsonValue, etc.) because
69- // it lifts their priority. Unfortunately, controlling implicit priority is not pretty.
70- // Also, it's probably good that we explicitly enable derivation only for REST-related raw types
71- // and not for all raw types - this avoids possible interference with other features using RPC.
61+ private implicit def wrappingAsImplicit : TransparentWrapping [Wrapped , T ] = wrapping
7262
7363 implicit def plainAsRaw (implicit wrappedAsRaw : AsRaw [PlainValue , Wrapped ]): AsRaw [PlainValue , T ] =
7464 AsRaw .fromTransparentWrapping
@@ -122,6 +112,27 @@ abstract class RestDataWrapperCompanion[Wrapped, T](implicit
122112 wrappedResponses.responses(resolver, ws => schemaTransform(nameAndAdjusters.restSchema(ws)))
123113}
124114
115+ /**
116+ * Base class for companion objects of wrappers over other data types (i.e. case classes with single field).
117+ * This companion ensures instances of all the REST typeclasses (serialization, schema, etc.) for wrapping type
118+ * assuming that these instances are available for the wrapped type.
119+ *
120+ * Using this base companion class makes the wrapper class effectively "transparent", i.e. as if it was annotated with
121+ * [[com.avsystem.commons.serialization.transparent transparent ]] annotation.
122+ *
123+ * @example
124+ * {{{
125+ * case class UserId(id: String) extends AnyVal
126+ * object UserId extends RestDataWrapperCompanion[String, UserId]
127+ * }}}
128+ */
129+ abstract class RestDataWrapperCompanion [Wrapped , T ](implicit
130+ instances : MacroInstances [DefaultRestImplicits , () => NameAndAdjusters [T ]]
131+ ) extends TransparentWrapperCompanion [Wrapped , T ] with RestDataWrapperImplicits [Wrapped , T ] {
132+ override protected def nameAndAdjusters : NameAndAdjusters [T ] = instances(DefaultRestImplicits , this ).apply()
133+ override protected def wrapping : TransparentWrapping [Wrapped , T ] = this
134+ }
135+
125136/**
126137 * Base class for companion objects of enum types [[ValueEnum ]] which are used as
127138 * parameter or result types in REST API traits. Automatically provides instance of
0 commit comments