on master help sign in
Decode empty inputs as Null

In my opinion, empty and comment only files are perfectly valid inputs for `Data.Yaml.Config.loadYamlSettings` and I don't see why they'd not be valid inputs for `Data.Yaml.decode` as well so I fixed it there.

commitd52b6412cf1f7d1ae22663defc605433ac807b60 authorTomas Janousek <tomi@nomi.cz> dateFri 25 Nov 2016, 11:29 (10 years ago) parentb0f2f34
4 files changed+38−8
Data/Yaml/Internal.hs+18−6 test/Data/YamlSpec.hs+19−2 test/resources/empty.yaml test/resources/empty2.yaml+1
Data/Yaml/Internal.hs+18−6
@@ -133,12 +133,24 @@ requireEvent e = do
133133
134134parse :: C.Sink Event Parse Value
135135parse = do
136− requireEvent EventStreamStart
137− requireEvent EventDocumentStart
138− res <- parseO
139− requireEvent EventDocumentEnd
140− requireEvent EventStreamEnd
141− return res
136+ streamStart <- CL.head
137+ case streamStart of
138+ Nothing ->
139+ -- empty string input
140+ return Null
141+ Just EventStreamStart -> do
142+ documentStart <- CL.head
143+ case documentStart of
144+ Just EventStreamEnd ->
145+ -- empty file input, comment only string/file input
146+ return Null
147+ Just EventDocumentStart -> do
148+ res <- parseO
149+ requireEvent EventDocumentEnd
150+ requireEvent EventStreamEnd
151+ return res
152+ _ -> liftIO $ throwIO $ UnexpectedEvent documentStart Nothing
153+ _ -> liftIO $ throwIO $ UnexpectedEvent streamStart Nothing
142154
143155parseScalar :: ByteString -> Anchor -> Style -> Tag
144156 -> C.Sink Event Parse Text
test/Data/YamlSpec.hs+19−2
@@ -95,7 +95,11 @@ spec = do
9595 it "parses when all uppercase" caseUppercaseBool
9696 it "parses when titlecase" caseTitlecaseBool
9797 describe "empty input" $ do
98− it "doesn't crash" caseEmptyInput
98+ it "parses as Null" caseEmptyInput
99+ it "parses as Null from file" caseEmptyInputFile
100+ describe "comment only input" $ do
101+ it "parses as Null" caseCommentOnlyInput
102+ it "parses as Null from file" caseCommentOnlyInputFile
99103 describe "alias stripping" $ do
100104 it "works" caseStripAlias
101105 describe "nulls" $ do
@@ -457,7 +461,20 @@ caseUppercaseBool = D.decode "foo: FALSE\nbar: Y\nbaz: ON" @?= obj
457461caseTitlecaseBool = D.decode "foo: No\nbar: Yes\nbaz: True" @?= obj
458462
459463caseEmptyInput :: Assertion
460−caseEmptyInput = D.decode B8.empty @?= (Nothing :: Maybe D.Value)
464+caseEmptyInput = D.decodeEither B8.empty @?= Right D.Null
465+
466+caseEmptyInputFile :: Assertion
467+caseEmptyInputFile = do
468+ out <- D.decodeFileEither "test/resources/empty.yaml"
469+ either (Left . D.prettyPrintParseException) Right out @?= Right D.Null
470+
471+caseCommentOnlyInput :: Assertion
472+caseCommentOnlyInput = D.decodeEither "# comment\n" @?= Right D.Null
473+
474+caseCommentOnlyInputFile :: Assertion
475+caseCommentOnlyInputFile = do
476+ out <- D.decodeFileEither "test/resources/empty2.yaml"
477+ either (Left . D.prettyPrintParseException) Right out @?= Right D.Null
461478
462479checkNull :: T.Text -> Spec
463480checkNull x =
test/resources/empty.yamlnew file

No changes to the content.

test/resources/empty2.yamlnew file+1
@@ -0,0 +1 @@
1+# comment