Skip to content

Commit 8e13335

Browse files
committed
xlsx th elimination wip
1 parent 6e8d225 commit 8e13335

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

pub/xlsx/src/Codec/Xlsx/Writer/Stream.hs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
{-# LANGUAGE RankNTypes #-}
88
{-# LANGUAGE ScopedTypeVariables #-}
99
{-# LANGUAGE StrictData #-}
10-
{-# LANGUAGE TemplateHaskell #-}
1110

1211
-- | Writes Excel files from a stream, which allows creation of
1312
-- large Excel files while remaining in constant memory.
@@ -16,11 +15,6 @@ module Codec.Xlsx.Writer.Stream
1615
writeXlsxWithSharedStrings,
1716
SheetWriteSettings (..),
1817
defaultSettings,
19-
wsSheetView,
20-
wsZip,
21-
wsColumnProperties,
22-
wsRowProperties,
23-
wsStyles,
2418

2519
-- *** Shared strings
2620
sharedStrings,
@@ -55,6 +49,8 @@ import Codec.Xlsx.Writer.Internal
5549
import Codec.Xlsx.Writer.Internal.Stream
5650
import Conduit (PrimMonad, yield, (.|))
5751
import qualified Conduit as C
52+
import Data.Generics.Labels
53+
import GHC.Generics (Generic)
5854
#ifdef USE_MICROLENS
5955
import Data.Traversable.WithIndex
6056
import Lens.Micro.Platform
@@ -86,7 +82,8 @@ import qualified Text.XML as TXML
8682
import Text.XML.Stream.Render
8783
import Text.XML.Unresolved (elementToEvents)
8884

89-
upsertSharedStrings :: (MonadState SharedStringState m) => Row -> m [(Text, Int)]
85+
upsertSharedStrings ::
86+
(MonadState SharedStringState m) => Row -> m [(Text, Int)]
9087
upsertSharedStrings row =
9188
traverse upsertSharedString items
9289
where
@@ -115,33 +112,32 @@ sharedStringsStream =
115112

116113
-- | Settings for writing a single sheet.
117114
data SheetWriteSettings = MkSheetWriteSettings
118-
{ _wsSheetView :: [SheetView],
115+
{ wsSheetView :: [SheetView],
119116
-- | Enable zipOpt64=True if you intend writing large xlsx files, zip needs 64bit for files over 4gb.
120-
_wsZip :: ZipOptions,
121-
_wsColumnProperties :: [ColumnsProperties],
122-
_wsRowProperties :: Map Int RowProperties,
123-
_wsStyles :: Styles
117+
wsZip :: ZipOptions,
118+
wsColumnProperties :: [ColumnsProperties],
119+
wsRowProperties :: Map Int RowProperties,
120+
wsStyles :: Styles
124121
}
122+
deriving stock (Generic)
125123

126124
instance Show SheetWriteSettings where
127125
-- ZipOptions lacks a show instance-}
128126
show (MkSheetWriteSettings s _ y r _) =
129127
printf
130-
"MkSheetWriteSettings{ _wsSheetView=%s, _wsColumnProperties=%s, _wsZip=defaultZipOptions, _wsRowProperties=%s }"
128+
"MkSheetWriteSettings{ wsSheetView=%s, wsColumnProperties=%s, wsZip=defaultZipOptions, wsRowProperties=%s }"
131129
(show s)
132130
(show y)
133131
(show r)
134132

135-
makeLenses ''SheetWriteSettings
136-
137133
defaultSettings :: SheetWriteSettings
138134
defaultSettings =
139135
MkSheetWriteSettings
140-
{ _wsSheetView = [],
141-
_wsColumnProperties = [],
142-
_wsRowProperties = mempty,
143-
_wsStyles = emptyStyles,
144-
_wsZip =
136+
{ wsSheetView = [],
137+
wsColumnProperties = [],
138+
wsRowProperties = mempty,
139+
wsStyles = emptyStyles,
140+
wsZip =
145141
defaultZipOptions
146142
{ zipOpt64 = False
147143
-- There is a magick number in the zip archive package,
@@ -196,7 +192,7 @@ writeXlsxWithSharedStrings ::
196192
ConduitT () Row m () ->
197193
ConduitT () ByteString m Word64
198194
writeXlsxWithSharedStrings settings sharedStrings' items =
199-
combinedFiles settings sharedStrings' items .| zipStream (settings ^. wsZip)
195+
combinedFiles settings sharedStrings' items .| zipStream (settings ^. #wsZip)
200196

201197
-- massive amount of boilerplate needed for excel to function
202198
boilerplate ::
@@ -213,7 +209,9 @@ boilerplate settings sharedStrings' =
213209
ZipDataSource $ writeContentTypes .| eventsToBS
214210
),
215211
(zipEntry "xl/workbook.xml", ZipDataSource $ writeWorkbook .| eventsToBS),
216-
(zipEntry "xl/styles.xml", ZipDataByteString $ coerce $ settings ^. wsStyles),
212+
( zipEntry "xl/styles.xml",
213+
ZipDataByteString $ coerce $ settings ^. #wsStyles
214+
),
217215
( zipEntry "xl/_rels/workbook.xml.rels",
218216
ZipDataSource $ writeWorkbookRels .| eventsToBS
219217
),
@@ -231,7 +229,9 @@ combinedFiles settings sharedStrings' items =
231229
boilerplate settings sharedStrings'
232230
<> [ ( zipEntry "xl/worksheets/sheet1.xml",
233231
ZipDataSource $
234-
items .| C.runReaderC settings (writeWorkSheet sharedStrings') .| eventsToBS
232+
items
233+
.| C.runReaderC settings (writeWorkSheet sharedStrings')
234+
.| eventsToBS
235235
)
236236
]
237237

@@ -359,12 +359,14 @@ writeEvents = renderBuilder (def {rsPretty = False})
359359
sheetViews ::
360360
forall m. (MonadReader SheetWriteSettings m) => forall i. ConduitT i Event m ()
361361
sheetViews = do
362-
sheetView <- view wsSheetView
362+
sheetView <- view #wsSheetView
363363

364364
unless (null sheetView) $ el (n_ "sheetViews") $ do
365365
let view' :: [Element]
366366
view' =
367-
setNameSpaceRec spreadSheetNS . toXMLElement . toElement (n_ "sheetView")
367+
setNameSpaceRec spreadSheetNS
368+
. toXMLElement
369+
. toElement (n_ "sheetView")
368370
<$> sheetView
369371

370372
C.yieldMany $ elementToEvents =<< view'
@@ -389,7 +391,7 @@ setNameSpaceRec space xelm =
389391

390392
columns :: (MonadReader SheetWriteSettings m) => ConduitT Row Event m ()
391393
columns = do
392-
colProps <- view wsColumnProperties
394+
colProps <- view #wsColumnProperties
393395
let cols :: Maybe TXML.Element
394396
cols = nonEmptyElListSimple (n_ "cols") $ map (toElement (n_ "col")) colProps
395397
traverse_ (C.yieldMany . elementToEvents . toXMLElement) cols
@@ -409,7 +411,7 @@ mapRow ::
409411
mapRow sharedStrings' sheetItem = do
410412
mRowProp <-
411413
preview $
412-
wsRowProperties
414+
#wsRowProperties
413415
. ix (unRowIndex rowIx)
414416
. rowHeightLens
415417
. _Just
@@ -446,7 +448,9 @@ renderCellType sharedStrings' cell =
446448
maybe
447449
mempty
448450
(attr "t" . renderType sharedStrings')
449-
$ cell ^? cellValue . _Just
451+
$ cell
452+
^? cellValue
453+
. _Just
450454

451455
renderCell :: Map Text Int -> Cell -> Text
452456
renderCell sharedStrings' cell = renderValue sharedStrings' val

pub/xlsx/xlsx.cabal

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ flag ghcid
3636
default: False
3737
description: Run dev ghcid shell
3838

39+
common ext
40+
default-extensions:
41+
AllowAmbiguousTypes
42+
DataKinds
43+
DeriveGeneric
44+
DerivingStrategies
45+
FlexibleContexts
46+
OverloadedLabels
47+
TypeApplications
48+
3949
common pkg
4050
hs-source-dirs: src
4151
build-depends:
@@ -53,6 +63,7 @@ common pkg
5363
, exceptions
5464
, extra
5565
, filepath
66+
, generic-lens
5667
, hexpat
5768
, monad-control
5869
, mtl >=2.1
@@ -98,6 +109,7 @@ common pkg
98109
TupleSections
99110

100111
library
112+
import: ext
101113
import: pkg
102114
ghc-options: -Wall
103115
exposed-modules:
@@ -152,9 +164,14 @@ library
152164
Codec.Xlsx.Parser.Stream.HexpatInternal
153165

154166
test-suite data-test
167+
import: ext
168+
155169
if flag(ghcid)
156170
import: pkg
157171

172+
else
173+
build-depends: xlsx
174+
158175
type: exitcode-stdio-1.0
159176
main-is: Main.hs
160177
hs-source-dirs: test/
@@ -190,7 +207,6 @@ test-suite data-test
190207
, text
191208
, time
192209
, vector
193-
, xlsx
194210
, xml-conduit >=1.1.0
195211
, zip
196212

0 commit comments

Comments
 (0)