module Hydra.Ext.Json.Eliminate where

import Hydra.All
import qualified Hydra.Ext.Json.Model as Json

import qualified Data.Map as M


expectArray :: Json.Value -> Flow s [Json.Value]
expectArray :: forall s. Value -> Flow s [Value]
expectArray Value
value = case Value
value of
  Json.ValueArray [Value]
els -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [Value]
els
  Value
_ -> forall (m :: * -> *) a1 a2.
(MonadFail m, Show a1) =>
String -> a1 -> m a2
unexpected String
"JSON array" Value
value

expectNumber :: Json.Value -> Flow s Double
expectNumber :: forall s. Value -> Flow s Double
expectNumber Value
value = case Value
value of
  Json.ValueNumber Double
d -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Double
d
  Value
_ -> forall (m :: * -> *) a1 a2.
(MonadFail m, Show a1) =>
String -> a1 -> m a2
unexpected String
"JSON number" Value
value

expectObject :: Json.Value -> Flow s (M.Map String Json.Value)
expectObject :: forall s. Value -> Flow s (Map String Value)
expectObject Value
value = case Value
value of
  Json.ValueObject Map String Value
m -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Map String Value
m
  Value
_ -> forall (m :: * -> *) a1 a2.
(MonadFail m, Show a1) =>
String -> a1 -> m a2
unexpected String
"JSON object" Value
value

expectString :: Json.Value -> Flow s String
expectString :: forall s. Value -> Flow s String
expectString Value
value = case Value
value of
  Json.ValueString String
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure String
s
  Value
_ -> forall (m :: * -> *) a1 a2.
(MonadFail m, Show a1) =>
String -> a1 -> m a2
unexpected String
"JSON string" Value
value

opt :: String -> M.Map String Json.Value -> Maybe Json.Value
opt :: String -> Map String Value -> Maybe Value
opt = forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup

optArray :: String -> M.Map String Json.Value -> Flow s (Maybe [Json.Value])
optArray :: forall s. String -> Map String Value -> Flow s (Maybe [Value])
optArray String
fname Map String Value
m = case String -> Map String Value -> Maybe Value
opt String
fname Map String Value
m of
  Maybe Value
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
  Just Value
a -> forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Value -> Flow s [Value]
expectArray Value
a

optString :: String -> M.Map String Json.Value -> Flow s (Maybe String)
optString :: forall s. String -> Map String Value -> Flow s (Maybe String)
optString String
fname Map String Value
m = case String -> Map String Value -> Maybe Value
opt String
fname Map String Value
m of
  Maybe Value
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
  Just Value
s -> forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Value -> Flow s String
expectString Value
s

require :: String -> M.Map String Json.Value -> Flow s Json.Value
require :: forall s. String -> Map String Value -> Flow s Value
require String
fname Map String Value
m = case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
fname Map String Value
m of
  Maybe Value
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"required attribute " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show String
fname forall a. [a] -> [a] -> [a]
++ String
" not found"
  Just Value
value -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
value

requireArray :: String -> M.Map String Json.Value -> Flow s [Json.Value]
requireArray :: forall s. String -> Map String Value -> Flow s [Value]
requireArray String
fname Map String Value
m = forall s. String -> Map String Value -> Flow s Value
require String
fname Map String Value
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s. Value -> Flow s [Value]
expectArray

requireNumber :: String -> M.Map String Json.Value -> Flow s Double
requireNumber :: forall s. String -> Map String Value -> Flow s Double
requireNumber String
fname Map String Value
m = forall s. String -> Map String Value -> Flow s Value
require String
fname Map String Value
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s. Value -> Flow s Double
expectNumber

requireString :: String -> M.Map String Json.Value -> Flow s String
requireString :: forall s. String -> Map String Value -> Flow s String
requireString String
fname Map String Value
m = forall s. String -> Map String Value -> Flow s Value
require String
fname Map String Value
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s. Value -> Flow s String
expectString