jsonnet-0.5.0.0: src/Language/Jsonnet/Std/Lib.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Language.Jsonnet.Std.Lib (
ExtVars (..),
UserFuncs (..),
StdEnv (..),
stdlib,
mkStdObject,
objectFieldsEx,
objectHasEx,
) where
import Control.Lens ((<&>))
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader, asks)
import Crypto.Hash.MD5 qualified as MD5
import Data.Aeson qualified as JSON
import Data.ByteString qualified as B
import Data.ByteString.Base16 qualified as B16
import Data.Fixed (mod')
import Data.Functor (($>))
import Data.HashMap.Strict qualified as H
import Data.List qualified as L
import Data.Map qualified as Map
import Data.Map.Strict (Map)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.IO (hPutStrLn)
import Data.Vector qualified as V
import Data.Yaml qualified as Yaml
import Language.Jsonnet.Common (Visibility (..))
import Language.Jsonnet.Error (EvalError (ExtVarNotFound, StdError))
import Language.Jsonnet.Eval.Monad
import Language.Jsonnet.Object (Field (..), Object)
import Language.Jsonnet.Object qualified as Object
import Language.Jsonnet.Value
import System.IO (stderr)
import Prelude hiding (length)
import Prelude qualified as P (length)
newtype ExtVars = ExtVars {unExtVars :: Map Text Value}
deriving newtype (Semigroup, Monoid)
newtype UserFuncs = UserFuncs {unUserFuncs :: Map Text Value}
deriving newtype (Semigroup, Monoid)
data StdEnv = StdEnv
{ stdNonNative :: Object
, stdExtVars :: ExtVars
, stdUserFuncs :: UserFuncs
}
stdlib :: (MonadIO m, MonadReader StdEnv m) => m Value
stdlib = asks mkStdObject >>= mkObj
mkStdObject :: StdEnv -> Object
mkStdObject (StdEnv nonnative (ExtVars ev) (UserFuncs uf)) =
nonnative <> Object.mkBaseObject (H.fromList xs)
where
infixr 8 ~>
k ~> v = (k, Field (inj v) Hidden False)
xs =
[ "type" ~> showTy
, "primitiveEquals" ~> primitiveEquals
, "equals" ~> equals
, "length" ~> length
, "pow" ~> (**) @Double
, "exp" ~> exp @Double
, "log" ~> log @Double
, "exponent" ~> exponent @Double
, "mantissa" ~> significand @Double
, "floor" ~> floor @Double @Integer
, "ceil" ~> ceiling @Double @Integer
, "sqrt" ~> sqrt @Double
, "sin" ~> sin @Double
, "cos" ~> cos @Double
, "tan" ~> tan @Double
, "asin" ~> asin @Double
, "acos" ~> acos @Double
, "atan" ~> atan @Double
, "atan2" ~> atan2 @Double
, "hypot" ~> \a b -> (sqrt @Double) (a * a + b * b)
, "modulo" ~> mod' @Double
, "codepoint" ~> fromEnum . T.head
, "char" ~> T.singleton . toEnum
, "encodeUTF8" ~> B.unpack . T.encodeUtf8
, "decodeUTF8" ~> T.decodeUtf8 . B.pack
, "makeArray" ~> V.generateM @EvalM @Value
, "filter" ~> V.filterM @EvalM @Value
, "join" ~> intercalate
, "objectHasEx" ~> objectHasEx
, "objectFieldsEx" ~> objectFieldsEx
, "objectRemoveKey" ~> flip (H.delete @Text @Value)
, "parseJson" ~> JSON.throwDecodeStrict @JSON.Value @EvalM
, "parseYaml" ~> parseYaml
, "extVar" ~> (liftMaybe . ExtVarNotFound <*> (`Map.lookup` ev))
, "thisFile" ~> getFileName
, "trace" ~> trace
, "native" ~> (`Map.lookup` uf)
, "md5" ~> T.decodeUtf8 . B16.encode . MD5.hash . T.encodeUtf8
]
intercalate :: Value -> [Value] -> EvalM Value
intercalate sep arr = go sep (filter notNull arr)
where
notNull VNull = False
notNull _ = True
go sep'@(VArr _) = app (L.intercalate @Value) sep'
go sep'@(VStr _) = app T.intercalate sep'
go _ = const $ throwE (StdError "join's separator must be a string or an array")
app f sep' arr' = inj <$> (f <$> proj sep' <*> traverse proj arr')
length :: Value -> EvalM Int
length = \case
VStr s -> pure $ T.length s
VArr a -> pure $ P.length a
VObj o _ -> pure $ P.length (objectFieldsEx o True)
VFun l _ -> pure $ l
_ ->
throwE
(StdError "length operates on strings, objects, functions and arrays, got ")
trace :: Text -> Value -> EvalM Value
trace str =
($>)
( do
fn <- T.pack <$> getFileName
ln <- getLineNum
let loc = maybe fn (\l -> fn <> ":" <> T.pack (show l)) ln
liftIO $ hPutStrLn stderr ("TRACE: " <> loc <> " " <> str)
)
parseYaml :: B.ByteString -> EvalM Value
parseYaml bs =
Yaml.decodeAllThrow @EvalM @JSON.Value bs <&> \case
[] -> VNull
[doc] -> inj doc
docs -> inj docs
objectFieldsEx :: Object -> Bool -> [Text]
objectFieldsEx o = \case
True -> Object.regularKeys o
False -> Object.visibleKeys o
objectHasEx :: Object -> Text -> Bool -> Bool
objectHasEx o f all' = f `elem` objectFieldsEx o all'