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

Enhancement/2024 12 make encoder and decoder compatible with twiddles #348

Conversation

takapi327
Copy link
Owner

@takapi327 takapi327 commented Dec 30, 2024

Implementation Details

Modification of Encoder and Decoder into a composable form using twiddles.

The method of building custom-type Decoders has changed. With this modification, Decoder can be converted to any type using the map function.

- given Decoder.Elem[Continent] = Decoder.Elem.mapping[String, Continent](str => Continent.valueOf(str.replace(" ", "_")))
+ given Decoder[Continent] = Decoder[String].map(str => Continent.valueOf(str.replace(" ", "_")))

Decoder is still constructed implicitly.

case class City(id: Int, name: String, age: Int)


sql"SELECT id, name, age FROM city LIMIT 1"
  .query[City]
  .to[Option]
  .readOnly(conn)

However, implicit searches may fail if there are many properties in the model.

[error]    |Implicit search problem too large.
[error]    |an implicit search was terminated with failure after trying 100000 expressions.
[error]    |The root candidate for the search was:
[error]    |
[error]    |  given instance given_Decoder_P in object Decoder  for  ldbc.dsl.codec.Decoder[City]}

In such cases, raising the search limit in the compilation options may resolve the problem.

scalacOptions += "-Ximplicit-search-limit:100000"

However, it may lead to amplification of compilation time. In that case, it can also be resolved by manually building the Decoder as follows.

given Decoder[City] = (Decoder[Int] *: Decoder[String] *: Decoder[Int]).to[City]

This is true not only for Decoder but also for Encoder.

Pull Request Checklist

  • Wrote unit and integration tests
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code formatting by scalafmt (sbt scalafmtAll command execution)
  • Add copyright headers to new files

References

@takapi327 takapi327 added 💪 enhancement Functional enhancement 🔧 refactor Refactoring project:queryBuilder Addition and modification of functionality to Query Builder projects project:dsl Addition and modification of functionality to DSL projects project:schema Addition and modification of functionality to Schema projects project:statement Addition and modification of functionality to Statement projects labels Dec 30, 2024
@takapi327 takapi327 added this to the 0.3.0 milestone Dec 30, 2024
@takapi327 takapi327 self-assigned this Dec 30, 2024
@takapi327 takapi327 merged commit 485a6aa into master Dec 31, 2024
27 checks passed
@takapi327 takapi327 deleted the enhancement/2024-12-Make-Encoder-and-Decoder-compatible-with-Twiddles branch December 31, 2024 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 enhancement Functional enhancement project:dsl Addition and modification of functionality to DSL projects project:queryBuilder Addition and modification of functionality to Query Builder projects project:schema Addition and modification of functionality to Schema projects project:statement Addition and modification of functionality to Statement projects 🔧 refactor Refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant