on master help sign in
Support for !!str tag
commit94b101f38f57ee9eaab5d9664982ca1d1cf38395 authorMichael Snoyman <michael@snoyman.com> dateTue 5 Jun 2012, 07:06 (14 years ago) parentd9505fe
2 files changed+19−16
Data/Yaml.hs+12−11 test/main.hs+7−5
Data/Yaml.hs+12−11
@@ -149,22 +149,23 @@ parse = do
149149 requireEvent EventStreamEnd
150150 return res
151151
152−parseScalar :: ByteString -> Anchor -> Style
152+parseScalar :: ByteString -> Anchor -> Style -> Tag
153153 -> C.Sink Event Parse Text
154−parseScalar v a style = do
154+parseScalar v a style tag = do
155155 let res = decodeUtf8With lenientDecode v
156156 case a of
157157 Nothing -> return res
158158 Just an -> do
159− lift $ modify (Map.insert an $ textToValue style res)
159+ lift $ modify (Map.insert an $ textToValue style tag res)
160160 return res
161161
162−textToValue :: Style -> Text -> Value
163−textToValue SingleQuoted t = String t
164−textToValue DoubleQuoted t = String t
165−textToValue Folded t = String t
166−textToValue _ "null" = Null
167−textToValue _ t
162+textToValue :: Style -> Tag -> Text -> Value
163+textToValue SingleQuoted _ t = String t
164+textToValue DoubleQuoted _ t = String t
165+textToValue _ StrTag t = String t
166+textToValue Folded _ t = String t
167+textToValue _ _ "null" = Null
168+textToValue _ _ t
168169 | any (t `isLike`) ["y", "yes", "on", "true"] = Bool True
169170 | any (t `isLike`) ["n", "no", "off", "false"] = Bool False
170171 | Right (x, "") <- signed decimal t = Number $ I x
@@ -178,7 +179,7 @@ parseO :: C.Sink Event Parse Value
178179parseO = do
179180 me <- CL.head
180181 case me of
181− Just (EventScalar v _t style a) -> fmap (textToValue style) $ parseScalar v a style
182+ Just (EventScalar v tag style a) -> fmap (textToValue style tag) $ parseScalar v a style tag
182183 Just (EventSequenceStart a) -> parseS a id
183184 Just (EventMappingStart a) -> parseM a M.empty
184185 Just (EventAlias an) -> do
@@ -223,7 +224,7 @@ parseM a front = do
223224 _ -> do
224225 CL.drop 1
225226 s <- case me of
226− Just (EventScalar v _ style a') -> parseScalar v a' style
227+ Just (EventScalar v tag style a') -> parseScalar v a' style tag
227228 _ -> liftIO $ throwIO $ UnexpectedEvent me Nothing
228229 o <- parseO
229230
test/main.hs+7−5
@@ -56,6 +56,7 @@ main = hspecX $ do
5656 describe "numbers" $ do
5757 it "parses as string when quoted" caseQuotedNumber
5858 it "parses as number when unquoted" caseUnquotedNumber
59+ it "parses as number !!str is present" caseAttribNumber
5960 describe "booleans" $ do
6061 it "parses when all lowercase" caseLowercaseBool
6162 it "parses when all uppercase" caseUppercaseBool
@@ -292,9 +293,9 @@ caseMergeSequence = do
292293 isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
293294 let res = fromJust maybeRes
294295 mappingKey res "foo1" @?= (mkScalar "foo")
295− mappingKey res "k1" @?= (D.Number 1)
296− mappingKey res "k2" @?= (D.Number 2)
297− mappingKey res "k3" @?= (D.Number 4)
296+ mappingKey res "k1" @?= (D.String "1")
297+ mappingKey res "k2" @?= (D.String "2")
298+ mappingKey res "k3" @?= (D.String "4")
298299
299300caseDataTypes :: Assertion
300301caseDataTypes =
@@ -309,9 +310,10 @@ caseDataTypes =
309310 , ("null", D.Null)
310311 ]
311312
312−caseQuotedNumber, caseUnquotedNumber :: Assertion
313+caseQuotedNumber, caseUnquotedNumber, caseAttribNumber :: Assertion
313314caseQuotedNumber = D.decode "foo: \"1234\"" @?= Just (object [("foo", D.String "1234")])
314315caseUnquotedNumber = D.decode "foo: 1234" @?= Just (object [("foo", D.Number 1234)])
316+caseAttribNumber = D.decode "foo: !!str 1234" @?= Just (object [("foo", D.String "1234")])
315317
316318obj :: Maybe D.Value
317319obj = Just (object [("foo", D.Bool False), ("bar", D.Bool True), ("baz", D.Bool True)])
@@ -319,4 +321,4 @@ obj = Just (object [("foo", D.Bool False), ("bar", D.Bool True), ("baz", D.Bool
319321caseLowercaseBool, caseUppercaseBool, caseTitlecaseBool :: Assertion
320322caseLowercaseBool = D.decode "foo: off\nbar: y\nbaz: true" @?= obj
321323caseUppercaseBool = D.decode "foo: FALSE\nbar: Y\nbaz: ON" @?= obj
322−caseTitlecaseBool = D.decode "foo: No\nbar: Yes\nbaz: True" @?= obj
324+caseTitlecaseBool = D.decode "foo: No\nbar: Yes\nbaz: True" @?= obj