on master help sign in
Check quoting style for scalars (#8)
commit35823b4497ddd00755cc15f7c8e0ed2ba0c14162 authorMichael Snoyman <michael@snoyman.com> dateThu 24 May 2012, 17:35 (14 years ago) parent2e735ab
2 files changed+20−10
Data/Yaml.hs+13−10 test/main.hs+7
Data/Yaml.hs+13−10
@@ -147,21 +147,24 @@ parse = do
147147 requireEvent EventStreamEnd
148148 return res
149149
150−parseScalar :: ByteString -> Anchor
150+parseScalar :: ByteString -> Anchor -> Style
151151 -> C.Sink Event Parse Text
152−parseScalar v a = do
152+parseScalar v a style = do
153153 let res = decodeUtf8With lenientDecode v
154154 case a of
155155 Nothing -> return res
156156 Just an -> do
157− lift $ modify (Map.insert an $ textToValue res)
157+ lift $ modify (Map.insert an $ textToValue style res)
158158 return res
159159
160−textToValue :: Text -> Value -- FIXME check for quoting style?
161−textToValue "true" = Bool True
162−textToValue "false" = Bool False
163−textToValue "null" = Null
164−textToValue t
160+textToValue :: Style -> Text -> Value
161+textToValue SingleQuoted t = String t
162+textToValue DoubleQuoted t = String t
163+textToValue Folded t = String t
164+textToValue _ "true" = Bool True
165+textToValue _ "false" = Bool False
166+textToValue _ "null" = Null
167+textToValue _ t
165168 | Right (x, "") <- signed decimal t = Number $ I x
166169 | Right (x, "") <- double t = Number $ D x
167170 | otherwise = String t
@@ -170,7 +173,7 @@ parseO :: C.Sink Event Parse Value
170173parseO = do
171174 me <- CL.head
172175 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
174177 Just (EventSequenceStart a) -> parseS a id
175178 Just (EventMappingStart a) -> parseM a M.empty
176179 Just (EventAlias an) -> do
@@ -215,7 +218,7 @@ parseM a front = do
215218 _ -> do
216219 CL.drop 1
217220 s <- case me of
218− Just (EventScalar v _ _ a') -> parseScalar v a'
221+ Just (EventScalar v _ style a') -> parseScalar v a' style
219222 _ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
220223 o <- parseO
221224
test/main.hs+7
@@ -53,6 +53,9 @@ main = hspecX $ do
5353 it "test uniqueness of keys" caseAllKeysShouldBeUnique
5454 it "test mapping merge" caseSimpleMappingMerge
5555 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
5659
5760counter :: Monad m => (Y.Event -> Bool) -> C.Sink Y.Event m Int
5861counter pred' =
@@ -301,3 +304,7 @@ caseDataTypes =
301304 , ("false", D.Bool False)
302305 , ("null", D.Null)
303306 ]
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)])