gdritter repos config-ini / 37404dc
Don't emit extra newlines in serialization (fixes #9) Getty Ritter 6 years ago
1 changed file(s) with 23 addition(s) and 13 deletion(s). Collapse all Expand all
6969
7070 import Data.Ini.Config.Raw
7171
72 -- | This is a "lens"-compatible type alias
72 -- * Utility functions
73
74 -- | This is a
75 -- <https://hackage.haskell.org/package/lens lens>-compatible
76 -- type alias
7377 type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
7478
7579 lkp :: Text -> Seq (Text, a) -> Maybe a
8286 rmv :: Text -> Seq (Field s) -> Seq (Field s)
8387 rmv n = Seq.filter (\ f -> T.toLower (fieldName f) /= T.toLower n)
8488
85 fieldName :: Field s -> Text
86 fieldName (Field _ FieldDescription { fdName = n }) = n
87 fieldName (FieldMb _ FieldDescription { fdName = n }) = n
88
89 fieldComment :: Field s -> Seq Text
90 fieldComment (Field _ FieldDescription { fdComment = n }) = n
91 fieldComment (FieldMb _ FieldDescription { fdComment = n }) = n
92
89 -- The & operator is really useful here, but it didn't show up in
90 -- earlier versions, so it gets redefined here.
9391 #if __GLASGOW_HASKELL__ < 710
9492 {- | '&' is a reverse application operator. This provides notational
9593 convenience. Its precedence is one higher than that of the
9997 a & f = f a
10098 infixl 1 &
10199 #endif
100
101 -- * Type definitions
102102
103103 -- | A value of type "FieldValue" packages up a parser and emitter
104104 -- function into a single value. These are used for bidirectional
155155 data Field s
156156 = forall a. Eq a => Field (Lens s s a a) (FieldDescription a)
157157 | forall a. Eq a => FieldMb (Lens s s (Maybe a) (Maybe a)) (FieldDescription a)
158
159 -- convenience accessors for things in a Field
160 fieldName :: Field s -> Text
161 fieldName (Field _ FieldDescription { fdName = n }) = n
162 fieldName (FieldMb _ FieldDescription { fdName = n }) = n
163
164 fieldComment :: Field s -> Seq Text
165 fieldComment (Field _ FieldDescription { fdComment = n }) = n
166 fieldComment (FieldMb _ FieldDescription { fdComment = n }) = n
158167
159168 -- | A 'FieldDescription' is a declarative representation of the
160169 -- structure of a field. This includes the name of the field and the
417426 { vLineNo = 0
418427 , vName = fdName descr
419428 , vValue = val
420 , vComments = BlankLine <| mkComments (fdComment descr)
429 , vComments = mkComments (fdComment descr)
421430 , vCommentedOut = optional
422431 , vDelimiter = '='
423432 }
506515 F.for sections $ \ (name, sec) -> do
507516 let err = (Left ("Unexpected top-level section: " ++ show name))
508517 Section _ spec _ <- maybe err Right
509 (F.find (\ (Section n _ _) -> n == name) fields)
518 (F.find (\ (Section n _ _) -> T.toLower n == name) fields)
510519 newVals <- updateIniSection s (isVals sec) spec pol
511520 return (name, sec { isVals = newVals })
512521
653662 > user = terry
654663
655664 We'd like to parse this INI file into a @Config@ type which we've
656 defined like this, using "lens" or a similar library to provide
657 lenses:
665 defined like this, using
666 <https://hackage.haskell.org/package/lens lens> or a similar library
667 to provide lenses:
658668
659669 > data Config = Config
660670 > { _cfHost :: String