on master help sign in
Check quoting style for scalars (#8)
35823b4Michael Snoyman
Data/Yaml.hs+13−10
@@ -147,21 +147,24 @@ parse = do
147 requireEvent EventStreamEnd
147 requireEvent EventStreamEnd
148 return res
148 return res
149
149
150−parseScalar :: ByteString -> Anchor
150+parseScalar :: ByteString -> Anchor -> Style
151 -> C.Sink Event Parse Text
151 -> C.Sink Event Parse Text
152−parseScalar v a = do
152+parseScalar v a style = do
153 let res = decodeUtf8With lenientDecode v
153 let res = decodeUtf8With lenientDecode v
154 case a of
154 case a of
155 Nothing -> return res
155 Nothing -> return res
156 Just an -> do
156 Just an -> do
157− lift $ modify (Map.insert an $ textToValue res)
157+ lift $ modify (Map.insert an $ textToValue style res)
158 return res
158 return res
159
159
160−textToValue :: Text -> Value -- FIXME check for quoting style?
160+textToValue :: Style -> Text -> Value
161+textToValue SingleQuoted t = String t
162+textToValue DoubleQuoted t = String t
163+textToValue Folded t = String t
161−textToValue "true" = Bool True
164+textToValue _ "true" = Bool True
162−textToValue "false" = Bool False
165+textToValue _ "false" = Bool False
163−textToValue "null" = Null
166+textToValue _ "null" = Null
164−textToValue t
167+textToValue _ t
165 | Right (x, "") <- signed decimal t = Number $ I x
168 | Right (x, "") <- signed decimal t = Number $ I x
166 | Right (x, "") <- double t = Number $ D x
169 | Right (x, "") <- double t = Number $ D x
167 | otherwise = String t
170 | otherwise = String t
@@ -170,7 +173,7 @@ parseO :: C.Sink Event Parse Value
170parseO = do
173parseO = do
171 me <- CL.head
174 me <- CL.head
172 case me of
175 case me of
173− Just (EventScalar v _t _s a) -> fmap textToValue $ parseScalar v a
176+ Just (EventScalar v _t style a) -> fmap (textToValue style) $ parseScalar v a style
174 Just (EventSequenceStart a) -> parseS a id
177 Just (EventSequenceStart a) -> parseS a id
175 Just (EventMappingStart a) -> parseM a M.empty
178 Just (EventMappingStart a) -> parseM a M.empty
176 Just (EventAlias an) -> do
179 Just (EventAlias an) -> do
@@ -215,7 +218,7 @@ parseM a front = do
215 _ -> do
218 _ -> do
216 CL.drop 1
219 CL.drop 1
217 s <- case me of
220 s <- case me of
218− Just (EventScalar v _ _ a') -> parseScalar v a'
221+ Just (EventScalar v _ style a') -> parseScalar v a' style
219 _ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
222 _ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
220 o <- parseO
223 o <- parseO
221
224
test/main.hs+7
@@ -53,6 +53,9 @@ main = hspecX $ do
53 it "test uniqueness of keys" caseAllKeysShouldBeUnique
53 it "test uniqueness of keys" caseAllKeysShouldBeUnique
54 it "test mapping merge" caseSimpleMappingMerge
54 it "test mapping merge" caseSimpleMappingMerge
55 it "test sequence of mappings merging" caseMergeSequence
55 it "test sequence of mappings merging" caseMergeSequence
56+ describe "numbers" $ do
57+ it "parses as string when quoted" caseQuotedNumber
58+ it "parses as number when unquoted" caseUnquotedNumber
56
59
57counter :: Monad m => (Y.Event -> Bool) -> C.Sink Y.Event m Int
60counter :: Monad m => (Y.Event -> Bool) -> C.Sink Y.Event m Int
58counter pred' =
61counter pred' =
@@ -301,3 +304,7 @@ caseDataTypes =
301 , ("false", D.Bool False)
304 , ("false", D.Bool False)
302 , ("null", D.Null)
305 , ("null", D.Null)
303 ]
306 ]
307+
308+caseQuotedNumber, caseUnquotedNumber :: Assertion
309+caseQuotedNumber = D.decode "foo: \"1234\"" @?= Just (object [("foo", D.String "1234")])
310+caseUnquotedNumber = D.decode "foo: 1234" @?= Just (object [("foo", D.Number 1234)])