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

Annotation to override DbCodec and SqlArrayCodec typeclasses #43

Open
AugustNagro opened this issue Oct 1, 2024 · 1 comment
Open

Comments

@AugustNagro
Copy link
Owner

It's inconvenient to derive a DbCodec when two fields of the same type require different codecs, because the imports conflict. One approach to fix this would be implementing a DbCodecOverride and SqlArrayCodecOverride annotation, like:

/** This annotation allows the user to explicitly select the DbCodec used on a field.
 * It's useful if you have two fields of the same type that require different codecs. For example,
 * assume we have two LongCodecs:
 * {{{
 *   val codecA: DbCodec[Long] = ???
 *   val codecB: DbCodec[Long] = ???
 *
 *   case class MyUser(a: Long, b: Long) derives DbCodec
 * }}}
 *
 * And `codecA` should be used for field `a`, and `codecB` for field b.
 * The normal approach of importing both codecs won't work, since they have the same type and will conflict.
 * Instead, we can do
 *
 * {{{
 *   case class MyUser(
 *     @DbCodecOverride(codecA) a: Long,
 *     @DbCodecOverride(codecB) b: Long
 *   ) derives DbCodec
 * }}}
 *
 * */
class DbCodecOverride[A](val codec: DbCodec[A]) extends StaticAnnotation

#36 (comment)

@Nexus6
Copy link

Nexus6 commented Oct 27, 2024

This looks like a workable solution. I personally would prefer a more succinct (and less dramatic) name for the annotation. Something like @DbColumn(codecA) maybe. "Override" suggests that you (the library user) are doing something really weird/crazy/out-of-the-ordinary/dangerous. But the annotation is merely for the purposes of selecting a particular codec when several are available.

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

No branches or pull requests

2 participants