yaml 0.5.1 → 0.5.1.1
raw patch · 3 files changed
+32/−6 lines, 3 filesdep +attoparsec
Dependencies added: attoparsec
Files
- Data/Yaml.hs +13/−2
- test/main.hs +17/−3
- yaml.cabal +2/−1
Data/Yaml.hs view
@@ -54,10 +54,12 @@ import Control.Monad (liftM) import qualified Data.Vector as V import Data.Text (Text, pack)+import Data.Text.Read (signed, decimal, double) import Data.Text.Encoding (encodeUtf8, decodeUtf8With) import Data.Text.Encoding.Error (lenientDecode) import qualified Data.HashMap.Strict as M import Data.Typeable+import Data.Attoparsec.Number encode :: ToJSON a => a -> ByteString encode obj = unsafePerformIO $@@ -150,14 +152,23 @@ case a of Nothing -> return res Just an -> do- lift $ modify (Map.insert an $ String res)+ lift $ modify (Map.insert an $ textToValue res) return res +textToValue :: Text -> Value -- FIXME check for quoting style?+textToValue "true" = Bool True+textToValue "false" = Bool False+textToValue "null" = Null+textToValue t+ | Right (x, "") <- signed decimal t = Number $ I x+ | Right (x, "") <- double t = Number $ D x+ | otherwise = String t+ parseO :: C.Sink Event Parse Value parseO = do me <- CL.head case me of- Just (EventScalar v _t _s a) -> fmap String $ parseScalar v a+ Just (EventScalar v _t _s a) -> fmap textToValue $ parseScalar v a Just (EventSequenceStart a) -> parseS a id Just (EventMappingStart a) -> parseM a M.empty Just (EventAlias an) -> do
test/main.hs view
@@ -41,6 +41,7 @@ it "encode/decode file" caseEncodeDecodeFileData it "encode/decode strings" caseEncodeDecodeStrings it "decode invalid file" caseDecodeInvalid+ it "processes datatypes" caseDataTypes describe "Data.Yaml aliases" $ do it "simple scalar alias" caseSimpleScalarAlias it "simple sequence alias" caseSimpleSequenceAlias@@ -284,6 +285,19 @@ isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing" let res = fromJust maybeRes mappingKey res "foo1" @?= (mkScalar "foo")- mappingKey res "k1" @?= (mkStrScalar "1")- mappingKey res "k2" @?= (mkStrScalar "2")- mappingKey res "k3" @?= (mkStrScalar "4")+ mappingKey res "k1" @?= (D.Number 1)+ mappingKey res "k2" @?= (D.Number 2)+ mappingKey res "k3" @?= (D.Number 4)++caseDataTypes :: Assertion+caseDataTypes =+ D.decode (D.encode val) @?= Just val+ where+ val = object+ [ ("string", D.String "foo")+ , ("int", D.Number 5)+ , ("float", D.Number 4.3)+ , ("true", D.Bool True)+ , ("false", D.Bool False)+ , ("null", D.Null)+ ]
yaml.cabal view
@@ -1,5 +1,5 @@ name: yaml-version: 0.5.1+version: 0.5.1.1 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov @@ -36,6 +36,7 @@ , unordered-containers , vector , text+ , attoparsec exposed-modules: Text.Libyaml Data.Yaml ghc-options: -Wall