packages feed

cassava-embed (empty) → 0.1.0.0

raw patch · 9 files changed

+210/−0 lines, 9 filesdep +basedep +bytestringdep +cassavasetup-changed

Dependencies added: base, bytestring, cassava, cassava-embed, template-haskell, th-lift, vector

Files

+ CHANGES.md view
@@ -0,0 +1,3 @@+## Version 0.1.0.0++* First package release
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Aleksey Pirogov (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Aleksey Pirogov nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,6 @@+# cassava-embed++This library makes possible to embed any CSV-file as a list of `cassava`-parseable records.++Take a look at `example/` folder for more info.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cassava-embed.cabal view
@@ -0,0 +1,65 @@+-- This file has been generated from package.yaml by hpack version 0.17.1.+--+-- see: https://github.com/sol/hpack++name:           cassava-embed+version:        0.1.0.0+synopsis:       CSV-file embedding library+description:    @cassava-embed@ helps to embed CSV-file using TemplateHaskell.+category:       Text,CSV+homepage:       https://github.com/typeable/cassava-embed#readme+author:         Typeable.io contributors+maintainer:     makeit@typeable.io+license:        BSD3+license-file:   LICENSE+tested-with:    GHC ==8.0.2, GHC ==7.10.3+build-type:     Simple+cabal-version:  >= 1.10++extra-source-files:+    CHANGES.md+    README.md++data-files:+    example/data.csv++flag build-examples+  description: Build example(s)+  manual: True+  default: False++library+  hs-source-dirs:+      src+  build-depends:+      base >= 4.7 && < 5+    , cassava+    , template-haskell+    , bytestring+    , vector+  exposed-modules:+      Data.Csv.Embed+  default-language: Haskell2010++executable example+  main-is: Main.hs+  hs-source-dirs:+      example+  build-depends:+      base >= 4.7 && < 5+    , cassava+    , template-haskell+  if flag(build-examples)+    buildable: True+    if impl(ghc < 8.0.1)+      build-depends:+          cassava-embed+        , th-lift+    else+      build-depends:+          cassava-embed+  else+    buildable: False+  other-modules:+      Types+  default-language: Haskell2010
+ example/Main.hs view
@@ -0,0 +1,13 @@+{-# LANGUAGE TemplateHaskell #-}++module Main where++import Data.Csv.Embed+import Data.Proxy++import Types++main :: IO ()+main = do+  print $(embedNamedRecords (Proxy :: Proxy Rec) "example/data.csv")+  print $(embedRecords (Proxy :: Proxy (String, Int)) "example/data.csv")
+ example/Types.hs view
@@ -0,0 +1,30 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE OverloadedStrings #-}+#if __GLASGOW_HASKELL__ >= 800+{-# LANGUAGE DeriveLift #-}+{-# LANGUAGE StandaloneDeriving #-}+#endif+{-# LANGUAGE TemplateHaskell #-}++module Types where++import Data.Csv+#if __GLASGOW_HASKELL__ < 800+import Language.Haskell.TH.Lift+#else+import Language.Haskell.TH.Syntax+#endif++data Rec = Rec+  { _rKey :: !String+  , _rVal :: !Int+  } deriving (Show)++#if __GLASGOW_HASKELL__ < 800+$(deriveLift ''Rec)+#else+deriving instance Lift Rec+#endif++instance FromNamedRecord Rec where+  parseNamedRecord r = Rec <$> r .: "key" <*> r .: "val"
+ example/data.csv view
@@ -0,0 +1,3 @@+key,val+foo,100+bar,42
+ src/Data/Csv/Embed.hs view
@@ -0,0 +1,58 @@+{-|+Module      : Data.Csv.Embed+Description : CSV-file embedding library+License     : BSD-3+Stability   : experimental+Portability : POSIX++@cassava-embed@ helps to embed CSV-file using TemplateHaskell.++Typical usage:++> print $(embedRecord (Proxy :: Proxy (String, Int)) "data.csv")++-}++module Data.Csv.Embed+  ( embedRecords+  , embedNamedRecords+  ) where++import Data.ByteString.Lazy as BSL+import Data.Csv+import Data.Proxy+import Data.Vector+import Language.Haskell.TH+import Language.Haskell.TH.Syntax++-- | Embeds CSV-file as list of values with type+-- that implements 'FromRecord'.+embedRecords+  :: (Lift a, FromRecord a)+  => Proxy a   -- ^ type of row (proxied)+  -> FilePath  -- ^ path to CSV-file+  -> ExpQ+embedRecords proxy path = do+  content <- runIO $ BSL.readFile path+  let+    Right rows = decode HasHeader content+    Right xs = traverse (fmap cast . runParser . parseRecord) $ toList rows+  ListE <$> Prelude.mapM lift xs+  where+    cast x = x `asProxyTypeOf` proxy++-- | Embeds CSV-file as list of values with type+-- that implements 'FromNamedRecord'.+embedNamedRecords+  :: (Lift a, FromNamedRecord a)+  => Proxy a   -- ^ type of row (proxied)+  -> FilePath  -- ^ path to CSV-file+  -> ExpQ+embedNamedRecords proxy path = do+  content <- runIO $ BSL.readFile path+  let+    Right (_, rows) = decodeByName content+    Right xs = traverse (fmap cast . runParser . parseNamedRecord) $ toList rows+  ListE <$> Prelude.mapM lift xs+  where+    cast x = x `asProxyTypeOf` proxy