|
1 |
{-| Provides all lens-related functions.
|
|
2 |
|
|
3 |
-}
|
|
4 |
|
|
5 |
{-
|
|
6 |
|
|
7 |
Copyright (C) 2014 Google Inc.
|
|
8 |
|
|
9 |
This program is free software; you can redistribute it and/or modify
|
|
10 |
it under the terms of the GNU General Public License as published by
|
|
11 |
the Free Software Foundation; either version 2 of the License, or
|
|
12 |
(at your option) any later version.
|
|
13 |
|
|
14 |
This program is distributed in the hope that it will be useful, but
|
|
15 |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 |
General Public License for more details.
|
|
18 |
|
|
19 |
You should have received a copy of the GNU General Public License
|
|
20 |
along with this program; if not, write to the Free Software
|
|
21 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
22 |
02110-1301, USA.
|
|
23 |
|
|
24 |
-}
|
|
25 |
|
|
26 |
module Ganeti.Lens
|
|
27 |
( module Control.Lens
|
|
28 |
, makeCustomLenses
|
|
29 |
, makeCustomLenses'
|
|
30 |
) where
|
|
31 |
|
|
32 |
import Control.Lens
|
|
33 |
import Control.Monad
|
|
34 |
import qualified Data.Set as S
|
|
35 |
import Language.Haskell.TH
|
|
36 |
|
|
37 |
lensFieldName :: String -> String
|
|
38 |
lensFieldName = (++ "L")
|
|
39 |
|
|
40 |
-- | Internal helper method for constructing partial set of lenses.
|
|
41 |
makeCustomLensesFiltered :: (String -> Bool) -> Name -> Q [Dec]
|
|
42 |
makeCustomLensesFiltered f = makeLensesWith customRules
|
|
43 |
where
|
|
44 |
customRules :: LensRules
|
|
45 |
customRules = set lensField (fmap lensFieldName . mfilter f . Just)
|
|
46 |
defaultRules
|
|
47 |
|
|
48 |
-- | Create lenses for all fields of a given data type.
|
|
49 |
makeCustomLenses :: Name -> Q [Dec]
|
|
50 |
makeCustomLenses = makeCustomLensesFiltered (const True)
|
|
51 |
|
|
52 |
-- | Create lenses for some fields of a given data type.
|
|
53 |
makeCustomLenses' :: Name -> [Name] -> Q [Dec]
|
|
54 |
makeCustomLenses' name lst = makeCustomLensesFiltered f name
|
|
55 |
where
|
|
56 |
allowed = S.fromList . map nameBase $ lst
|
|
57 |
f = flip S.member allowed
|