yaml-light 0.1.1 → 0.1.3
raw patch · 2 files changed
+64/−12 lines, 2 files
Files
- Data/Yaml/YamlLight.hs +63/−11
- yaml-light.cabal +1/−1
Data/Yaml/YamlLight.hs view
@@ -18,24 +18,27 @@ -- * YamlLight versions of Syck functions , parseYaml, parseYamlFile, parseYamlBytes -- * YamlLight utility functions- , fromYamlNode, lookupYL, lookupYLWith, combineSequencedMaps+ , fromYamlNode, lookupYL, lookupYLWith, combineSequencedMaps, combineMappedSequences -- ** Extractors , unSeq, unMap, unStr ) where import Control.Applicative- import Data.Data+-- import Data.Data import Data.List import Data.Maybe+ import Control.Arrow import qualified Data.Yaml.Syck as Syck import qualified Data.Map as Map import qualified Data.ByteString as ByteString - -- | A Single, lighter-weight representation than that of HsSyck. Note that the YMap is an actual Map from- -- Data.Map, so behavior with respect to identical keys and ordering of entries will behave as Data.Map- -- dictates. This behavior is also in compliance with the Yaml spec. If you currently rely on HsSyck's- -- preservation of ordering, you can also consider representing- -- such maps as sequences of single entry maps. See the examples of \"Ordered Mappings\" in the Yaml spec: <http://www.yaml.org/spec/1.2/spec.html>.-+ {- | A light-weight, single ADT representation of a yaml document in contrast with what is provided by HsSyck.+ Note that the YMap is an actual Map from+ Data.Map, so behavior with respect to identical keys and ordering of entries will behave as Data.Map+ dictates. This behavior is also in compliance with the Yaml spec. If you currently rely on HsSyck's+ preservation of ordering, you can also consider representing+ such maps as sequences of single entry maps. See the examples of \"Ordered Mappings\" in the Yaml+ spec: <http://www.yaml.org/spec/1.2/spec.html>.+ -} data YamlLight = YMap (Map.Map YamlLight YamlLight) | YSeq [YamlLight] | YStr ByteString.ByteString@@ -81,7 +84,7 @@ lookupYLWith p (YMap m) = snd <$> (find (p . fst) $ Map.toList m) lookupYLWith _ _ = Nothing - {- | Combine a sequence of maps into a list of (key,value) pairs. The ordering of the result preserves the ordering+ {- | Combine a sequence of YMaps into a list of (key,value) pairs. The ordering of the result preserves the ordering of the sequence, but the ordering of the individual maps is as Data.Map handles it. Example:@@ -99,13 +102,49 @@ @ where key1 and key2 might be arranged differently as Data.Map would- arrange them. This does not preserve uniqueness of keys across maps.- Any items of the sequence that are not maps will be ignored. Returns Nothing if not called on a Sequence+ arrange them. This does not enforce uniqueness of keys across different maps.+ Any items of the sequence that are not maps will not be present in the output list.+ Returns Nothing if not called on a Sequence -} combineSequencedMaps :: YamlLight -> Maybe [(YamlLight, YamlLight)] combineSequencedMaps (YSeq ys) = Just . concatMap Map.assocs . catMaybes $ map unMap ys combineSequencedMaps _ = Nothing + {- | Take a YamlLight that is a YMap of keys to YSeqs, and return a list of (key,elem) pairs, where elem is an element+ of the YSeq under key.++ Example:++ @+ key1: [val1, val2, val3]+ key2: [val4, val5]+ @++ Would become:++ @+ [(key1,val1),(key1,val2),(key1,val3),(key2,val4),(key2,val5)]+ @++ where the precise ordering of the key1 and key2 pairs depends on the ordering of Data.Map.+ Any values of keys that are not sequences will not appear in the output list.+ Returns Nothing if not called on a YMap.+ -}++ combineMappedSequences :: YamlLight -> Maybe [(YamlLight, YamlLight)]+ combineMappedSequences (YMap m) = Just . concatMap flattenTags . removeSndMaybes $ mapThenList unSeq m+ combineMappedSequences _ = Nothing++ mapThenList :: (b -> Maybe [c]) -> Map.Map a b -> [(a, Maybe [c])]+ mapThenList f m = Map.toList $ Map.map f m++ removeSndMaybes :: [(a,Maybe [b])] -> [(a,[b])]+ removeSndMaybes = map (second fromJust) . filter (isJust . snd)++ flattenTags :: (a,[b]) -> [(a,b)]+ flattenTags (a,bs) = map ((,) a) bs++ -- | Get the contents of a sequence unSeq :: YamlLight -> Maybe [YamlLight] unSeq (YSeq s) = Just s@@ -120,5 +159,18 @@ unStr :: YamlLight -> Maybe ByteString.ByteString unStr (YStr s) = Just s unStr _ = Nothing+++ -- tests++ performTest :: Show a => (YamlLight -> a) -> String -> IO ()+ performTest f s = parseYaml s >>= print . f++ cSeqMap1 = "[{key1: val1, key2: val2}, {key3: val3}]"+ cMapSeq1 = "{key1: [val1, val2, val3], key2: [val4, val5]}"++ testCombineSequencedMaps1 = performTest combineSequencedMaps cSeqMap1++ testCombineMappedSequences1 = performTest combineMappedSequences cMapSeq1
yaml-light.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version: 0.1.1+Version: 0.1.3 -- A short (one-line) description of the package. Synopsis: A light-weight wrapper with utility functions around HsSyck