Giter VIP home page Giter VIP logo

Comments (9)

bartavelle avatar bartavelle commented on August 16, 2024

What would be the proper Elm code?

type alias Email = String
type alias User  =
   { name: String
   , email: Email
   }

?

from elm-bridge.

paparga avatar paparga commented on August 16, 2024

Sorry for not being clear, the problem is that jsonDecEmail it's not defined.
some alternatives:

1.-

type alias User  =
   { name: String
   , email: String
   }

jsonDecUser : Json.Decode.Decoder ( User )
jsonDecUser =
   ("name" := Json.Decode.string) >>= \pname ->
   ("email" := Json.Decode.string) >>= \pemail ->
   Json.Decode.succeed {name = pname, email = pemail}

2.-

type alias Email = String
type alias User  =
   { name: String
   , email: Email
   }

jsonDecEmail : Json.Decode.Decoder String
jsonDecEmail = Json.Decode.string

jsonDecUser : Json.Decode.Decoder ( User )
jsonDecUser =
   ("name" := Json.Decode.string) >>= \pname ->
   ("email" := jsonDecEmail) >>= \pemail ->
   Json.Decode.succeed {name = pname, email = pemail}

from elm-bridge.

bartavelle avatar bartavelle commented on August 16, 2024

Alright, do you mind testing the latest patch (commit a1b6106) ?

from elm-bridge.

paparga avatar paparga commented on August 16, 2024

I did some testing:

data EmailT 
type Email = Tagged EmailT String

data User1
    = User1
    { name1 :: String
    , email1 :: Tagged EmailT String
    } deriving (Show, Eq)

data User2
    = User2
    { name2 :: String
    , email2 :: Email
    } deriving (Show, Eq)

User1 works

type alias User1  =
   { name1: String
   , email1: String
   }

jsonDecUser1 : Json.Decode.Decoder ( User1 )
jsonDecUser1 =
   ("name1" := Json.Decode.string) >>= \pname1 ->
   ("email1" := Json.Decode.string) >>= \pemail1 ->
   Json.Decode.succeed {name1 = pname1, email1 = pemail1}

jsonEncUser1 : User1 -> Value
jsonEncUser1  val =
   Json.Encode.object
   [ ("name1", Json.Encode.string val.name1)
   , ("email1", Json.Encode.string val.email1)
   ]

But User2 don't

type alias User2  =
   { name2: String
   , email2: Email
   }

jsonDecUser2 : Json.Decode.Decoder ( User2 )
jsonDecUser2 =
   ("name2" := Json.Decode.string) >>= \pname2 ->
   ("email2" := jsonDecEmail) >>= \pemail2 ->
   Json.Decode.succeed {name2 = pname2, email2 = pemail2}

jsonEncUser2 : User2 -> Value
jsonEncUser2  val =
   Json.Encode.object
   [ ("name2", Json.Encode.string val.name2)
   , ("email2", jsonEncEmail val.email2)
   ]

from elm-bridge.

bartavelle avatar bartavelle commented on August 16, 2024

Yes, type aliases are not supported yet, and you can't derive something on them specifically!

You can support them by either:

  • write helper code when generating the module
  • write a custom alteration that handles this type alias

from elm-bridge.

paparga avatar paparga commented on August 16, 2024

Ok!
I will close this issue.

Thanks!

from elm-bridge.

bartavelle avatar bartavelle commented on August 16, 2024

(in theory it would be possible to handle the type alias case too, but it is a bit more work, and the problem si that you can't define how you would like it to be encoded on the Elm side with the current framework)

from elm-bridge.

bartavelle avatar bartavelle commented on August 16, 2024

Let me know if you require an upload on hackage, or if you are fine with using the git master for now.

from elm-bridge.

paparga avatar paparga commented on August 16, 2024

I'm fine with git and stack for now.

If you need help for implementing type alias, like documentation or testing just ping me. I've never done template haskell before, but maybe I could also help there.

Thanks for your time!

from elm-bridge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.