Skip to content

Commit

Permalink
Change API to serialise IDs and Names plainly
Browse files Browse the repository at this point in the history
i.e. we now have '1' and '"foo"' instead of
'{unID:1}' and '{unName:"foo"}'
  • Loading branch information
brprice committed Oct 20, 2021
1 parent 2255f58 commit 477fcd8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions primer-service/src/Primer/OpenAPI.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Primer.OpenAPI (
Expand All @@ -6,9 +7,10 @@ module Primer.OpenAPI (
) where

import Data.OpenApi (ToSchema)
import Data.Text (Text)
import Primer.API (APIDef, APIProg, Tree)
import Primer.App (InitialApp)
import Primer.Core (ID)
import Primer.Core (ID (..))
import Primer.Database (Session, SessionName)
import Primer.Name (Name)

Expand All @@ -22,8 +24,14 @@ import Primer.Name (Name)
instance ToSchema SessionName
instance ToSchema Session
instance ToSchema InitialApp
instance ToSchema ID
instance ToSchema Name

-- We need to GND the ID instance to matche its To/FromJSON instances
deriving newtype instance ToSchema ID

-- We can't GND derive for Name as it is an opaque class
-- But the JSON instance is done by GND, so we must match here...
-- This instance works because the parameter has a phantom role!
deriving via Text instance (ToSchema Name)
instance ToSchema Tree
instance ToSchema APIDef
instance ToSchema APIProg
2 changes: 1 addition & 1 deletion primer/src/Primer/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ getProgram sid = withSession' sid $ QueryApp $ viewProg . handleGetProgramReques
-- just enough information to render nicely.
-- (NB: currently this is just a first draft, and is expected to evolve.)
data Tree = Tree
{ nodeId :: ID -- REVIEW: here, and in APIDef we maybe want a raw Int, as IDs serialise as '{unID: 5}'
{ nodeId :: ID
, label :: Text
, children :: [Tree]
}
Expand Down
2 changes: 1 addition & 1 deletion primer/src/Primer/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ newtype ID = ID {unID :: Int}
-- The Ord and Enum instances are useful for tests but we may remove them in
-- future, so don't use them in app code.
deriving newtype (Show, Num, Ord, Enum)
deriving (FromJSON, ToJSON) via VJSON ID
deriving newtype (FromJSON, ToJSON)

instance ToJSONKey ID

Expand Down
2 changes: 1 addition & 1 deletion primer/src/Primer/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Primer.JSON
newtype Name = Name {unName :: Text}
deriving (Eq, Ord, Generic, Data)
deriving newtype (Show, IsString)
deriving (FromJSON, ToJSON) via VJSON Name
deriving newtype (FromJSON, ToJSON)

-- | Construct a name from a Text. This is called unsafe because there are no
-- guarantees about whether the name refers to anything that is in scope.
Expand Down

0 comments on commit 477fcd8

Please sign in to comment.