Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/KDL/Decoder/Arrow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module KDL.Decoder.Arrow (
ValueDecodeArrow,
DecodeValue (..),
any,
string,
text,
number,
bool,
Expand Down Expand Up @@ -424,7 +425,7 @@ argsAt name = withDecodeValue $ argsAtWith' name
-- email "a@example.com" "b@example.com"
-- """
-- decoder = KDL.document $ proc () -> do
-- KDL.argsAtWith "email" KDL.text -< ()
-- KDL.argsAtWith "email" KDL.string -< ()
-- KDL.decodeWith decoder config == Right ["a@example.com", "b@example.com"]
-- @
argsAtWith :: forall a b. (Typeable b) => Text -> ValueDecodeArrow a b -> NodeListDecodeArrow a [b]
Expand Down Expand Up @@ -470,7 +471,7 @@ dashChildrenAt name = withDecodeValue $ dashChildrenAtWith' name
-- }
-- """
-- decoder = KDL.document $ proc () -> do
-- KDL.dashChildrenAtWith "attendees" $ KDL.text -< ()
-- KDL.dashChildrenAtWith "attendees" $ KDL.string -< ()
-- KDL.decodeWith decoder config == Right [\"Alice", \"Bob"]
-- @
dashChildrenAtWith :: forall a b. (Typeable b) => Text -> ValueDecodeArrow a b -> NodeListDecodeArrow a [b]
Expand Down Expand Up @@ -665,7 +666,7 @@ arg = withDecodeValue argWith'
-- decoder = KDL.document $ proc () -> do
-- KDL.nodeWith "person" $ decodePerson -< ()
-- decodePerson = proc () -> do
-- name \<- KDL.argWith $ Text.toUpper \<$> KDL.text -< ()
-- name \<- KDL.argWith $ Text.toUpper \<$> KDL.string -< ()
-- vals \<- KDL.many $ KDL.argWith $ show \<$> KDL.valueDecoder @Int -< ()
-- returnA -< (name, vals)
-- KDL.decodeWith decoder config == Right (\"ALICE", ["1", "2", "3"])
Expand Down Expand Up @@ -910,11 +911,11 @@ instance DecodeValue Value where
instance DecodeValue ValueData where
valueDecoder = (.data_) <$> any
instance DecodeValue Text where
validValueTypeAnns _ = ["text"]
valueDecoder = text
validValueTypeAnns _ = ["string"]
valueDecoder = string
instance DecodeValue String where
validValueTypeAnns _ = ["string"]
valueDecoder = Text.unpack <$> text
valueDecoder = Text.unpack <$> string
instance DecodeValue Bool where
validValueTypeAnns _ = ["bool", "boolean"]
valueDecoder = bool
Expand Down Expand Up @@ -1018,11 +1019,16 @@ valueDataDecoderPrim schema f = DecodeArrow schema $ \_ -> Trans.lift . f =<< St
any :: DecodeArrow Value a Value
any = valueDataDecoderPrim (SchemaOr $ map SchemaOne [minBound .. maxBound]) pure

-- | Decode a KDL text value.
text :: DecodeArrow Value a Text
text = valueDataDecoderPrim (SchemaOne TextSchema) $ \case
-- | Decode a KDL string value.
string :: DecodeArrow Value a Text
string = valueDataDecoderPrim (SchemaOne TextSchema) $ \case
Value{data_ = String s} -> pure s
v -> decodeThrow DecodeError_ValueDecodeFail{expectedType = "text", value = v}
v -> decodeThrow DecodeError_ValueDecodeFail{expectedType = "string", value = v}

-- | Deprecated in favor of 'string'.
text :: DecodeArrow Value a Text
text = string
{-# DEPRECATED text "Use KDL.string instead" #-}

-- | Decode a KDL number value.
number :: DecodeArrow Value a Scientific
Expand Down
2 changes: 1 addition & 1 deletion test/KDL/ApplicativeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ spec = do
KDL.SchemaOne . KDL.NodeArg $
KDL.TypedValueSchema
{ typeHint = typeRep $ Proxy @Text
, validTypeAnns = ["text"]
, validTypeAnns = ["string"]
, dataSchema = KDL.SchemaOne KDL.TextSchema
}
}
Expand Down
46 changes: 23 additions & 23 deletions test/KDL/Decoder/ArrowSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ apiSpec = do
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected text, got: 1.0"
, " Expected string, got: 1.0"
]

-- Most behaviors tested with `nodeWith`
Expand Down Expand Up @@ -297,7 +297,7 @@ apiSpec = do
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected text, got: 1"
, " Expected string, got: 1"
]

-- Most behaviors tested with `argAt`
Expand All @@ -319,7 +319,7 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo (test)a"
decoder = KDL.document $ proc () -> do
KDL.argAtWith' "foo" anns KDL.text -< ()
KDL.argAtWith' "foo" anns KDL.string -< ()
KDL.decodeWith decoder config `shouldBe` Right "a"

it "decodes argument without an annotation" $ do
Expand All @@ -331,13 +331,13 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo a"
decoder = KDL.document $ proc () -> do
KDL.argAtWith' "foo" anns KDL.text -< ()
KDL.argAtWith' "foo" anns KDL.string -< ()
KDL.decodeWith decoder config `shouldBe` Right "a"

