Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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