packages feed

sheets (empty) → 0.1.0.0

raw patch · 5 files changed

+143/−0 lines, 5 filesdep +aesondep +basedep +base64-bytestring

Dependencies added: aeson, base, base64-bytestring, bytestring, cassava, composite-base, composite-cassava, text, vector

Files

+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Changelog for sheets++## 0.0.1.0++* Added `SheetT` transformer, and `FromJSON`, `ToJSON` instances.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2022++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 Author name here 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,11 @@+# sheets++Sheets is a spreadsheet type using+[composite-base](https://hackage.haskell.org/package/composite-base) and+[composite-cassava](https://hackage.haskell.org/package/composite-cassava).+Combined with [composite-ix](https://hackage.haskell.org/package/composite-ix),+you can do indexing queries on composite records to group and aggregate data+over multiple fields.++Sheets also provides `FromJSON` and `ToJSON` instances using a base64 encoding+to Text, so the entire CSV will fit in a single JSON text field.
+ sheets.cabal view
@@ -0,0 +1,55 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.6.+--+-- see: https://github.com/sol/hpack++name:           sheets+version:        0.1.0.0+synopsis:       Spreadsheet type for composite.+description:    Spreadsheet type for composite.+category:       Web+author:         Daniel Firth+maintainer:     dan.firth@homotopic.tech+copyright:      2022 Daniel Firth+license:        BSD3+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    README.md+    ChangeLog.md++source-repository head+  type: git+  location: https://gitlab.homotopic.tech/haskell/sheets++library+  exposed-modules:+      Composite.Sheet+  other-modules:+      Paths_sheets+  hs-source-dirs:+      src+  default-extensions:+      AllowAmbiguousTypes+      DeriveGeneric+      DerivingStrategies+      FlexibleContexts+      FlexibleInstances+      GADTs+      RankNTypes+      ScopedTypeVariables+      StandaloneDeriving+      TypeApplications+      TypeOperators+  build-depends:+      aeson+    , base >=4.7 && <5+    , base64-bytestring+    , bytestring+    , cassava+    , composite-base+    , composite-cassava+    , text+    , vector+  default-language: Haskell2010
+ src/Composite/Sheet.hs view
@@ -0,0 +1,42 @@+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE UndecidableInstances #-}+module Composite.Sheet where++import qualified Composite.Csv          as Csv+import           Composite.Record+import           Control.Applicative+import           Data.Aeson             as A+import           Data.ByteString.Base64 as B64+import           Data.ByteString.Lazy   (fromStrict, toStrict)+import           Data.Csv               hiding (Record)+import           Data.Functor.Identity+import           Data.Proxy+import           Data.Text.Encoding+import qualified Data.Vector            as V+import           GHC.Generics++-- | The SheetT type. This is a functor of hetrogenous records.+-- A typical "SpreadSheet" might be something like `SheetT [] Identity`+-- This provides a convenient newtype for deriving instances.+newtype SheetT w f xs = SheetT { runSheetT :: w (Rec f xs) }++type Sheet f xs = SheetT f Identity xs++deriving stock instance Eq (w (Rec f xs)) => Eq (SheetT w f xs)+deriving stock instance Show (w (Rec f xs)) => Show (SheetT w f xs)+deriving stock instance Generic (SheetT w f xs)++instance (ToNamedRecord (Record ixs), Csv.ToHeader (Record ixs)) => ToJSON (Sheet [] ixs) where+  toJSON (SheetT xs) =+    let z = encodeByName (Csv.extractRecHeader (Proxy @(Record ixs))) xs+     in A.String $ decodeUtf8 $ B64.encode $ toStrict $ z++instance FromNamedRecord (Record ixs) => FromJSON (Sheet [] ixs) where+  parseJSON (String x) = do+   let k = case B64.decode $ encodeUtf8 x of+             Left e  -> Left e+             Right a -> decodeByName . fromStrict $ a+   case k of+     Left e  -> fail $ show e+     Right a -> pure $ SheetT $ V.toList $ snd a+  parseJSON _ = empty