I like the look and feel of it! I threw a bunch of types at the decoder generator to see what it could handle . I’m impressed !
It handles:
Records
Tuples
Dicts
Lists
Nested structures like a list of tuples
Sum types (e.g. type Color = Red | Green | Blue)
I’ve noticed is struggles with product types like type Email = Email String. If I were writing this by hand I’d implement the decoder as this one-liner:
decodeEmail =
Dec.map Email Dec.string
but the generator attempts to decode it using a huge nested case and multiple calls to decodeValue
decodeEmail =
let
recover xs =
case xs of
a0::bs->
case decodeValue Dec.string a0 of
Ok "Email"->
case bs of
a1::cs ->
case decodeValue Dec.string a1 of
Ok a1_->
Dec.succeed <| Email a1_
Err err->
Dec.fail err
_->
Dec.fail <| "Invalid fields for constructor Email: " ++ toString bs
Ok other->
Dec.fail <| "Invalid constructor field found: " ++ other
Err err->
Dec.fail err
_->
Dec.fail "Invalid JSON input: empty list"
in
Dec.list Dec.value |> andThen recover
I would find this more usable if it were available as a command line tool, it works nicely but not realistic to cope paste every file in a large project.
I would find this more usable if it were available as a command line tool, it works nicely but not realistic to cope paste every file in a large project.
I agree. I’ll try to do that if the copy/paste version doesn’t uncover any obvious bugs.
Pretty cool! I am always using elm-analyse to find unused imports, but I can never quite get rid of all of them, so having an automatic tool would be super useful for me. Thanks for working on this.
However, I did apply the unused import tool to four big modules of mine, and I had a lot of problems:
0 It would delete this import: import Data.Id exposing(Id(Id, New)), where the module contained…
type Id
= Id String
| New
1 I had an import that was like import Data.A as A exposing (B), and I had functions that used B in both aliased and unaliased forms like String -> A.B -> ... and String -> B and it removed the import for B entirely
2 I import a union type and expose many constructors like import Tracking exposing (Event(A,B,C)) and it would format that to import Tracking exposing (A, B, C)
I don’t mean to be demanding because you’ve done a great job here. Is it possible for it to just recursively replace all .elm files under a specified folder directly? Rather than to write new files with ‘-Impfix.elm’ in the name?
Does not have to be the default behaviour and I can see why you would be careful when writing a new tool, as you don’t want to accidentally obliterate some existing .elm code that has not been checked in yet. On the other hand, we all know how to use git and revert files if something goes wrong - so I would feel quite safe running it recursively on a large project and seeing if I like the results.
This is a great tool. Is there any chance of a option to Decode with out needing the Json.Decode.Pipeline package and instead use the core decoders only?
I think that’s fairly easy to do; I’ll check. I used Pipeline because you can decode any number of fields whereas the Dec.map's run out at 8. But I guess one could make Pipeline as a last-resort option for cases when there are more than 8 fields.