Skip to content

Commit

Permalink
HsYAML 0.2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayphoenix committed Jul 17, 2019
1 parent 66671a4 commit 68d1841
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion HsYAML-aeson.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ source-repository head
library
exposed-modules: Data.YAML.Aeson
build-depends:
, HsYAML ^>= 0.1.1.0
, HsYAML ^>= 0.2
, aeson ^>= 1.4.0.0
, base >= 4.5 && < 4.13
, bytestring ^>= 0.9.2.1 || ^>= 0.10.0.2
Expand Down
29 changes: 15 additions & 14 deletions src/Data/YAML/Aeson.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
module Data.YAML.Aeson
( -- * Parsing YAML using JSON models
-- ** High-level parsing/decoding via 'FromJSON' instances
decode1
Data.YAML.Aeson.decode1
, decode1'
-- ** Parsing into JSON AST ('J.Value')
, decodeValue
Expand All @@ -35,6 +35,7 @@ import qualified Data.ByteString.Lazy as BS.L
import Data.Text (Text)
import qualified Data.Vector as V
import Data.YAML as Y
import Data.YAML.Event (Pos)

-- | Parse a single YAML document using the 'coreSchemaResolver' and decode to Haskell types using 'FromJSON' instances.
--
Expand All @@ -46,9 +47,9 @@ import Data.YAML as Y
-- decoder configuration.
--
decode1 :: FromJSON v => BS.L.ByteString -> Either String v
decode1 bs = do
vs <- decodeValue bs
case vs of
decode1 bs = case decodeValue bs of
Left (_ ,err) -> Left err
Right vs -> case vs of
[] -> Left "No documents found in YAML stream"
(_:_:_) -> Left "Multiple documents encountered in YAML stream"
[v1] -> do
Expand All @@ -58,9 +59,9 @@ decode1 bs = do

-- | Variant of 'decode1' allowing for customization. See 'decodeValue'' for documentation of parameters.
decode1' :: FromJSON v => SchemaResolver -> (J.Value -> Either String Text) -> BS.L.ByteString -> Either String v
decode1' schema keyconv bs = do
vs <- decodeValue' schema keyconv bs
case vs of
decode1' schema keyconv bs = case decodeValue' schema keyconv bs of
Left (_ ,err) -> Left err
Right vs -> case vs of
[] -> Left "No documents found in YAML stream"
(_:_:_) -> Left "Multiple documents encountered in YAML stream"
[v1] -> do
Expand All @@ -83,7 +84,7 @@ decode1' schema keyconv bs = do
-- which performs no conversion and will fail when encountering YAML
-- Scalars that have not been resolved to a text Scalar (according to
-- the respective YAML schema resolver).
decodeValue :: BS.L.ByteString -> Either String [J.Value]
decodeValue :: BS.L.ByteString -> Either (Pos, String) [J.Value]
decodeValue = decodeValue' coreSchemaResolver identityKeyConv
where
identityKeyConv :: J.Value -> Either String Text
Expand All @@ -103,15 +104,15 @@ decodeValue' :: SchemaResolver -- ^ YAML Schema resolver to use
-- ^ JSON object key conversion function. This operates on the YAML node as resolved by the 'SchemaResolver' and subsequently converted into a JSON Value according to the 'scalarToValue' conversion. See 'decodeValue' documentation for an example.

-> BS.L.ByteString -- ^ YAML document to parse
-> Either String [J.Value]
-> Either (Pos, String) [J.Value]
decodeValue' SchemaResolver{..} keyconv bs0
= runIdentity (decodeLoader failsafeLoader bs0)
where
failsafeLoader = Loader { yScalar = \t s v -> pure $! schemaResolverScalar t s v >>= mkScl
, ySequence = \t vs -> pure $! schemaResolverSequence t >>= \_ -> mkArr vs
, yMapping = \t kvs -> pure $! schemaResolverMapping t >>= \_ -> mkObj kvs
, yAlias = \_ c n -> pure $! if c then Left "cycle detected" else Right n
, yAnchor = \_ n -> Ap.pure $! Right $! n
failsafeLoader = Loader { yScalar = \t s v _ -> pure $! schemaResolverScalar t s v >>= mkScl
, ySequence = \t vs _ -> pure $! schemaResolverSequence t >>= \_ -> mkArr vs
, yMapping = \t kvs _ -> pure $! schemaResolverMapping t >>= \_ -> mkObj kvs
, yAlias = \_ c n _ -> pure $! if c then Left "cycle detected" else Right n
, yAnchor = \_ n _ -> Ap.pure $! Right $! n
}

mkObj :: [(J.Value, J.Value)] -> Either String J.Value
Expand Down

0 comments on commit 68d1841

Please sign in to comment.