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

Misleading error message when trying to use _.into.transform() or _.to in a given Transformer definition #165

Closed
BenFradet opened this issue May 13, 2024 · 2 comments · Fixed by #167

Comments

@BenFradet
Copy link

I must be dense but the following:

  final case class A(a: Int)
  final case class B(b: Int)
  given Transformer[A, B] =
    _.into[B]
      .transform(Field.renamed(_.b, _.a))

gives me The path segment 'b' is not valid as it is not a field of a case class or an argument of a function @ B

What am I missing?

BenFradet added a commit to BenFradet/ducktape that referenced this issue May 13, 2024
@arainko
Copy link
Owner

arainko commented May 13, 2024

Thanks for the report!

So the problem here is that _.into[B].transform() (you can see that the code will also compile without specifying Field.renamed) basically loops on itself since it's declared with a Transformer[A, B] in scope so it resolves to that transformer, which will fail with a SO at runtime.

The error message is misleading in this case (underneath it looks for a Plan.BetweenProducts node to try and dive into but the node is actually Plan.UserDefined i.e. a transformation with a user-defined Transformer in scope which is not traversable with a path). There should be a way of making that error message actually friendly for the user, I'll try and tackle that.

As for a proper fix for your use case, when building out Transformer instances you should use Transformer.define[A, B].build(Field.renamed(_.a, _.b) - this construct is built NOT to loop on itself in presence of a toplevel transformer in scope.

tl;dr

final case class A(a: Int)
final case class B(b: Int)
given Transformer[A, B] = Transformer.define[A, B].build(Field.renamed(_.b, _.a))

@arainko arainko changed the title Field.renamed Misleading error message when trying to use _.into.transform() or _.to in a given Transformer definition May 13, 2024
@BenFradet
Copy link
Author

Thanks a lot 👍

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