Skip to content

Commit 7ef6fed

Browse files
Test schema of KDL.fail
1 parent 61e113c commit 7ef6fed

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

src/Data/KDL/Decoder/Internal/Monad.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ withDecoder decoder f = decoder >>> liftDecodeM f
178178
-- returnA -< x
179179
-- @
180180
fail :: forall b o. Decoder o Text b
181-
fail = liftDecodeM failM
181+
fail = Decoder (SchemaOr []) (Trans.lift . failM)
182182

183183
-- | Debug the current state of the object being decoded.
184184
--

src/Data/KDL/Decoder/Schema.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ data instance SchemaItem Value
6868
schemaJoin :: Schema a -> Schema a -> Schema a
6969
schemaJoin = curry $ \case
7070
(SchemaAnd l, SchemaAnd r) -> SchemaAnd (l <> r)
71+
(l, SchemaAnd []) -> l
7172
(l, SchemaAnd r) -> SchemaAnd (l : r)
73+
(SchemaAnd [], r) -> r
7274
(SchemaAnd l, r) -> SchemaAnd (l <> [r])
7375
(l, r) -> SchemaAnd [l, r]
7476

7577
schemaAlt :: Schema a -> Schema a -> Schema a
7678
schemaAlt = curry $ \case
7779
(SchemaOr l, SchemaOr r) -> SchemaOr (l <> r)
80+
(l, SchemaOr []) -> l
7881
(l, SchemaOr r) -> SchemaOr (l : r)
79-
(SchemaOr l, r) -> SchemaOr (r : l)
82+
(SchemaOr [], r) -> r
83+
(SchemaOr l, r) -> SchemaOr (l <> [r])
8084
(l, r) -> SchemaOr [l, r]

test/Data/KDL/Decoder/ArrowSpec.hs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Data.KDL.Decoder.ArrowSpec (spec) where
66

77
import Control.Arrow (returnA)
88
import Control.Monad (forM_, unless, when)
9+
import Data.Int (Int64)
910
import Data.KDL.Arrow qualified as KDL
1011
import Data.KDL.Types (
1112
Entry (..),
@@ -1046,12 +1047,21 @@ schemaSpec = do
10461047
let decoder = KDL.document $ proc () -> do
10471048
x <- KDL.nodeWith "foo" $ show <$> KDL.argWith decodeFoo -< ()
10481049
ys <- KDL.many $ KDL.nodeWith "bar" $ KDL.arg @String -< ()
1049-
returnA -< (x, ys)
1050+
1051+
zType <- KDL.argAt @Text "baz_type" -< ()
1052+
z <- KDL.argAtWith "baz" decodeBaz -< zType
1053+
1054+
returnA -< (x, ys, z)
10501055
decodeFoo =
10511056
KDL.oneOf
10521057
[ Left <$> KDL.valueDecoder @Bool
10531058
, Right <$> KDL.valueDecoder @Text
10541059
]
1060+
decodeBaz = proc zType -> do
1061+
case zType of
1062+
"int" -> KDL.valueDecoder @Int64 -< ()
1063+
"bool" -> (\b -> if b then 1 else 0) <$> KDL.valueDecoder @Bool -< ()
1064+
_ -> KDL.fail -< "Invalid type: " <> zType
10551065
expected =
10561066
KDL.SchemaAnd
10571067
[ KDL.SchemaOne . KDL.NodeNamed "foo" $
@@ -1085,6 +1095,34 @@ schemaSpec = do
10851095
}
10861096
, KDL.SchemaAnd []
10871097
]
1098+
, KDL.SchemaOne . KDL.NodeNamed "baz_type" $
1099+
KDL.TypedNodeSchema
1100+
{ typeHint = typeRep $ Proxy @Text
1101+
, validTypeAnns = []
1102+
, nodeSchema =
1103+
KDL.SchemaOne . KDL.NodeArg $
1104+
KDL.TypedValueSchema
1105+
{ typeHint = typeRep $ Proxy @Text
1106+
, validTypeAnns = ["text"]
1107+
, dataSchema = KDL.SchemaOne KDL.TextSchema
1108+
}
1109+
}
1110+
, KDL.SchemaOne . KDL.NodeNamed "baz" $
1111+
KDL.TypedNodeSchema
1112+
{ typeHint = typeRep $ Proxy @Int64
1113+
, validTypeAnns = []
1114+
, nodeSchema =
1115+
KDL.SchemaOne . KDL.NodeArg $
1116+
KDL.TypedValueSchema
1117+
{ typeHint = typeRep $ Proxy @Int64
1118+
, validTypeAnns = []
1119+
, dataSchema =
1120+
KDL.SchemaOr
1121+
[ KDL.SchemaOne KDL.NumberSchema
1122+
, KDL.SchemaOne KDL.BoolSchema
1123+
]
1124+
}
1125+
}
10881126
]
10891127
KDL.documentSchema decoder `shouldBe` expected
10901128

0 commit comments

Comments
 (0)