Skip to content

Commit

Permalink
hlb, use Int64 / Fixed E10 as appropriate (digital-asset#2440)
Browse files Browse the repository at this point in the history
nickchapman-da authored Aug 7, 2019
1 parent 966dfbd commit 8f9a781
Showing 3 changed files with 12 additions and 8 deletions.
9 changes: 5 additions & 4 deletions language-support/hs/bindings/src/DA/Ledger/Convert.hs
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ import Data.Map(Map)
import Data.Maybe (fromMaybe)
import Data.Text.Lazy (Text)
import Data.Vector as Vector (Vector,fromList,toList)
import qualified Data.Text.Lazy as Text (pack,unpack)

import qualified Google.Protobuf.Empty as LL
import qualified Google.Protobuf.Timestamp as LL
@@ -121,7 +122,7 @@ lowerValue = LL.Value . Just . \case
VContract c -> (LL.ValueSumContractId . unContractId) c
VList vs -> (LL.ValueSumList . LL.List . Vector.fromList . map lowerValue) vs
VInt i -> (LL.ValueSumInt64 . fromIntegral) i
VDecimal t -> LL.ValueSumDecimal t
VDecimal t -> LL.ValueSumDecimal $ Text.pack $ show t
VText t -> LL.ValueSumText t
VTime x -> (LL.ValueSumTimestamp . fromIntegral . unMicroSecondsSinceEpoch) x
VParty p -> (LL.ValueSumParty . unParty) p
@@ -140,7 +141,7 @@ lowerVariant = \case
, variantConstructor = unConstructorId cons
, variantValue = Just $ lowerValue value
}

lowerEnum :: Enum -> LL.Enum
lowerEnum = \case
Enum{..} ->
@@ -176,7 +177,7 @@ runRaise :: (a -> Perhaps b) -> a -> IO (Perhaps b)
runRaise raise a = fmap collapseErrors $ try $ evaluate $ raise a
where
collapseErrors :: Either SomeException (Perhaps a) -> Perhaps a
collapseErrors = either (Left . ThrewException) id
collapseErrors = either (Left . ThrewException) id


data RaiseFailureReason = Missing String | Unexpected String | ThrewException SomeException deriving Show
@@ -354,7 +355,7 @@ raiseValue = \case
LL.ValueSumContractId c -> (return . VContract . ContractId) c
LL.ValueSumList vs -> (fmap VList . raiseList raiseValue . LL.listElements) vs
LL.ValueSumInt64 i -> (return . VInt . fromIntegral) i
LL.ValueSumDecimal t -> (return . VDecimal) t
LL.ValueSumDecimal t -> (return . VDecimal . read . Text.unpack) t
LL.ValueSumText t -> (return . VText) t
LL.ValueSumTimestamp x -> (return . VTime . MicroSecondsSinceEpoch . fromIntegral) x
LL.ValueSumParty p -> (return . VParty . Party) p
9 changes: 6 additions & 3 deletions language-support/hs/bindings/src/DA/Ledger/Types.hs
Original file line number Diff line number Diff line change
@@ -58,6 +58,9 @@ module DA.Ledger.Types( -- High Level types for communication over Ledger API

) where

import DA.Daml.LF.Ast.Base (E10)
import Data.Fixed
import Data.Int (Int64)
import Data.Map (Map)
import Data.Text.Lazy (Text)
import Prelude hiding(Enum)
@@ -192,7 +195,7 @@ data Value
| VContract ContractId
| VList [Value]
| VInt Int
| VDecimal Text
| VDecimal (Fixed E10)
| VText Text
| VTime MicroSecondsSinceEpoch
| VParty Party
@@ -248,7 +251,7 @@ data LedgerConfiguration = LedgerConfiguration
}
deriving (Eq,Ord,Show)

newtype MicroSecondsSinceEpoch = MicroSecondsSinceEpoch { unMicroSecondsSinceEpoch :: Int}
newtype MicroSecondsSinceEpoch = MicroSecondsSinceEpoch { unMicroSecondsSinceEpoch :: Int64}
deriving (Eq,Ord,Show)

newtype DaysSinceEpoch = DaysSinceEpoch { unDaysSinceEpoch :: Int}
@@ -273,6 +276,6 @@ newtype AbsOffset = AbsOffset { unAbsOffset :: Text } deriving (Eq,Ord,Show)
newtype Choice = Choice { unChoice :: Text } deriving (Eq,Ord,Show)

newtype Party = Party { unParty :: Text } deriving (Eq,Ord)
instance Show Party where show = Text.unpack . unParty
instance Show Party where show p = "'" <> (Text.unpack $ unParty p) <> "'"

newtype Verbosity = Verbosity { unVerbosity :: Bool } deriving (Eq,Ord,Show)
2 changes: 1 addition & 1 deletion language-support/hs/bindings/test/DA/Ledger/Tests.hs
Original file line number Diff line number Diff line change
@@ -516,7 +516,7 @@ bucket = VRecord $ Record Nothing
, RecordField "contract"$ VContract (ContractId "xxxxx")
, RecordField "list" $ VList []
, RecordField "int" $ VInt 42
, RecordField "decimal" $ VDecimal "123.456"
, RecordField "decimal" $ VDecimal (read "123.456")
, RecordField "text" $ VText "OMG lol"
, RecordField "time" $ VTime (MicroSecondsSinceEpoch $ 1000 * 1000 * 60 * 60 * 24 * 365 * 50)
, RecordField "party" $ VParty $ Party "good time"

0 comments on commit 8f9a781

Please sign in to comment.