Move json code into separate module
This commit is contained in:
parent
df26aa12e9
commit
7d2f9d874a
|
@ -454,6 +454,7 @@ library
|
|||
Nix.Expr.Types
|
||||
Nix.Expr.Types.Annotated
|
||||
Nix.Frames
|
||||
Nix.Json
|
||||
Nix.Lint
|
||||
Nix.Normal
|
||||
Nix.Options
|
||||
|
|
|
@ -29,6 +29,7 @@ import Data.Text.Prettyprint.Doc.Render.Text
|
|||
import Nix
|
||||
import Nix.Convert
|
||||
import qualified Nix.Eval as Eval
|
||||
import Nix.Json
|
||||
-- import Nix.Lint
|
||||
import Nix.Options.Parser
|
||||
import qualified Nix.Type.Env as Env
|
||||
|
@ -145,7 +146,8 @@ main = do
|
|||
. TL.decodeUtf8
|
||||
. A.encodingToLazyByteString
|
||||
. toEncodingSorted
|
||||
<=< fromNix
|
||||
. snd
|
||||
<=< nvalueToJSON
|
||||
| strict opts =
|
||||
liftIO . print . prettyNValueNF <=< normalForm
|
||||
| values opts =
|
||||
|
|
|
@ -57,7 +57,6 @@ import Data.Char (isDigit)
|
|||
import Data.Fix
|
||||
import Data.Foldable (foldrM)
|
||||
import qualified Data.HashMap.Lazy as M
|
||||
import qualified Data.HashSet as HS
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
import Data.Scientific
|
||||
|
@ -81,6 +80,7 @@ import Nix.Exec
|
|||
import Nix.Expr.Types
|
||||
import Nix.Expr.Types.Annotated
|
||||
import Nix.Frames
|
||||
import Nix.Json
|
||||
import Nix.Normal
|
||||
import Nix.Options
|
||||
import Nix.Parser hiding (nixPath)
|
||||
|
@ -255,7 +255,7 @@ builtinsList = sequence [
|
|||
, add2 Normal "sort" sort_
|
||||
, add2 Normal "split" split_
|
||||
, add Normal "splitVersion" splitVersion_
|
||||
, add0 Normal "storeDir" (return $ nvPath "/nix/store")
|
||||
, add0 Normal "storeDir" (return $ nvStr $ principledMakeNixStringWithoutContext "/nix/store")
|
||||
, add' Normal "stringLength" (arity1 $ Text.length . principledStringIgnoreContext)
|
||||
, add' Normal "sub" (arity2 ((-) @Integer))
|
||||
, add' Normal "substring" substring
|
||||
|
@ -1038,29 +1038,6 @@ prim_toJSON x = do
|
|||
let t = decodeUtf8 $ LBS.toStrict $ A.encodingToLazyByteString $ toEncodingSorted v
|
||||
pure $ nvStr $ principledMakeNixString t ctx
|
||||
|
||||
nvalueToJSON
|
||||
:: MonadNix e m
|
||||
=> NValue m
|
||||
-> m (HS.HashSet StringContext, A.Value)
|
||||
nvalueToJSON v = case v of
|
||||
NVConstant a -> retEmpty $ case a of
|
||||
NInt n -> A.toJSON n
|
||||
NFloat n -> A.toJSON n
|
||||
NBool b -> A.toJSON b
|
||||
NNull -> A.Null
|
||||
NVStr ns -> pure (principledGetContext ns, A.toJSON $ principledStringIgnoreContext ns)
|
||||
NVList l -> do
|
||||
(ctxs, vals) <- unzip <$> traverse (`force` nvalueToJSON) l
|
||||
return (HS.unions ctxs, A.Array $ V.fromList vals)
|
||||
NVSet m _ ->
|
||||
fmap A.Object . sequence <$> traverse (`force` nvalueToJSON) m
|
||||
NVPath p -> do
|
||||
fp <- unStorePath <$> addPath p
|
||||
return (HS.singleton $ StringContext (Text.pack fp) DirectPath, A.toJSON fp)
|
||||
_ -> throwError $ CoercionToJson v
|
||||
where
|
||||
retEmpty a = pure (mempty, a)
|
||||
|
||||
toXML_ :: MonadNix e m => m (NValue m) -> m (NValue m)
|
||||
toXML_ v = v >>= normalForm >>= \x ->
|
||||
pure $ nvStr $ hackyMakeNixStringWithoutContext $ Text.pack (toXML x)
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
{-# LANGUAGE FlexibleContexts #-}
|
||||
|
||||
module Nix.Json where
|
||||
|
||||
import qualified Data.Aeson as A
|
||||
import qualified Data.HashSet as HS
|
||||
import qualified Data.Text as Text
|
||||
import qualified Data.Vector as V
|
||||
import Nix.Atoms
|
||||
import Nix.Effects
|
||||
import Nix.Exec
|
||||
import Nix.Frames
|
||||
import Nix.String
|
||||
import Nix.Thunk
|
||||
import Nix.Value
|
||||
|
||||
nvalueToJSON
|
||||
:: MonadNix e m
|
||||
=> NValue m
|
||||
-> m (HS.HashSet StringContext, A.Value)
|
||||
nvalueToJSON v = case v of
|
||||
NVConstant a -> retEmpty $ case a of
|
||||
NInt n -> A.toJSON n
|
||||
NFloat n -> A.toJSON n
|
||||
NBool b -> A.toJSON b
|
||||
NNull -> A.Null
|
||||
NVStr ns -> pure (principledGetContext ns, A.toJSON $ principledStringIgnoreContext ns)
|
||||
NVList l -> do
|
||||
(ctxs, vals) <- unzip <$> traverse (`force` nvalueToJSON) l
|
||||
return (HS.unions ctxs, A.Array $ V.fromList vals)
|
||||
NVSet m _ ->
|
||||
fmap A.Object . sequence <$> traverse (`force` nvalueToJSON) m
|
||||
NVPath p -> do
|
||||
fp <- unStorePath <$> addPath p
|
||||
return (HS.singleton $ StringContext (Text.pack fp) DirectPath, A.toJSON fp)
|
||||
_ -> throwError $ CoercionToJson v
|
||||
where
|
||||
retEmpty a = pure (mempty, a)
|
Loading…
Reference in New Issue