Last active
September 29, 2016 13:56
-
-
Save teh/c67c6370bdcc43536a1b600cc3cd2578 to your computer and use it in GitHub Desktop.
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 X where | |
import Prelude | |
import Data.Foreign (F) | |
import Data.Foreign.Class (class IsForeign, readJSON, readProp) | |
import Data.Foreign.Index (prop) | |
import Control.Monad.Eff.Console (logShow) | |
import Data.Either (Either(..)) | |
-- { repo(owner: “tom”, name: “holborn”) | |
-- { commit(ref: “master“) { target blob(path: “README.md”) { contents date } } } } | |
type DateTime = Int | |
data BrowseFragment = BrowseFragment | |
{ repo :: | |
{ commit :: | |
{ target :: String | |
, blob :: { contents :: String, date :: DateTime } | |
} | |
} | |
} | |
-- manually constructed fragment: | |
fragment = BrowseFragment { repo: { commit: { target: "master", blob: { contents: "main = print 10", date: 2016 } } } } | |
-- reading a fragment from JSON | |
instance isForeignBrowseFragment :: IsForeign BrowseFragment where | |
read value = do | |
repo <- prop "repo" value | |
commit <- prop "commit" repo | |
target <- readProp "target" commit | |
blob <- prop "blob" commit | |
contents <- readProp "contents" blob | |
date <- readProp "date" blob | |
pure (BrowseFragment { repo: { commit: { target: target, blob: { contents, date }}}}) | |
testJSON = """{ "repo": { "commit": { "target": "master", "blob": { "contents": "main = print 10", "date": 2016 } } } }""" | |
test = case (readJSON testJSON :: F BrowseFragment) of | |
Right r -> do | |
logShow "Ok" | |
Left err -> do | |
logShow "no" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment