Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hlb review comments #1881

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
rename: Valuable -> IsLedgerValue
  • Loading branch information
nickchapman-da committed Jun 26, 2019
commit ad81f357c88a9ddc7b68e6cdbc3df03f1c1b3ed5
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import DA.Ledger (
Command(..), Event(..), Transaction(..)
)

import DA.Ledger.Valuable (toRecord,fromRecord)
import DA.Ledger.IsLedgerValue (toRecord,fromRecord)
import Domain (Introduce,Message,Broadcast)
import Logging (Logger)

Expand Down
8 changes: 4 additions & 4 deletions language-support/hs/bindings/examples/chat/src/Domain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ module Domain(Party(..),
) where

import DA.Ledger.Types as L (Party(..),Value(VList))
import DA.Ledger.Valuable (Valuable(..))
import DA.Ledger.IsLedgerValue (IsLedgerValue(..))
import Data.Text.Lazy (Text)

data Introduce = Introduce { from :: Party, people :: [Party] }
deriving Show

instance Valuable Introduce where
instance IsLedgerValue Introduce where
toValue Introduce{from,people} = L.VList [toValue from, toValue people]
fromValue = \case
L.VList [v1,v2] -> do
Expand All @@ -30,7 +30,7 @@ instance Valuable Introduce where
data Message = Message { from :: Party, to :: Party, body :: Text }
deriving Show

instance Valuable Message where
instance IsLedgerValue Message where
toValue Message{from,to,body} = L.VList [toValue from, toValue to, toValue body]
fromValue = \case
L.VList [v1,v2,v3] -> do
Expand All @@ -43,7 +43,7 @@ instance Valuable Message where
data Broadcast = Broadcast { from :: Party, to :: [Party], body :: Text }
deriving Show

instance Valuable Broadcast where
instance IsLedgerValue Broadcast where
toValue Broadcast{from,to,body} = L.VList [toValue from, toValue to, toValue body]
fromValue = \case
L.VList [v1,v2,v3] -> do
Expand Down
10 changes: 5 additions & 5 deletions language-support/hs/bindings/examples/nim-console/src/Domain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Domain(Player(..), partyOfPlayer,

import Data.List.Extra(zipWithFrom)
import DA.Ledger.Types
import DA.Ledger.Valuable(Valuable(..))
import DA.Ledger.IsLedgerValue(IsLedgerValue(..))
import qualified Data.Text.Lazy as Text

data Player = Player { unPlayer :: String } deriving (Eq,Ord)
Expand All @@ -24,14 +24,14 @@ instance Show Player where show (Player s) = s
partyOfPlayer :: Player -> Party
partyOfPlayer = Party . Text.pack . unPlayer

instance Valuable Player where
instance IsLedgerValue Player where
toValue = toValue . Party . Text.pack . unPlayer
fromValue = fmap (Player . Text.unpack . unParty) . fromValue

data Offer = Offer { from :: Player, to :: [Player] }
deriving (Show)

instance Valuable Offer where
instance IsLedgerValue Offer where
toValue Offer{from,to} = VList [toValue from, toValue to]
fromValue = \case
VList [v1,v2] -> do
Expand All @@ -42,7 +42,7 @@ instance Valuable Offer where

data Game = Game { p1 :: Player, p2 :: Player, piles :: [Int] } deriving (Show)

instance Valuable Game where
instance IsLedgerValue Game where
toValue Game{} = undefined -- we never send games to the ledger
fromValue = \case
VList[VRecord Record{fields=[
Expand All @@ -62,7 +62,7 @@ data Move = Move { pileNum :: Int, howMany :: Int }
instance Show Move where
show Move{pileNum,howMany} = show pileNum <> ":" <> show howMany

instance Valuable Move where
instance IsLedgerValue Move where
toValue Move{pileNum,howMany} =
VRecord(Record{rid=Nothing,
fields=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module NimCommand(NimCommand(..), makeLedgerCommands,) where

import DA.Ledger as Ledger
import DA.Ledger.Valuable
import DA.Ledger.IsLedgerValue
import Domain

type Oid = ContractId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module NimTrans(NimTrans(..), extractTransaction,) where

import DA.Ledger as Ledger
import DA.Ledger.Valuable
import DA.Ledger.IsLedgerValue
import Domain
import Logging

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

{-# LANGUAGE OverloadedStrings #-}

module DA.Ledger.Valuable( -- TODO: Better name!
Valuable(..), -- types which can be converted to/from a Ledger API Value
module DA.Ledger.IsLedgerValue (
IsLedgerValue(..), -- types which can be converted to/from a Ledger API Value
) where

import Data.Text.Lazy (Text)
import DA.Ledger.Types

class Valuable a where
class IsLedgerValue a where
toValue :: a -> Value
fromValue :: Value -> Maybe a

Expand All @@ -28,18 +28,18 @@ class Valuable a where
. map fieldValue --(\RecordField{value} -> value)
. fields

instance Valuable Int where
instance IsLedgerValue Int where
toValue = VInt
fromValue = \case VInt x -> Just x; _ -> Nothing

instance Valuable Party where
instance IsLedgerValue Party where
toValue = VParty
fromValue = \case VParty x -> Just x; _ -> Nothing

instance Valuable a => Valuable [a] where
instance IsLedgerValue a => IsLedgerValue [a] where
toValue = VList . map toValue
fromValue = \case VList vs -> mapM fromValue vs; _ -> Nothing

instance Valuable Text where
instance IsLedgerValue Text where
toValue = VString
fromValue = \case VString x -> Just x; _ -> Nothing