on master help sign in
Decode empty inputs as Null
d52b641Tomas Janousek
Data/Yaml/Internal.hs+18−6
@@ -133,12 +133,24 @@ requireEvent e = do
133
133
134parse :: C.Sink Event Parse Value
134parse :: C.Sink Event Parse Value
135parse = do
135parse = do
136+ streamStart <- CL.head
137+ case streamStart of
138+ Nothing ->
139+ -- empty string input
140+ return Null
136− requireEvent EventStreamStart
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
137− requireEvent EventDocumentStart
147+ Just EventDocumentStart -> do
138− res <- parseO
148+ res <- parseO
139− requireEvent EventDocumentEnd
149+ requireEvent EventDocumentEnd
140− requireEvent EventStreamEnd
150+ requireEvent EventStreamEnd
141− return res
151+ return res
152+ _ -> liftIO $ throwIO $ UnexpectedEvent documentStart Nothing
153+ _ -> liftIO $ throwIO $ UnexpectedEvent streamStart Nothing
142
154
143parseScalar :: ByteString -> Anchor -> Style -> Tag
155parseScalar :: ByteString -> Anchor -> Style -> Tag
144 -> C.Sink Event Parse Text
156 -> C.Sink Event Parse Text
test/Data/YamlSpec.hs+19−2
@@ -95,7 +95,11 @@ spec = do
95 it "parses when all uppercase" caseUppercaseBool
95 it "parses when all uppercase" caseUppercaseBool
96 it "parses when titlecase" caseTitlecaseBool
96 it "parses when titlecase" caseTitlecaseBool
97 describe "empty input" $ do
97 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
99 describe "alias stripping" $ do
103 describe "alias stripping" $ do
100 it "works" caseStripAlias
104 it "works" caseStripAlias
101 describe "nulls" $ do
105 describe "nulls" $ do
@@ -457,7 +461,20 @@ caseUppercaseBool = D.decode "foo: FALSE\nbar: Y\nbaz: ON" @?= obj
457caseTitlecaseBool = D.decode "foo: No\nbar: Yes\nbaz: True" @?= obj
461caseTitlecaseBool = D.decode "foo: No\nbar: Yes\nbaz: True" @?= obj
458
462
459caseEmptyInput :: Assertion
463caseEmptyInput :: 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
461
478
462checkNull :: T.Text -> Spec
479checkNull :: T.Text -> Spec
463checkNull x =
480checkNull x =
test/resources/empty.yamlnew file

No changes to the content.

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