Created
June 9, 2017 17:02
-
-
Save dwhitney/ef474427191c135e9977c090ac30c6da to your computer and use it in GitHub Desktop.
Toy example of purescript argonaut generic encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.Eff (Eff) | |
import Control.Monad.Eff.Console (log, CONSOLE) | |
import Data.Argonaut.Core (Json, stringify) | |
import Data.Argonaut.Generic.Aeson (encodeJson) | |
import Data.Generic | |
import Data.Maybe (Maybe(..)) | |
import Prelude (($), Unit) | |
newtype EmailAddress = EmailAddress String | |
derive instance genericEmailAddress ∷ Generic EmailAddress | |
newtype Email = Email{ | |
to ∷ EmailAddress | |
, from ∷ EmailAddress | |
, subject ∷ Maybe String | |
, content ∷ Maybe String | |
} | |
derive instance genericEmail ∷ Generic Email | |
newtype BatchEmails = BatchEmails{ | |
emails :: Array Email | |
} | |
derive instance batchBatchEmails ∷ Generic BatchEmails | |
aBatchOfEmails :: BatchEmails | |
aBatchOfEmails = BatchEmails{ | |
emails : [ | |
Email{ | |
to : EmailAddress "dustin@gp.com" | |
, from: EmailAddress "agustin@gp.com" | |
, subject: Nothing | |
, content : Just "Hello, World!" | |
} | |
, Email{ | |
to : EmailAddress "dustin@gp.com" | |
, from: EmailAddress "agustin@gp.com" | |
, subject: Just "This is a subject" | |
, content : Nothing | |
} | |
] | |
} | |
anEncodedBatchOfEmails :: Json | |
anEncodedBatchOfEmails = encodeJson aBatchOfEmails | |
main :: ∀ eff . Eff (console ∷ CONSOLE | eff) Unit | |
main = log $ stringify anEncodedBatchOfEmails |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment