Comments (9)
What would be the proper Elm code?
type alias Email = String
type alias User =
{ name: String
, email: Email
}
?
from elm-bridge.
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.
Alright, do you mind testing the latest patch (commit a1b6106) ?
from elm-bridge.
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.
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.
Ok!
I will close this issue.
Thanks!
from elm-bridge.
(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.
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.
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)
- Support For Haskell Type Alias HOT 5
- Derived elm code does not respect `unwrapUnaryRecords=False` HOT 5
- Dropping support for GHC 7
- Good way to deal with Persistent's Entity? HOT 9
- ETypeDef instances for base types HOT 6
- How to deal with UTCTime? HOT 20
- Support for simple phantom types HOT 17
- Tests are failing after 0.19 changes HOT 1
- GraphQL HOT 1
- General questions about design of elm-bridge HOT 18
- Base on smaller elm codegen library? HOT 2
- Are the tests actually running the generated elm code? HOT 3
- Lambda paramaters fail Elm 0.19 shadowing rules HOT 8
- Release 0.6.0 HOT 2
- Dict serialization escape key string HOT 4
- ghc-9.0.1 : Kind change of TyVarBndr breaks compilation HOT 3
- How to deal with # (magic hash) in Haskell data types? HOT 2
- Issue with UTCTime to Posix HOT 2
- Higher Kinded Data HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elm-bridge.