KacperFKorban / GUInep

Automatic UI forms for Scala 3 functions
https://kacperfkorban.github.io/GUInep/
Apache License 2.0
14 stars 0 forks source link

Use typeclasses for decoding input types #5

Open KacperFKorban opened 5 months ago

KacperFKorban commented 5 months ago

Add decoders for fields, so that we don't have to hardcode generation logic in the macro code

KacperFKorban commented 5 months ago

Now that I think about it. Returning Either[String, ...] doesn't make that much sense. There is no good feedback loop in the form. It would only be able to return the error when submitting the form, so it is no different than verifying the input in the function implementation. Unless of course, we compile the type classes separately to js functions using Scala.js and attach them to the generated site. I don't have any good ideas on how to do that though. (Other than running scala-cli during site generation, which seems like a HUGE hack)

KacperFKorban commented 5 months ago

A decent idea is to define a decoder as

trait Decoder[-I, O]:
  def decide(I: I): O

Then we can summon Decoder[Nothing, T] in the macro generated code. (If we can manage to get that to work) Or maybe we can make I a type member instead.

Then based on the I we can figure out the field type, probably.

Not sure how to handle dropdowns with this encoding though. Maybe the I type should only ever be an internal InputType? Or maybe just add a field to the type class that defines the input field type?

e.g.

trait Decoder[O]:
  type I <: InputType
  def decide(I: InType[I]): O

(This might have to be on the term level)

KacperFKorban commented 4 months ago

One implementation detail for implementing this is that the generated code should change the eithers into exceptions, apply the function to the arguments extracted from the eithers and optionally catch the exceptions.