-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
modules/generic/src/main/scala/vulcan/generic/AvroAlias.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2019-2025 OVO Energy Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package vulcan.generic | ||
|
||
import scala.annotation.StaticAnnotation | ||
|
||
/** | ||
* Annotation which can be used to include the record alias | ||
* in derived schemas. | ||
* | ||
* The annotation can be used in the following situations.<br> | ||
* - Annotate a type for enum alias when using | ||
* [[deriveEnum]].<br> | ||
* - Annotate a type for fixed alias when using | ||
* [[deriveFixed]].<br> | ||
* - Annotate a `case class` for record alias | ||
* when using `Codec.derive` from the generic module.<br> | ||
* - Annotate a `case class` parameter for record field | ||
* alias when using `Codec.derive` from the | ||
* generic module. | ||
*/ | ||
final class AvroAlias(final val alias: String) extends StaticAnnotation { | ||
override final def toString: String = | ||
s"AvroAlias($alias)" | ||
} | ||
|
||
private[vulcan] object AvroAlias { | ||
final def unapply(avroAlias: AvroAlias): Some[String] = | ||
Some(avroAlias.alias) | ||
} |
34 changes: 34 additions & 0 deletions
34
modules/generic/src/test/scala/vulcan/generic/AvroAliasSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2019-2024 OVO Energy Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package vulcan.generic | ||
|
||
import vulcan.BaseSpec | ||
|
||
final class AvroAliasSpec extends BaseSpec { | ||
describe("AvroAlias") { | ||
it("should provide alias via alias") { | ||
forAll { (s: String) => | ||
assert(new AvroAlias(s).alias == s) | ||
} | ||
} | ||
|
||
it("should include alias in toString") { | ||
forAll { (s: String) => | ||
assert(new AvroAlias(s).toString.contains(s)) | ||
} | ||
} | ||
|
||
it("should provide an extractor for alias") { | ||
forAll { (s1: String) => | ||
assert(new AvroAlias(s1) match { | ||
case AvroAlias(`s1`) => true | ||
case AvroAlias(s2) => fail(s2) | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
modules/generic/src/test/scala/vulcan/generic/examples/CaseClassAvroAlias.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2019-2024 OVO Energy Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package vulcan.generic.examples | ||
|
||
import vulcan.Codec | ||
import vulcan.generic._ | ||
|
||
@AvroAlias("CaseClassOtherAlias") | ||
final case class CaseClassAvroAlias( | ||
@AvroAlias("otherValueAlias") | ||
value: Option[String] | ||
) | ||
|
||
object CaseClassAvroAlias { | ||
implicit val codec: Codec[CaseClassAvroAlias] = Codec.derive | ||
} |
22 changes: 22 additions & 0 deletions
22
modules/generic/src/test/scala/vulcan/generic/examples/FixedAvroAlias.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2019-2024 OVO Energy Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package vulcan.generic.examples | ||
|
||
import vulcan.Codec | ||
import vulcan.generic._ | ||
|
||
@AvroAlias("FixedOtherAlias") | ||
final case class FixedAvroAlias(bytes: Array[Byte]) | ||
|
||
object FixedAvroAlias { | ||
implicit val codec: Codec[FixedAvroAlias] = | ||
deriveFixed( | ||
size = 1, | ||
encode = _.bytes, | ||
decode = bytes => Right(apply(bytes)) | ||
) | ||
} |