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

Utilities for extracting GenCodec field / type names #674

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sebaciv
Copy link
Contributor

@sebaciv sebaciv commented Feb 13, 2025

Utilities for extracting GenCodec field / type names

*
* @see [[com.avsystem.commons.serialization.GenCodecUtils.codecTypeName]]
*/
final class GencodecTypeName[T](val name: String)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenCodecTypeName

import com.avsystem.commons.annotation.explicitGenerics

/**
* Typeclass holding name of a type that will be used in [[GenCodec]] serialization
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: something like Typeclass holding a name used by GenCodec to serialize the T type. would be easier to understand for first time reader imho


implicit def materialize[T]: GencodecTypeName[T] =
macro com.avsystem.commons.macros.serialization.GenCodecUtilMacros.codecTypeName[T]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not gonna die on this hill, but I'm wondering if we even need the typeclass, the use case is pretty esoteric and the util covers it.

test("subtypes count leaf") {
GenCodecUtils.knownSubtypesCount[CaseOther] shouldBe 0
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to scaladoc:
@name annotation may be defined on any level of inheritance hierarchy. For instance, if a case class field overrides a method of some base trait, the @name annotation may be used on that method and will affect the case class field.`

We should definitely test that.


class GenCodecUtilsTest extends AnyFunSuite with Matchers {
import GenCodecUtilsTest.*

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change behaviour of GenCodec, but not the behaviour of this class, we may miss the discrepancy. Maybe we should write a custom output that will make it simple to compare if the name is also equal to what gencodec actually outputs.

import scala.annotation.tailrec
import scala.reflect.macros.blackbox

class GenCodecUtilMacros(ctx: blackbox.Context) extends CodecMacroCommons(ctx) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this macro could share some code with GenCodec macros for more intended coupling now that they are in the same module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants