Over the past two weeks my team and I have been able to use a row of types to generate all kinds of code to run well-typed forms in Halogen components with minimal boilerplate. It’s been working wonderfully, but now we have to turn to a new challenge: dynamic forms.
I’d like to use this same approach with dynamic forms, but there’s a hitch: we have to serialize and deserialize our form configurations, so we just store a data constructor representing what the type of the field ought to be instead of the literal PureScript type.
To briefly summarize my problem: I have a map of ids to data constructors like this:
data InputType
= TextField
| NumberField
myInputs :: Map Int InputType
myInputs = Map.fromFoldable [ Tuple 0 TextField, Tuple 1 NumberField ]
I want to write this function:
makeRow :: Map Int InputType -> ( ref0 :: String, ref1 :: Number )
makeRow = ?a
As to why, it’s because we’ve got all kinds of lovely code that can go from a row like that and generate all the various types and initial states and so on for forms running in Halogen components. And the reason I can’t just write the row in the first place is because we’re getting this information via JSON, it’s not in PureScript.
Previous discussions about generating code can be found here:
- https://github.com/citizennet/purescript-ocelot/pull/36#issuecomment-385132668
- Type class that has a Monoid instance seems to require all instances to also be monoids
- Generating Lenses from Variants
Is it possible to write this function? If so, I’d love to hear advice, and if not, I’d really love to hear advice.