Skip to content

Commit

Permalink
Merge branch 'master' into update/refined-0.11.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoub-benali authored Jan 27, 2025
2 parents d469b40 + ebb1c5c commit 2895fb3
Show file tree
Hide file tree
Showing 37 changed files with 191 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ val enumeratumVersion = "1.7.5"
val jacksonVersion = "2.18.2"
val magnolia2Version = "0.17.0"
val magnolia3Version = "1.3.8"
val munitVersion = "1.0.0"
val munitVersion = "1.1.0"
val refinedVersion = "0.11.3"
val scalaCollectionCompatVersion = "2.12.0"
val shapeless3Version = "3.4.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala-2/AvroDoc.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala-2/AvroNamespace.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala-2/vulcan/CodecCompat.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
11 changes: 10 additions & 1 deletion modules/core/src/main/scala-2/vulcan/internal/tags.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -18,6 +18,15 @@ private[vulcan] object tags {
doc.substring(1, doc.length - 1)
}

final def aliasFrom[A](tag: WeakTypeTag[A]): Seq[String] =
tag.tpe.typeSymbol.annotations.collectFirst {
case annotation
if annotation.tree.tpe.typeSymbol.fullName == "vulcan.AvroAlias" ||
annotation.tree.tpe.typeSymbol.fullName == "vulcan.generic.AvroAlias" =>
val doc = annotation.tree.children.last.toString
doc.substring(1, doc.length - 1)
}.toList

final def nameFrom[A](tag: WeakTypeTag[A]): String =
tag.tpe.typeSymbol.annotations
.collectFirst {
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala-3/vulcan/CodecCompat.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/Avro.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/AvroError.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/AvroException.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/Codec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/Prism.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/Props.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/internal/schema.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/internal/syntax.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
2 changes: 1 addition & 1 deletion modules/enumeratum/src/main/scala/enumeratum/Vulcan.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
16 changes: 12 additions & 4 deletions modules/generic/src/main/scala-2/vulcan/generic/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -68,7 +68,10 @@ package object generic {
.getOrElse(caseClass.typeName.owner),
doc = caseClass.annotations.collectFirst {
case AvroDoc(doc) => doc
}
},
aliases = caseClass.annotations.collectFirst {
case AvroAlias(alias) => alias
}.toList
) { (f: Codec.FieldBuilder[A]) =>
val nullDefaultBase = caseClass.annotations
.collectFirst { case AvroNullDefault(enabled) => enabled }
Expand Down Expand Up @@ -97,6 +100,9 @@ package object generic {
doc = param.annotations.collectFirst {
case AvroDoc(doc) => doc
},
aliases = param.annotations.collectFirst {
case AvroAlias(alias) => alias
}.toList,
default = param.default.orElse(
if (codec.schema.exists(_.isNullable) && nullDefaultField)
Some(None.asInstanceOf[param.PType]) // TODO: remove cast
Expand Down Expand Up @@ -149,7 +155,8 @@ package object generic {
encode = encode,
decode = decode,
namespace = namespaceFrom(tag),
doc = docFrom(tag)
doc = docFrom(tag),
aliases = aliasFrom(tag)
)

/**
Expand All @@ -170,6 +177,7 @@ package object generic {
encode = encode,
decode = decode,
namespace = namespaceFrom(tag),
doc = docFrom(tag)
doc = docFrom(tag),
aliases = aliasFrom(tag)
)
}
21 changes: 17 additions & 4 deletions modules/generic/src/main/scala-3/vulcan/generic/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -39,7 +39,10 @@ package object generic {
.getOrElse(caseClass.typeInfo.owner),
doc = caseClass.annotations.collectFirst {
case AvroDoc(doc) => doc
}
},
aliases = caseClass.annotations.collectFirst {
case AvroAlias(alias) => alias
}.toList
) { (f: Codec.FieldBuilder[A]) =>
val nullDefaultBase = caseClass.annotations
.collectFirst { case AvroNullDefault(enabled) => enabled }
Expand Down Expand Up @@ -68,6 +71,9 @@ package object generic {
doc = param.annotations.collectFirst {
case AvroDoc(doc) => doc
},
aliases = param.annotations.collectFirst {
case AvroAlias(alias) => alias
}.toList,
default = param.default.orElse(
Option.when(codec.schema.exists(_.isNullable) && nullDefaultField)(
None.asInstanceOf[param.PType] // TODO: remove cast
Expand Down Expand Up @@ -112,7 +118,8 @@ package object generic {
encode = encode,
decode = decode,
namespace = namespaceOf[A],
doc = docOf[A]
doc = docOf[A],
aliases = aliasOf[A]
)

/**
Expand All @@ -133,7 +140,8 @@ package object generic {
encode = encode,
decode = decode,
namespace = namespaceOf[A],
doc = docOf[A]
doc = docOf[A],
aliases = aliasOf[A]
)


Expand All @@ -151,4 +159,9 @@ package object generic {
case a: Annotation[AvroDoc, A] => Some(a().doc)
case _ => None
}

private inline def aliasOf[A]: Seq[String] = summonFrom {
case a: Annotation[AvroAlias, A] => Seq(a().alias)
case _ => Seq()
}
}
34 changes: 34 additions & 0 deletions modules/generic/src/main/scala/vulcan/generic/AvroAlias.scala
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)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 OVO Energy Limited
* Copyright 2019-2025 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
34 changes: 34 additions & 0 deletions modules/generic/src/test/scala/vulcan/generic/AvroAliasSpec.scala
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)
})
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ final class DerivationSpec extends BaseSpec with RoundtripHelpers with CodecSpec
}
}

it("should use alias annotation") {
assertSchemaIs[FixedAvroAlias] {
"""{"type":"fixed","name":"FixedAvroAlias","namespace":"vulcan.generic.examples","size":1,"aliases":["FixedOtherAlias"]}"""
}
}

it("should use namespace annotation") {
assertSchemaIs[FixedNamespace] {
"""{"type":"fixed","name":"FixedNamespace","namespace":"vulcan.generic.examples.overridden","doc":"Some documentation","size":1}"""
Expand Down
Loading

0 comments on commit 2895fb3

Please sign in to comment.