2018-04-14 03:09:12 +02:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
2018-04-16 05:43:04 +02:00
|
|
|
{-# LANGUAGE DefaultSignatures #-}
|
2018-04-14 03:09:12 +02:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE MultiWayIf #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2018-04-16 19:56:29 +02:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
2018-04-14 03:09:12 +02:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -Wno-missing-signatures #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
-- | Although there are a lot of instances in this file, really it's just a
|
|
|
|
-- combinatorial explosion of the following combinations:
|
|
|
|
--
|
|
|
|
-- - Several Haskell types being converted to/from Nix wrappers
|
|
|
|
-- - Several types of Nix wrappers
|
|
|
|
-- - Whether to be shallow or deep while unwrapping
|
|
|
|
|
2018-04-14 03:09:12 +02:00
|
|
|
module Nix.Convert where
|
|
|
|
|
|
|
|
import Control.Monad
|
|
|
|
import Data.Aeson (toJSON)
|
|
|
|
import qualified Data.Aeson as A
|
2018-04-15 07:58:50 +02:00
|
|
|
import Data.ByteString
|
2018-04-14 03:09:12 +02:00
|
|
|
import Data.Fix
|
|
|
|
import Data.HashMap.Lazy (HashMap)
|
|
|
|
import qualified Data.HashMap.Lazy as M
|
2018-04-15 07:58:50 +02:00
|
|
|
import Data.Scientific
|
2018-04-14 03:09:12 +02:00
|
|
|
import Data.Text (Text)
|
2018-04-15 08:52:21 +02:00
|
|
|
import qualified Data.Text as Text
|
2018-04-15 07:58:50 +02:00
|
|
|
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
2018-04-14 03:09:12 +02:00
|
|
|
import qualified Data.Vector as V
|
|
|
|
import Nix.Atoms
|
|
|
|
import Nix.Effects
|
|
|
|
import Nix.Expr.Types
|
|
|
|
import Nix.Expr.Types.Annotated
|
|
|
|
import Nix.Normal
|
|
|
|
import Nix.Stack
|
|
|
|
import Nix.Thunk
|
2018-04-16 08:00:09 +02:00
|
|
|
import Nix.Utils
|
2018-04-14 03:09:12 +02:00
|
|
|
import Nix.Value
|
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
class FromValue a m v where
|
|
|
|
fromValue :: v -> m a
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay :: v -> m (Maybe a)
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
type Convertible e m = (Framed e m, MonadVar m, MonadFile m)
|
|
|
|
|
|
|
|
instance Convertible e m => FromValue () m (NValueNF m) where
|
2018-04-16 08:00:09 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF NNull) -> pure $ Just ()
|
2018-04-16 08:00:09 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-16 08:00:09 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a null, but saw: " ++ show v
|
2018-04-16 08:00:09 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue () m (NValueF m r) where
|
2018-04-16 08:00:09 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF NNull -> pure $ Just ()
|
2018-04-16 08:00:09 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-16 08:00:09 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a null, but saw: " ++ show v
|
2018-04-16 08:00:09 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue Bool m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF (NBool b)) -> pure $ Just b
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a bool, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue Bool m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF (NBool b) -> pure $ Just b
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a bool, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue Int m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF (NInt b)) -> pure $ Just (fromInteger b)
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an integer, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue Int m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF (NInt b) -> pure $ Just (fromInteger b)
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an integer, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue Integer m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF (NInt b)) -> pure $ Just b
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an integer, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue Integer m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF (NInt b) -> pure $ Just b
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an integer, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue Float m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF (NFloat b)) -> pure $ Just b
|
|
|
|
Fix (NVConstantF (NInt i)) -> pure $ Just (fromInteger i)
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a float, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue Float m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF (NFloat b) -> pure $ Just b
|
|
|
|
NVConstantF (NInt i) -> pure $ Just (fromInteger i)
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a float, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, MonadEffects m)
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue Text m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF (NUri u)) -> pure $ Just u
|
|
|
|
Fix (NVStrF t _) -> pure $ Just t
|
|
|
|
Fix (NVPathF p) -> Just . Text.pack . unStorePath <$> addPath p
|
|
|
|
Fix (NVSetF s _) -> case M.lookup "outPath" s of
|
2018-04-18 04:10:36 +02:00
|
|
|
Nothing -> pure Nothing
|
2018-04-18 06:49:29 +02:00
|
|
|
Just p -> fromValueMay @Text p
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a string, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, MonadEffects m,
|
|
|
|
FromValue Text m r, Show (NValueF m r))
|
|
|
|
=> FromValue Text m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF (NUri u) -> pure $ Just u
|
|
|
|
NVStrF t _ -> pure $ Just t
|
|
|
|
NVPathF p -> Just . Text.pack . unStorePath <$> addPath p
|
|
|
|
NVSetF s _ -> case M.lookup "outPath" s of
|
2018-04-18 04:10:36 +02:00
|
|
|
Nothing -> pure Nothing
|
2018-04-18 06:49:29 +02:00
|
|
|
Just p -> fromValueMay @Text p
|
2018-04-16 19:55:57 +02:00
|
|
|
_ -> pure Nothing
|
|
|
|
fromValue v = fromValueMay v >>= \case
|
|
|
|
Just b -> pure b
|
|
|
|
_ -> throwError $ "Expected a string, but saw: " ++ show v
|
2018-04-16 19:56:29 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, MonadEffects m)
|
2018-04-16 19:56:29 +02:00
|
|
|
=> FromValue (Text, DList Text) m (NValueNF m) where
|
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF (NUri u)) -> pure $ Just (u, mempty)
|
|
|
|
Fix (NVStrF t d) -> pure $ Just (t, d)
|
|
|
|
Fix (NVPathF p) -> Just . (,mempty) . Text.pack . unStorePath <$> addPath p
|
|
|
|
Fix (NVSetF s _) -> case M.lookup "outPath" s of
|
2018-04-18 04:10:36 +02:00
|
|
|
Nothing -> pure Nothing
|
2018-04-18 06:49:29 +02:00
|
|
|
Just p -> fmap (,mempty) <$> fromValueMay @Text p
|
2018-04-16 19:56:29 +02:00
|
|
|
_ -> pure Nothing
|
|
|
|
fromValue v = fromValueMay v >>= \case
|
|
|
|
Just b -> pure b
|
|
|
|
_ -> throwError $ "Expected a string, but saw: " ++ show v
|
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, MonadEffects m,
|
|
|
|
FromValue Text m r, Show (NValueF m r))
|
|
|
|
=> FromValue (Text, DList Text) m (NValueF m r) where
|
2018-04-16 19:56:29 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF (NUri u) -> pure $ Just (u, mempty)
|
|
|
|
NVStrF t d -> pure $ Just (t, d)
|
|
|
|
NVPathF p -> Just . (,mempty) . Text.pack . unStorePath <$> addPath p
|
|
|
|
NVSetF s _ -> case M.lookup "outPath" s of
|
2018-04-18 04:10:36 +02:00
|
|
|
Nothing -> pure Nothing
|
2018-04-18 06:49:29 +02:00
|
|
|
Just p -> fmap (,mempty) <$> fromValueMay @Text p
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a string, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue ByteString m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVStrF t _) -> pure $ Just (encodeUtf8 t)
|
2018-04-15 07:58:50 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-15 07:58:50 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a string, but saw: " ++ show v
|
2018-04-15 07:58:50 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue ByteString m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVStrF t _ -> pure $ Just (encodeUtf8 t)
|
2018-04-15 07:58:50 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-15 07:58:50 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a string, but saw: " ++ show v
|
2018-04-15 07:58:50 +02:00
|
|
|
|
2018-04-14 03:09:12 +02:00
|
|
|
newtype Path = Path { getPath :: FilePath }
|
|
|
|
deriving Show
|
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue Path m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF (NUri u)) -> pure $ Just (Path (Text.unpack u))
|
|
|
|
Fix (NVPathF p) -> pure $ Just (Path p)
|
|
|
|
Fix (NVStrF s _) -> pure $ Just (Path (Text.unpack s))
|
|
|
|
Fix (NVSetF s _) -> case M.lookup "outPath" s of
|
2018-04-18 06:49:29 +02:00
|
|
|
Nothing -> pure Nothing
|
|
|
|
Just p -> fromValueMay @Path p
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a path, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Convertible e m, FromValue Path m r, Show (NValueF m r))
|
2018-04-22 03:18:03 +02:00
|
|
|
=> FromValue Path m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVConstantF (NUri u) -> pure $ Just (Path (Text.unpack u))
|
|
|
|
NVPathF p -> pure $ Just (Path p)
|
|
|
|
NVStrF s _ -> pure $ Just (Path (Text.unpack s))
|
|
|
|
NVSetF s _ -> case M.lookup "outPath" s of
|
2018-04-18 06:49:29 +02:00
|
|
|
Nothing -> pure Nothing
|
|
|
|
Just p -> fromValueMay @Path p
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a path, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m,
|
2018-04-16 04:05:44 +02:00
|
|
|
FromValue a m (NValueNF m), Show a)
|
|
|
|
=> FromValue [a] m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVListF l) -> sequence <$> traverse fromValueMay l
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue [r] m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVListF l -> pure $ Just l
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue (HashMap Text (NValueNF m)) m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVSetF s _) -> pure $ Just s
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue (HashMap Text r) m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVSetF s _ -> pure $ Just s
|
2018-04-14 03:09:12 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue (HashMap Text (NValueNF m),
|
2018-04-16 01:21:47 +02:00
|
|
|
HashMap Text SourcePos) m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVSetF s p) -> pure $ Just (s, p)
|
2018-04-15 08:52:21 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-15 08:52:21 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-15 08:52:21 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, Show (NValueF m r))
|
|
|
|
=> FromValue (HashMap Text r,
|
|
|
|
HashMap Text SourcePos) m (NValueF m r) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVSetF s p -> pure $ Just (s, p)
|
2018-04-15 08:52:21 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-15 08:52:21 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-15 08:52:21 +02:00
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (MonadThunk (NValue m) (NThunk m) m, Convertible e m)
|
|
|
|
=> FromValue (NThunk m) m (NValueF m (NThunk m)) where
|
|
|
|
fromValueMay = pure . Just . value @_ @_ @m . NValue Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-15 07:58:50 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected a thunk, but saw: " ++ show v
|
2018-04-15 07:58:50 +02:00
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Monad m, FromValue a m v) => FromValue a m (m v) where
|
2018-04-17 06:39:41 +02:00
|
|
|
fromValueMay = (>>= fromValueMay)
|
|
|
|
fromValue = (>>= fromValue)
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (MonadThunk (NValue m) (NThunk m) m,
|
|
|
|
FromValue a m (NValueF m (NThunk m)))
|
2018-04-17 06:39:41 +02:00
|
|
|
=> FromValue a m (NThunk m) where
|
|
|
|
fromValueMay = force ?? fromValueMay
|
|
|
|
fromValue = force ?? fromValue
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance FromValue a m (NValueF m (NThunk m))
|
|
|
|
=> FromValue a m (NValue m) where
|
|
|
|
fromValueMay = fromValueMay . baseValue
|
|
|
|
fromValue = fromValue . baseValue
|
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, MonadEffects m)
|
2018-04-16 04:05:44 +02:00
|
|
|
=> FromValue A.Value m (NValueNF m) where
|
2018-04-16 05:22:24 +02:00
|
|
|
fromValueMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVConstantF a) -> pure $ Just $ case a of
|
2018-04-14 03:09:12 +02:00
|
|
|
NInt n -> toJSON n
|
|
|
|
NFloat n -> toJSON n
|
|
|
|
NBool b -> toJSON b
|
|
|
|
NNull -> A.Null
|
|
|
|
NUri u -> toJSON u
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVStrF s _) -> pure $ Just $ toJSON s
|
|
|
|
Fix (NVListF l) -> fmap (A.Array . V.fromList) . sequence
|
2018-04-16 05:22:24 +02:00
|
|
|
<$> traverse fromValueMay l
|
2018-04-22 23:32:55 +02:00
|
|
|
Fix (NVSetF m _) -> fmap A.Object . sequence <$> traverse fromValueMay m
|
|
|
|
Fix NVClosureF {} -> pure Nothing
|
|
|
|
Fix (NVPathF p) -> Just . toJSON . unStorePath <$> addPath p
|
|
|
|
Fix (NVBuiltinF _ _) -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromValue v = fromValueMay v >>= \case
|
2018-04-14 03:09:12 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Cannot convert value to JSON: " ++ show v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
class ToValue a m v where
|
|
|
|
toValue :: a -> m v
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 08:00:09 +02:00
|
|
|
instance Applicative m => ToValue () m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue _ = pure . Fix . NVConstantF $ NNull
|
2018-04-16 08:00:09 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue () m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue _ = pure . NVConstantF $ NNull
|
2018-04-16 08:00:09 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue Bool m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . NVConstantF . NBool
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue Bool m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . NVConstantF . NBool
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue Int m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . NVConstantF . NInt . toInteger
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue Int m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . NVConstantF . NInt . toInteger
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue Integer m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . NVConstantF . NInt
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue Integer m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . NVConstantF . NInt
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue Float m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . NVConstantF . NFloat
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue Float m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . NVConstantF . NFloat
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue Text m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . flip NVStrF mempty
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue Text m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . flip NVStrF mempty
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 19:56:29 +02:00
|
|
|
instance Applicative m => ToValue (Text, DList Text) m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . uncurry NVStrF
|
2018-04-16 19:56:29 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue (Text, DList Text) m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . uncurry NVStrF
|
2018-04-16 19:56:29 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue ByteString m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . flip NVStrF mempty . decodeUtf8
|
2018-04-15 07:58:50 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue ByteString m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . flip NVStrF mempty . decodeUtf8
|
2018-04-15 07:58:50 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue Path m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . NVPathF . getPath
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue Path m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . NVPathF . getPath
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance MonadThunk (NValue m) (NThunk m) m
|
|
|
|
=> ToValue SourcePos m (NValueF m (NThunk m)) where
|
2018-04-16 04:05:44 +02:00
|
|
|
toValue (SourcePos f l c) = do
|
2018-04-22 23:32:55 +02:00
|
|
|
f' <- NValue Nothing <$> toValue (Text.pack f)
|
|
|
|
l' <- NValue Nothing <$> toValue (unPos l)
|
|
|
|
c' <- NValue Nothing <$> toValue (unPos c)
|
2018-04-16 01:21:47 +02:00
|
|
|
let pos = M.fromList
|
2018-04-15 10:43:01 +02:00
|
|
|
[ ("file" :: Text, value @_ @_ @m f')
|
|
|
|
, ("line", value @_ @_ @m l')
|
|
|
|
, ("column", value @_ @_ @m c') ]
|
2018-04-22 23:32:55 +02:00
|
|
|
pure $ NVSetF pos mempty
|
2018-04-15 08:52:21 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance (ToValue a m (NValueNF m), Applicative m)
|
|
|
|
=> ToValue [a] m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = fmap (Fix . NVListF) . traverse toValue
|
2018-04-15 10:43:01 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue [r] m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . NVListF
|
2018-04-15 10:43:01 +02:00
|
|
|
|
2018-04-16 01:21:47 +02:00
|
|
|
instance Applicative m
|
2018-04-16 04:05:44 +02:00
|
|
|
=> ToValue (HashMap Text (NValueNF m)) m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . Fix . flip NVSetF M.empty
|
2018-04-15 08:52:21 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue (HashMap Text r) m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue = pure . flip NVSetF M.empty
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue (HashMap Text (NValueNF m),
|
2018-04-16 01:21:47 +02:00
|
|
|
HashMap Text SourcePos) m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue (s, p) = pure $ Fix $ NVSetF s p
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToValue (HashMap Text r,
|
|
|
|
HashMap Text SourcePos) m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
toValue (s, p) = pure $ NVSetF s p
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance (MonadThunk (NValue m) (NThunk m) m, ToValue a m (NValue m))
|
|
|
|
=> ToValue a m (NThunk m) where
|
|
|
|
toValue = fmap (value @(NValue m) @_ @m) . toValue
|
2018-04-14 03:09:12 +02:00
|
|
|
|
2018-04-16 04:05:44 +02:00
|
|
|
instance Applicative m => ToValue Bool m (NExprF r) where
|
|
|
|
toValue = pure . NConstant . NBool
|
2018-04-15 07:58:50 +02:00
|
|
|
|
2018-04-16 08:00:09 +02:00
|
|
|
instance Applicative m => ToValue () m (NExprF r) where
|
|
|
|
toValue _ = pure . NConstant $ NNull
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Framed e m, MonadThunk (NValue m) (NThunk m) m)
|
|
|
|
=> ToValue A.Value m (NValueF m (NThunk m)) where
|
2018-04-16 04:05:44 +02:00
|
|
|
toValue = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
A.Object m -> flip NVSetF M.empty
|
|
|
|
<$> traverse (thunk . fmap (NValue Nothing)
|
|
|
|
. toValue @_ @_ @(NValueF m (NThunk m))) m
|
|
|
|
A.Array l -> NVListF <$>
|
2018-04-19 01:12:31 +02:00
|
|
|
traverse (thunk . withStringContext "While coercing to a JSON value"
|
|
|
|
. toValue) (V.toList l)
|
2018-04-22 23:32:55 +02:00
|
|
|
A.String s -> pure $ NVStrF s mempty
|
|
|
|
A.Number n -> pure $ NVConstantF $ case floatingOrInteger n of
|
2018-04-15 07:58:50 +02:00
|
|
|
Left r -> NFloat r
|
|
|
|
Right i -> NInt i
|
2018-04-22 23:32:55 +02:00
|
|
|
A.Bool b -> pure $ NVConstantF $ NBool b
|
|
|
|
A.Null -> pure $ NVConstantF NNull
|
|
|
|
|
|
|
|
instance (MonadThunk (NValue m) (NThunk m) m,
|
|
|
|
ToValue a m (NValueF m (NThunk m)))
|
|
|
|
=> ToValue a m (NValue m) where
|
|
|
|
toValue = fmap (NValue Nothing) . toValue
|
2018-04-16 05:43:04 +02:00
|
|
|
|
|
|
|
class FromNix a m v where
|
|
|
|
fromNix :: v -> m a
|
|
|
|
default fromNix :: FromValue a m v => v -> m a
|
|
|
|
fromNix = fromValue
|
|
|
|
|
|
|
|
fromNixMay :: v -> m (Maybe a)
|
|
|
|
default fromNixMay :: FromValue a m v => v -> m (Maybe a)
|
|
|
|
fromNixMay = fromValueMay
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Convertible e m, MonadThunk (NValue m) (NThunk m) m,
|
|
|
|
FromNix a m (NValueF m (NThunk m)), Show a)
|
|
|
|
=> FromNix [a] m (NValueF m (NThunk m)) where
|
2018-04-16 05:43:04 +02:00
|
|
|
fromNixMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVListF l -> sequence <$> traverse (`force` fromNixMay . baseValue) l
|
2018-04-16 05:43:04 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromNix v = fromNixMay v >>= \case
|
2018-04-16 05:43:04 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-16 05:43:04 +02:00
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Convertible e m, MonadThunk (NValue m) (NThunk m) m,
|
|
|
|
FromNix a m (NValueF m (NThunk m)), Show a)
|
|
|
|
=> FromNix (HashMap Text a) m (NValueF m (NThunk m)) where
|
2018-04-16 05:43:04 +02:00
|
|
|
fromNixMay = \case
|
2018-04-22 23:32:55 +02:00
|
|
|
NVSetF s _ -> sequence <$> traverse (`force` fromNixMay . baseValue) s
|
2018-04-16 05:43:04 +02:00
|
|
|
_ -> pure Nothing
|
2018-04-16 19:54:55 +02:00
|
|
|
fromNix v = fromNixMay v >>= \case
|
2018-04-16 05:43:04 +02:00
|
|
|
Just b -> pure b
|
2018-04-16 19:54:55 +02:00
|
|
|
_ -> throwError $ "Expected an attrset, but saw: " ++ show v
|
2018-04-16 05:43:04 +02:00
|
|
|
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m => FromNix () m (NValueNF m) where
|
|
|
|
instance (Convertible e m, Show (NValueF m r)) => FromNix () m (NValueF m r) where
|
|
|
|
instance Convertible e m => FromNix Bool m (NValueNF m) where
|
|
|
|
instance (Convertible e m, Show (NValueF m r)) => FromNix Bool m (NValueF m r) where
|
|
|
|
instance Convertible e m => FromNix Int m (NValueNF m) where
|
|
|
|
instance (Convertible e m, Show (NValueF m r)) => FromNix Int m (NValueF m r) where
|
|
|
|
instance Convertible e m => FromNix Integer m (NValueNF m) where
|
|
|
|
instance (Convertible e m, Show (NValueF m r)) => FromNix Integer m (NValueF m r) where
|
|
|
|
instance Convertible e m => FromNix Float m (NValueNF m) where
|
|
|
|
instance (Convertible e m, Show (NValueF m r)) => FromNix Float m (NValueF m r) where
|
|
|
|
instance (Convertible e m, MonadEffects m) => FromNix Text m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Convertible e m, MonadEffects m, MonadThunk (NValue m) (NThunk m) m, FromValue Text m r, Show (NValueF m r)) => FromNix Text m (NValueF m r) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, MonadEffects m) => FromNix (Text, DList Text) m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Convertible e m, MonadEffects m, MonadThunk (NValue m) (NThunk m) m, FromValue Text m r, Show (NValueF m r)) => FromNix (Text, DList Text) m (NValueF m r) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Convertible e m => FromNix ByteString m (NValueNF m) where
|
|
|
|
instance (Convertible e m, Show (NValueF m r)) => FromNix ByteString m (NValueF m r) where
|
|
|
|
instance Convertible e m => FromNix Path m (NValueNF m) where
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Convertible e m, MonadThunk (NValue m) (NThunk m) m) => FromNix Path m (NValueF m (NThunk m)) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance (Convertible e m, FromValue a m (NValueNF m), Show a) => FromNix [a] m (NValueNF m) where
|
|
|
|
instance Convertible e m => FromNix (HashMap Text (NValueNF m)) m (NValueNF m) where
|
|
|
|
instance Convertible e m => FromNix (HashMap Text (NValueNF m), HashMap Text SourcePos) m (NValueNF m) where
|
|
|
|
instance (Convertible e m, Show (NValueF m r)) => FromNix (HashMap Text r, HashMap Text SourcePos) m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Convertible e m, MonadEffects m, MonadThunk (NValue m) (NThunk m) m) => FromNix A.Value m (NValueNF m) where
|
|
|
|
|
|
|
|
instance (Convertible e m, MonadEffects m, MonadThunk (NValue m) (NThunk m) m) => FromNix A.Value m (NValueF m (NThunk m)) where
|
|
|
|
fromNixMay = fromNixMay <=< normalForm . NValue Nothing
|
|
|
|
fromNix = fromNix <=< normalForm . NValue Nothing
|
|
|
|
|
|
|
|
instance FromNix a m (NValueF m (NThunk m)) => FromNix a m (NValue m) where
|
|
|
|
fromNixMay = fromNixMay . baseValue
|
|
|
|
fromNix = fromNix . baseValue
|
2018-04-22 03:18:03 +02:00
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Monad m, FromNix a m v) => FromNix a m (m v) where
|
2018-04-17 06:39:41 +02:00
|
|
|
fromNixMay = (>>= fromNixMay)
|
|
|
|
fromNix = (>>= fromNix)
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (MonadThunk (NValue m) (NThunk m) m, FromNix a m (NValue m))
|
|
|
|
=> FromNix a m (NThunk m) where
|
2018-04-16 08:00:09 +02:00
|
|
|
fromNixMay = force ?? fromNixMay
|
|
|
|
fromNix = force ?? fromNix
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance MonadThunk (NValue m) (NThunk m) m
|
|
|
|
=> FromNix (NThunk m) m (NValueF m (NThunk m)) where
|
|
|
|
fromNixMay = pure . Just . value . NValue Nothing
|
|
|
|
fromNix = pure . value . NValue Nothing
|
|
|
|
|
2018-04-16 05:43:04 +02:00
|
|
|
class ToNix a m v where
|
|
|
|
toNix :: a -> m v
|
|
|
|
default toNix :: ToValue a m v => a -> m v
|
|
|
|
toNix = toValue
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Framed e m, MonadThunk (NValue m) (NThunk m) m,
|
|
|
|
ToNix a m (NValueF m (NThunk m)))
|
|
|
|
=> ToNix [a] m (NValueF m (NThunk m)) where
|
|
|
|
toNix = fmap NVListF
|
|
|
|
. traverse (thunk . withStringContext "While coercing to a list"
|
|
|
|
. fmap (NValue Nothing)
|
|
|
|
. toNix)
|
|
|
|
|
|
|
|
instance (Framed e m, MonadThunk (NValue m) (NThunk m) m,
|
|
|
|
ToNix a m (NValueF m (NThunk m)))
|
|
|
|
=> ToNix (HashMap Text a) m (NValueF m (NThunk m)) where
|
|
|
|
toNix = fmap (flip NVSetF M.empty)
|
|
|
|
. traverse (thunk . withStringContext "While coercing to a set"
|
|
|
|
. fmap (NValue Nothing)
|
|
|
|
. toNix)
|
2018-04-16 05:43:04 +02:00
|
|
|
|
2018-04-16 08:00:09 +02:00
|
|
|
instance Applicative m => ToNix () m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix () m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix Bool m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix Bool m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix Int m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix Int m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix Integer m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix Integer m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix Float m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix Float m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix Text m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix Text m (NValueF m r) where
|
2018-04-16 19:56:29 +02:00
|
|
|
instance Applicative m => ToNix (Text, DList Text) m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix (Text, DList Text) m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix ByteString m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix ByteString m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix Path m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix Path m (NValueF m r) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix (HashMap Text (NValueNF m)) m (NValueNF m) where
|
|
|
|
instance Applicative m => ToNix (HashMap Text (NValueNF m), HashMap Text SourcePos) m (NValueNF m) where
|
2018-04-22 03:18:03 +02:00
|
|
|
instance Applicative m => ToNix (HashMap Text r, HashMap Text SourcePos) m (NValueF m r) where
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (Framed e m, MonadThunk (NValue m) (NThunk m) m) => ToNix A.Value m (NValueF m (NThunk m)) where
|
2018-04-16 05:43:04 +02:00
|
|
|
instance Applicative m => ToNix Bool m (NExprF r) where
|
2018-04-16 08:00:09 +02:00
|
|
|
instance Applicative m => ToNix () m (NExprF r) where
|
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
instance (MonadThunk (NValue m) (NThunk m) m, ToNix a m (NValueF m (NThunk m)))
|
|
|
|
=> ToNix a m (NThunk m) where
|
|
|
|
toNix = thunk . fmap (NValue Nothing) . toNix
|
|
|
|
|
|
|
|
instance (MonadThunk (NValue m) (NThunk m) m, ToNix a m (NValueF m (NThunk m)))
|
|
|
|
=> ToNix a m (NValue m) where
|
|
|
|
toNix = fmap (NValue Nothing) . toNix
|
|
|
|
|
|
|
|
instance (Applicative m, ToNix a m (NValueNF m)) => ToNix [a] m (NValueNF m) where
|
|
|
|
toNix = fmap (Fix . NVListF) . traverse toNix
|
|
|
|
|
|
|
|
instance MonadThunk (NValue m) (NThunk m) m => ToNix (NThunk m) m (NValue m) where
|
2018-04-16 08:00:09 +02:00
|
|
|
toNix = force ?? pure
|
2018-04-22 23:32:55 +02:00
|
|
|
|
|
|
|
instance MonadThunk (NValue m) (NThunk m) m
|
|
|
|
=> ToNix (NThunk m) m (NValueF m (NThunk m)) where
|
|
|
|
toNix = force ?? (pure . baseValue)
|