Created
December 4, 2015 16:30
-
-
Save codedmart/08d9c416ffa8a9dac1cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Types.Foo where | |
| import Prelude | |
| import Data.Argonaut ((~>), (:=), (.?), jsonEmptyObject) | |
| import Data.Argonaut.Encode (EncodeJson, encodeJson) | |
| import Data.Argonaut.Decode (DecodeJson, decodeJson) | |
| import Data.Maybe (Maybe(..)) | |
| type Foo = | |
| { foo :: Maybe Number | |
| , bar :: Maybe String | |
| } | |
| newtype FooNT = FooNT | |
| { foo :: Maybe Number | |
| , bar :: Maybe String | |
| } | |
| instance decodeJsonFooNT :: DecodeJson FooNT where | |
| decodeJson json = do | |
| obj <- decodeJson json | |
| foo <- obj .? "foo" | |
| bar <- obj .? "bar" | |
| pure $ FooNT {foo: foo, bar: bar} | |
| instance encodeJsonFooNT :: EncodeJson FooNT where | |
| encodeJson (FooNT f) | |
| = "bar" := f.bar | |
| ~> "foo" := f.foo | |
| ~> jsonEmptyObject | |
| instance showFooNT :: Show FooNT where | |
| show (FooNT f) = "Foo {foo: " ++ show f.foo ++ ", bar:" ++ show f.bar ++ "}" | |
| runFoo :: FooNT -> Foo | |
| runFoo (FooNT f) = f | |
| initialFoo :: Foo | |
| initialFoo = | |
| { foo: Just 42.0 | |
| , bar: Nothing | |
| } | |
| foo :: FooNT | |
| foo = FooNT initialFoo | |
| encodedFoo = encodeJson foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment