Holmusk / elm-street

:deciduous_tree: Crossing the road between Haskell and Elm
Mozilla Public License 2.0
88 stars 6 forks source link

Allow for customisation of the field names that are generated #129

Closed silky closed 1 year ago

silky commented 1 year ago

Following a conversation in https://github.com/Holmusk/elm-street/pull/20 it would be convenient to be able to customise the field-name-generation rules when emitting elm code.

In particular, the main culprit is the places this function - https://github.com/Holmusk/elm-street/blob/master/src/Elm/Generic.hs#L291- is used.

If that function could instead live in the Settings - https://hackage.haskell.org/package/elm-street-0.2.0.0/docs/Elm-Generate.html#t:Settings - then we'd get to customise that :)

jhrcek commented 1 year ago

Having the function in Settings is probably not a good idea, because settings are meant to be shared across multiple types potentially. We'd have to make this configurable on per-type basis.

turboMaCk commented 1 year ago

just a note from my side: I think with new record dot syntax this issue will become only more relevant since a need for current implicit prefix striping will become less relevant.

Also I second @jhrcek's opinion on allowing configuration per type. That IMO at least should be the underlying philosophy.

silky commented 1 year ago

it's a hack, but to avoid extreme configuration doom, note that the function already depends on the type name, so you could always check that as a string

silky commented 1 year ago

( fwiw, for my use case i'm happy with a change for all types )

jhrcek commented 1 year ago

@silky please check this PR https://github.com/Holmusk/elm-street/pull/131 and let us know if this change is sufficient for your purposes.

I wanted to keep the old behavior (stripping type name prefix) so as not to break dozens of instances our codebase at work uses. Yet to provide way to override that default behavior in a way that is not too hacky or inconvenient.

This screenshot from updated haddock tells you how you could use the new capability:

Screenshot from 2023-03-25 09-41-47

jhrcek commented 1 year ago

The abovementioned PR was merged and released as part of 0.2.1.1 release. See https://hackage.haskell.org/package/elm-street-0.2.1.1/docs/Elm-Generic.html#t:CodeGenOptions for example of how to use it