elm-bridge icon indicating copy to clipboard operation
elm-bridge copied to clipboard

Bridge, Lens and variables beginning with underscores

Open UrKr opened this issue 9 years ago • 6 comments

Currently, if you use this module to convert types on which you are using the Lens library, you get an invalid output file since it generates variables with underscores. Fixing this manually is possible, but increasingly tedious as the number of types increases.

UrKr avatar Apr 26 '16 12:04 UrKr

I am not sure I understand the problem. Can you show how you derive the instances in a dummy test case, what the result is and what the expected result should ?

bartavelle avatar Apr 27 '16 07:04 bartavelle

Sorry, I really should have explained more.

data Record = Record
    { _fieldA :: Int
    }

makeLens ''Record

The library generates lens functions for the fields with underscore. When the elm module is generated for this record, it looks like this:

type alias ARecord  =
   { _fieldA: Int
   }

jsonDecARecord : Json.Decode.Decoder ( ARecord )
jsonDecARecord =
   ("_fieldA" := Json.Decode.int) `Json.Decode.andThen` \p_fieldA ->
   Json.Decode.succeed {_fieldA = p_fieldA}

jsonEncARecord : ARecord -> Value
jsonEncARecord  val =
   Json.Encode.object
   [ ("_fieldA", Json.Encode.int val._fieldA)
   ]

However, elm doesn't allow underscores at the beginning of variables. I guess it could be so that it still receives records with underscores, but generates elm type aliases without the underscores.

UrKr avatar Apr 27 '16 08:04 UrKr

Usual "fix" is:

$(deriveBoth defaultOptions{ fieldLabelModifier = drop 1}) ''Record

bartavelle avatar Apr 27 '16 09:04 bartavelle

Is that acceptable for your use case ?

bartavelle avatar Apr 27 '16 09:04 bartavelle

This is a perfect fix. I can't think of a case where it wouldn't be. Sorry, I guess I should have read the documentation. Figures that this would already have been adressed

UrKr avatar Apr 27 '16 09:04 UrKr

I think it might be worth it to keep the issue open, as elm-bridge shouldn't generate invalid elm code.

bartavelle avatar Apr 27 '16 11:04 bartavelle