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

Modifying field names in generated Haskell types #2262

Closed
mgoldenberg opened this issue Jul 23, 2021 · 6 comments · Fixed by #2285
Closed

Modifying field names in generated Haskell types #2262

mgoldenberg opened this issue Jul 23, 2021 · 6 comments · Fixed by #2285
Labels
marshalling Marshalling between Dhall and Haskell

Comments

@mgoldenberg
Copy link

I have a set of Dhall types which I recently started generating in Haskell with Dhall.TH.makeHaskellTypes. This is a really handy tool, but there are certain cases where the mechanism breaks down. For example, Dhall and Haskell don't have the same restrictions on naming. Given that the Haskell types are generated without modification from the Dhall types, one can run into naming issues which prevent proper Haskell code from being generated.

Consider the following (contrived) example. Given a Dhall file, Foo.dhall, with contents:

let Foo = { type : Natural } in Foo

when generating this type with

Dhall.TH.makeHaskellTypes [ Dhall.TH.SingleConstructor "Foo" "MakeFoo" "./Foo.dhall" ]

we get the following generated code

data Foo = MakeFoo { type :: GHC.Natural.Natural }

This code doesn't compile, of course, because type is a reserved word in Haskell. In some cases it may be easy to just rename the field to something that is a proper field name in Haskell; however, renaming a field in the Dhall type is not always straightforward if you're in later stages of a project and are already depending on a particular field name in other places prior to having generating the Haskell code.

I was wondering if there is an easy way to modify the field name that is generated in the Haskell code. If not, perhaps there could be another function for doing so. For instance, one could add a prefix to all fields generated, like so:

Dhall.TH.makeHaskellTypesWithFieldModifier 
    [ Dhall.TH.SingleConstructor "Foo" "MakeFoo" "./Foo.dhall" (\field -> "foo" ++ [toUpper . head $ field] ++ tail field) ]

which might generate

data Foo = MakeFoo { fooType : GHC.Natural.Natural }

Or perhaps instead of a single function, one passes a record that has all sorts of settings for how to generate the Haskell type.

Any thoughts or help would be greatly appreciated!

@sjakobi sjakobi added the marshalling Marshalling between Dhall and Haskell label Jul 26, 2021
@Gabriella439
Copy link
Collaborator

@mgoldenberg: Sorry for the delay in answering this!

What if the generated code could optionally omit the generated Generic / FromDhall / ToDhall instances? I believe if that were an option then you could provide your own instances, especially using the utilities from the Dhall.Deriving module. Would that work for you?

@mmhat
Copy link
Collaborator

mmhat commented Aug 17, 2021

IMHO something like aesons Options type or lens LensRules would be a nice fit for this. @Gabriel439 I could work on that next week.

@mgoldenberg
Copy link
Author

Thanks so much for the replies!

I had not noticed the Dhall.Deriving module and will look into that. I actually did have in mind what @mmhat is mentioning though, particularly because I am using that exact pattern with Aeson - it's really quite nice! Let me know if you all would consider implementing that, I'm not sure if there are other things that have higher priority.

@mgoldenberg
Copy link
Author

@Gabriel439, I had a chance to look at Dhall.Deriving and the utilities there would be great. It also looks like Dhall.Marshall.Decode already has the pattern mentioned in previous comments, which is pretty comfortable as I'm not familiar with DerivingVia - I'm a pretty inexperienced Haskell programmer. At any rate, it seems like an option to omit the generated ToDhall / FromDhall instances would give me a number of ways for moving forward. Thanks for the direction!

@TristanCacqueray
Copy link
Collaborator

Thanks @mmhat , that's very useful!

@mgoldenberg
Copy link
Author

Thanks for implementing this, I really appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
marshalling Marshalling between Dhall and Haskell
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants