Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add props annotation #623

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ package object generic {
case AvroName(newName) => newName
}

def getProps =
param.annotations
.collectFirst {
case AvroProps(props) => props
}

implicit val codec = param.typeclass

f(
Expand All @@ -101,7 +107,8 @@ package object generic {
if (codec.schema.exists(_.isNullable) && nullDefaultField)
Some(None.asInstanceOf[param.PType]) // TODO: remove cast
else None
)
),
props = getProps.getOrElse(Props.empty)
).widen
}
.map(caseClass.rawConstruct(_))
Expand Down
35 changes: 35 additions & 0 deletions modules/generic/src/main/scala/vulcan/generic/AvroProps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019-2024 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/

package vulcan.generic

import vulcan.Props
import scala.annotation.StaticAnnotation

/**
* Annotation which can be used to alter props
* in derived schemas.
*
* The annotation can be used in the following situations.<br>
* - Annotate a type for enum props when using
* [[deriveEnum]].<br>
* - Annotate a type for fixed props when using
* [[deriveFixed]].<br>
* - Annotate a `case class` for record props
* when using `Codec.derive` from the generic module.<br>
* - Annotate a `case class` parameter for record field
* props when using `Codec.derive` from the
* generic module.
*/
final class AvroProps(final val prop: Props) extends StaticAnnotation {
override final def toString: String =
s"AvroProps($prop)"
}

private[vulcan] object AvroProps {
final def unapply(avroProps: AvroProps): Some[Props] =
Some(avroProps.prop)
}
35 changes: 35 additions & 0 deletions modules/generic/src/test/scala/vulcan/generic/AvroPropsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019-2024 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/

package vulcan.generic

import vulcan.{BaseSpec, Props}
final class AvroPropsSpec extends BaseSpec {
describe("AvroProps") {
it("should provide props via props") {
forAll { (name: String, value: String) =>
assert(new AvroProps(Props.one(name, value)).prop.toChain == Props.one(name, value).toChain)
}
}

it("should include name and value in props toString") {
forAll { (name: String, value: String) =>
val propsString = new AvroProps(Props.one(name, value)).toString
assert(propsString.contains(name) && propsString.contains(value))
}
}

it("should provide an extractor for props") {
forAll { (name: String, value: String) =>
assert(new AvroProps(Props.one(name, value)) match {
case AvroProps(props) =>
props.toChain.toOption.exists(_.exists { case (n, _) => n == name })
case _ => false
})
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ final class GenericDerivationCodecSpec extends CodecBase {
}
}

it("should support annotation for props") {
assertSchemaIs[CaseClassAvroProps] {
"""{"type":"record","name":"CaseClassAvroProps","namespace":"vulcan.generic.examples","fields":[{"name":"value","type":["null","string"],"prop1":"A","prop2":"B"}]}"""
}
}

it("should capture errors on invalid names") {
assertSchemaError[CaseClassFieldInvalidName] {
"""org.apache.avro.SchemaParseException: Illegal initial character: -value"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package vulcan.generic.examples

import vulcan.{Codec, Props}
import vulcan.generic._

@AvroName("CaseClassAvroProps")
final case class CaseClassAvroProps(
@AvroName("value")
@AvroProps(Props.one("prop1", "A").add("prop2", "B"))
value: Option[String]
)

object CaseClassAvroProps {
implicit val codec: Codec[CaseClassAvroProps] = Codec.derive
}