it "fails when argument has unexpected annotation" $ do
let config = "foo (test)a"
decoder = KDL.document $ proc () -> do
KDL.argAtWith' "foo" ["VAL"] KDL.text -< ()
KDL.argAtWith' "foo" ["VAL"] KDL.string -< ()
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
Expand Down Expand Up @@ -392,7 +392,7 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo (test)a (test)b"
decoder = KDL.document $ proc () -> do
KDL.argsAtWith' "foo" anns KDL.text -< ()
KDL.argsAtWith' "foo" anns KDL.string -< ()
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]

it "decodes arguments without an annotation" $ do
Expand All @@ -404,13 +404,13 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo a b"
decoder = KDL.document $ proc () -> do
KDL.argsAtWith' "foo" anns KDL.text -< ()
KDL.argsAtWith' "foo" anns KDL.string -< ()
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]

it "fails when argument has unexpected annotation" $ do
let config = "foo (VAL)a (test)b"
decoder = KDL.document $ proc () -> do
KDL.argsAtWith' "foo" ["VAL"] KDL.text -< ()
KDL.argsAtWith' "foo" ["VAL"] KDL.string -< ()
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #1"
Expand Down Expand Up @@ -485,7 +485,7 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo { - (test)a; - (test)b; }"
decoder = KDL.document $ proc () -> do
KDL.dashChildrenAtWith' "foo" anns KDL.text -< ()
KDL.dashChildrenAtWith' "foo" anns KDL.string -< ()
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]

it "decodes dash children without an annotation" $ do
Expand All @@ -497,13 +497,13 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo { - a; - b; }"
decoder = KDL.document $ proc () -> do
KDL.dashChildrenAtWith' "foo" anns KDL.text -< ()
KDL.dashChildrenAtWith' "foo" anns KDL.string -< ()
KDL.decodeWith decoder config `shouldBe` Right ["a", "b"]

it "fails when child has unexpected annotation" $ do
let config = "foo { - (test)a; }"
decoder = KDL.document $ proc () -> do
KDL.dashChildrenAtWith' "foo" ["VAL"] KDL.text -< ()
KDL.dashChildrenAtWith' "foo" ["VAL"] KDL.string -< ()
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > - #0 > arg #0"
Expand Down Expand Up @@ -631,7 +631,7 @@ apiSpec = do
it "decodes an argument" $ do
let config = "foo bar"
decoder = proc () -> do
KDL.argWith KDL.text -< ()
KDL.argWith KDL.string -< ()
decodeNode "foo" decoder config `shouldBe` Right "bar"

-- Most behaviors tested with `argWith`
Expand All @@ -645,7 +645,7 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo (test)a"
decoder = proc () -> do
KDL.argWith' anns KDL.text -< ()
KDL.argWith' anns KDL.string -< ()
decodeNode "foo" decoder config `shouldBe` Right "a"

it "decodes argument without an annotation" $ do
Expand All @@ -657,13 +657,13 @@ apiSpec = do
forM_ testCases $ \anns -> do
let config = "foo a"
decoder = proc () -> do
KDL.argWith' anns KDL.text -< ()
KDL.argWith' anns KDL.string -< ()
decodeNode "foo" decoder config `shouldBe` Right "a"

it "fails when argument has unexpected annotation" $ do
let config = "foo (test)a"
decoder = proc () -> do
KDL.argWith' ["VAL"] KDL.text -< ()
KDL.argWith' ["VAL"] KDL.string -< ()
decodeNode "foo" decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
Expand Down Expand Up @@ -896,21 +896,21 @@ apiSpec = do
KDL.decodeWith decoder config
`shouldBe` Right [val $ Number 1, val $ String "asdf", val $ Bool True]

describe "text" $ do
it "decodes text value" $ do
describe "string" $ do
it "decodes string value" $ do
let config = "foo asdf"
decoder = KDL.document $ proc () -> do
KDL.argAtWith "foo" KDL.text -< ()
KDL.argAtWith "foo" KDL.string -< ()
KDL.decodeWith decoder config `shouldBe` Right "asdf"

it "fails when value is not text" $ do
it "fails when value is not string" $ do
let config = "foo 1"
decoder = KDL.document $ proc () -> do
KDL.argAtWith "foo" KDL.text -< ()
KDL.argAtWith "foo" KDL.string -< ()
KDL.decodeWith decoder config
`shouldSatisfy` decodeErrorMsg
[ "At: foo #0 > arg #0"
, " Expected text, got: 1"
, " Expected string, got: 1"
]

describe "number" $ do
Expand Down Expand Up @@ -970,7 +970,7 @@ apiSpec = do
let config = "foo 123 hello"
decoder = KDL.document $ proc () -> do
KDL.nodeWith "foo" . KDL.many . KDL.argWith $
KDL.oneOf [Left <$> KDL.number, Right <$> KDL.text]
KDL.oneOf [Left <$> KDL.number, Right <$> KDL.string]
-<
()
KDL.decodeWith decoder config `shouldBe` Right [Left 123, Right "hello"]
Expand Down Expand Up @@ -1059,7 +1059,7 @@ schemaSpec = do
KDL.SchemaOne . KDL.NodeArg $
KDL.TypedValueSchema
{ typeHint = typeRep $ Proxy @Text
, validTypeAnns = ["text"]
, validTypeAnns = ["string"]
, dataSchema = KDL.SchemaOne KDL.TextSchema
}
}
Expand Down
Loading