21. Elm とHaskell
こういう定義から
type Todo = Record
'[ "id" >: Int
, "title" >: String
, "done" >: Bool
]
instance IsElmType Todo where
compileElmType = compileElmRecordTypeWith "Todo"
instance IsElmDefinition Todo where
compileElmDef = ETypeAlias . compileElmRecordAliasWith "Todo"
type CRUD
= "todos" :> Get '[JSON] [Todo]
:<|> "todos" :> ReqBody '[JSON, FormUrlEncoded] Todo
:> Post '[JSON] Todo
22. Elm とHaskell
こういうElm コードを⽣成してくれる
type alias Todo =
{ id : Int
, title : String
, done : Bool
}
getApiTodos : (Result Http.Error (List Todo) -> msg) -> Cmd msg
getApiTodos = ...
postApiTodos : Todo -> (Result Http.Error Todo -> msg) -> Cmd msg
postApiTodos = ...
JSON のデコーダー・エンコーダーも⼀緒に⽣成する
24. キャッシュとHaskell
実装はServant + STM (超簡単)
type Store = IntMap [Build] -- Build is target data type
type API = Get '[JSON] Store
:<|> Capture "problem" Int :> Patch '[JSON] NoContent
server :: TVar Store -> ServerT API Plant
server store = getStore :<|> putStore
where
getStore = liftIO $ readTVarIO store
putStore pid = do
findProblemWith pid $ problem -> do
builds <- uniqByTeam <$> fetchBuilds problem
liftIO $
atomically (modifyTVar' store $ modifyWith problem builds)
pure NoContent
modifyWith :: Problem -> [Build] -> Store -> Store
modifyWith = ...