Skip to content

Commit

Permalink
add pactValueToText
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoeldner committed Oct 14, 2024
1 parent c948d93 commit 88f1d77
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
3 changes: 1 addition & 2 deletions pact/Pact/Core/IR/Eval/Runtime/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ import Pact.Core.Capabilities
import Pact.Core.Hash
import Pact.Core.SizeOf
import Pact.Core.StableEncoding
import qualified Pact.Core.Pretty as Pretty
import qualified Pact.Core.Principal as Pr

emitReservedEvent :: Text -> [PactValue] -> ModuleHash -> EvalM e b i ()
Expand Down Expand Up @@ -465,7 +464,7 @@ renderPactValue :: i -> PactValue -> EvalM e b i Text
renderPactValue info pv = do
sz <- sizeOf info SizeOfV0 pv
chargeGasArgs info $ GConcat $ TextConcat $ GasTextLength $ fromIntegral sz
pure $ Pretty.renderCompactText pv
pure $ pactValueToText pv -- Pretty.renderCompactText pv


createPrincipalForGuard
Expand Down
61 changes: 60 additions & 1 deletion pact/Pact/Core/PactValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ module Pact.Core.PactValue
, pattern PUnit
, _PUnit
, synthesizePvType
, pactValueToText
) where

import Control.Lens
import Data.Vector(Vector)
import Data.Map.Strict(Map)
import Data.Text(Text)
import Data.Decimal(Decimal)
import Data.Decimal

import Control.DeepSeq
import GHC.Generics
Expand All @@ -44,6 +45,7 @@ import qualified Data.Vector as V
import qualified Data.Map.Strict as M
import qualified Pact.Time as PactTime
import qualified Data.Set as S
import qualified Data.Text as T

import Pact.Core.Type
import Pact.Core.Names
Expand Down Expand Up @@ -109,6 +111,63 @@ instance Pretty PactValue where
"CapToken" <> pretty (CapToken (fqnToQualName fqn) args)
PTime t -> pretty (PactTime.formatTime "%Y-%m-%d %H:%M:%S%Q %Z" t)


pactValueToText :: PactValue -> Text
pactValueToText = \case
PLiteral lit -> case lit of
LString s -> tdquotes s
LInteger i -> tshow i
LDecimal d ->
if roundTo 0 d == d then
tshow (roundTo 0 d) <> ".0"
else tshow d
LUnit -> "()"
LBool b -> if b then "true" else "false"
PList l -> let
l' = pactValueToText <$> V.toList l
in tlist l'
PGuard g -> case g of
GKeyset (KeySet ks f) -> let
keys = tlist (fmap _pubKey $ S.toList ks)
p = predicateToText f
in tcurly "KeySet" [("keys", keys), ("pred", p)]
GKeySetRef ksn -> T.concat ["'", renderKeySetName ksn]
GUserGuard (UserGuard f args) -> let
f' = renderQualName f
args' = tlist (pactValueToText <$> args)
in tcurly "UserGuard" [("fun", f'), ("args", args')]
GCapabilityGuard (CapabilityGuard n args pid) -> let
mpid = [("pactId", maybe mempty _defPactId pid)]
pvs = pactValueToText <$> args
elems = [("name", renderQualName n), ("args", tlist pvs)] ++ mpid
in tcurly "CapabilityGuard" elems
GModuleGuard (ModuleGuard mn n) ->
tcurly "ModuleGuard" [("module", renderModuleName mn), ("name", n)]
GDefPactGuard (DefPactGuard pid n) ->
tcurly "PactGuard" [("pactId", _defPactId pid), ("name", n)]
PObject o -> let
o' = fmap (\(Field f, pv) -> T.concat [tdquotes f, ": ", pactValueToText pv]) $ M.toList o
in T.concat ["{",T.intercalate "," o', "}"]
PModRef (ModRef _mn imp) -> let
imp' = renderModuleName <$> S.toList imp
in T.concat ["module{",tlist imp', "}"]
PCapToken (CapToken qn args) -> let
args' = if null args then mempty else " " <> T.intercalate " " (pactValueToText <$> args)
qualName = fqnToQualName qn
in T.concat ["CapToken(", renderQualName qualName, args',")"] -- Todo: check
PTime t -> let
t' = PactTime.formatTime "%Y-%m-%d %H:%M:%S%Q %Z" t
in T.pack t'
where
tdquotes x = T.concat ["\"",x,"\""]
tshow :: Show a => a -> Text
tshow = T.pack . show
tlist l = T.concat ["[",T.intercalate ", " l, "]"]
tcurly :: Text -> [(Text, Text)] -> Text
tcurly n l = let
l' = fmap (\(k, v) -> T.concat [k, ": ", v]) l
in T.concat [n, " {", T.intercalate "," l', "}"]

synthesizePvType :: PactValue -> Type
synthesizePvType = \case
PLiteral l -> typeOfLit l
Expand Down

0 comments on commit 88f1d77

Please sign in to comment.