on master help sign in
Proper types for values
commit72649df0b8493c730b7e9e0f2a0b8411a77c232a authorMichael Snoyman <michael@snoyman.com> dateFri 30 Dec 2011, 11:49 (15 years ago) parenta15fa04
3 files changed+32−6
Data/Yaml.hs+13−2 test/main.hs+17−3 yaml.cabal+2−1
Data/Yaml.hs+13−2
@@ -54,10 +54,12 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
5454import Control.Monad (liftM)
5555import qualified Data.Vector as V
5656import Data.Text (Text, pack)
57+import Data.Text.Read (signed, decimal, double)
5758import Data.Text.Encoding (encodeUtf8, decodeUtf8With)
5859import Data.Text.Encoding.Error (lenientDecode)
5960import qualified Data.HashMap.Strict as M
6061import Data.Typeable
62+import Data.Attoparsec.Number
6163
6264encode :: ToJSON a => a -> ByteString
6365encode obj = unsafePerformIO $
@@ -150,14 +152,23 @@ parseScalar v a = do
150152 case a of
151153 Nothing -> return res
152154 Just an -> do
153− lift $ modify (Map.insert an $ String res)
155+ lift $ modify (Map.insert an $ textToValue res)
154156 return res
155157
158+textToValue :: Text -> Value -- FIXME check for quoting style?
159+textToValue "true" = Bool True
160+textToValue "false" = Bool False
161+textToValue "null" = Null
162+textToValue t
163+ | Right (x, "") <- signed decimal t = Number $ I x
164+ | Right (x, "") <- double t = Number $ D x
165+ | otherwise = String t
166+
156167parseO :: C.Sink Event Parse Value
157168parseO = do
158169 me <- CL.head
159170 case me of
160− Just (EventScalar v _t _s a) -> fmap String $ parseScalar v a
171+ Just (EventScalar v _t _s a) -> fmap textToValue $ parseScalar v a
161172 Just (EventSequenceStart a) -> parseS a id
162173 Just (EventMappingStart a) -> parseM a M.empty
163174 Just (EventAlias an) -> do
test/main.hs+17−3
@@ -41,6 +41,7 @@ main = hspecX $ do
4141 it "encode/decode file" caseEncodeDecodeFileData
4242 it "encode/decode strings" caseEncodeDecodeStrings
4343 it "decode invalid file" caseDecodeInvalid
44+ it "processes datatypes" caseDataTypes
4445 describe "Data.Yaml aliases" $ do
4546 it "simple scalar alias" caseSimpleScalarAlias
4647 it "simple sequence alias" caseSimpleSequenceAlias
@@ -284,6 +285,19 @@ caseMergeSequence = do
284285 isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
285286 let res = fromJust maybeRes
286287 mappingKey res "foo1" @?= (mkScalar "foo")
287− mappingKey res "k1" @?= (mkStrScalar "1")
288− mappingKey res "k2" @?= (mkStrScalar "2")
289− mappingKey res "k3" @?= (mkStrScalar "4")
288+ mappingKey res "k1" @?= (D.Number 1)
289+ mappingKey res "k2" @?= (D.Number 2)
290+ mappingKey res "k3" @?= (D.Number 4)
291+
292+caseDataTypes :: Assertion
293+caseDataTypes =
294+ D.decode (D.encode val) @?= Just val
295+ where
296+ val = object
297+ [ ("string", D.String "foo")
298+ , ("int", D.Number 5)
299+ , ("float", D.Number 4.3)
300+ , ("true", D.Bool True)
301+ , ("false", D.Bool False)
302+ , ("null", D.Null)
303+ ]
yaml.cabal+2−1
@@ -1,5 +1,5 @@
11name: yaml
2−version: 0.5.1
2+version: 0.5.1.1
33license: BSD3
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
@@ -36,6 +36,7 @@ library
3636 , unordered-containers
3737 , vector
3838 , text
39+ , attoparsec
3940 exposed-modules: Text.Libyaml
4041 Data.Yaml
4142 ghc-options: -Wall