Revision 01606931 htools/Ganeti/Query/Language.hs
b/htools/Ganeti/Query/Language.hs | ||
---|---|---|
29 | 29 |
( Filter(..) |
30 | 30 |
, FilterField |
31 | 31 |
, FilterValue(..) |
32 |
, FilterRegex -- note: we don't export the constructor, must use helpers |
|
33 |
, mkRegex |
|
34 |
, stringRegex |
|
35 |
, compiledRegex |
|
32 | 36 |
, Fields |
33 | 37 |
, Query(..) |
34 | 38 |
, QueryResult(..) |
... | ... | |
53 | 57 |
import Text.JSON.Pretty (pp_value) |
54 | 58 |
import Text.JSON.Types |
55 | 59 |
import Text.JSON |
60 |
import qualified Text.Regex.PCRE as PCRE |
|
56 | 61 |
|
57 | 62 |
import qualified Ganeti.Constants as C |
58 | 63 |
import Ganeti.THH |
... | ... | |
123 | 128 |
| GTFilter a FilterValue -- ^ > <field> <value> |
124 | 129 |
| LEFilter a FilterValue -- ^ <= <field> <value> |
125 | 130 |
| GEFilter a FilterValue -- ^ >= <field> <value> |
126 |
| RegexpFilter a FilterRegexp -- ^ =~ <field> <regexp>
|
|
131 |
| RegexpFilter a FilterRegex -- ^ =~ <field> <regexp>
|
|
127 | 132 |
| ContainsFilter a FilterValue -- ^ =[] <list-field> <value> |
128 | 133 |
deriving (Show, Read, Eq) |
129 | 134 |
|
... | ... | |
270 | 275 |
showJSON = showFilterValue |
271 | 276 |
readJSON = readFilterValue |
272 | 277 |
|
273 |
-- | Regexp to apply to the filter value, for filteriong purposes. |
|
274 |
type FilterRegexp = String |
|
278 |
-- | Regexp to apply to the filter value, for filtering purposes. It |
|
279 |
-- holds both the string format, and the \"compiled\" format, so that |
|
280 |
-- we don't re-compile the regex at each match attempt. |
|
281 |
data FilterRegex = FilterRegex |
|
282 |
{ stringRegex :: String -- ^ The string version of the regex |
|
283 |
, compiledRegex :: PCRE.Regex -- ^ The compiled regex |
|
284 |
} |
|
285 |
|
|
286 |
-- | Builder for 'FilterRegex'. We always attempt to compile the |
|
287 |
-- regular expression on the initialisation of the data structure; |
|
288 |
-- this might fail, if the RE is not well-formed. |
|
289 |
mkRegex :: (Monad m) => String -> m FilterRegex |
|
290 |
mkRegex str = do |
|
291 |
compiled <- case PCRE.getVersion of |
|
292 |
Nothing -> fail "regex-pcre library compiled without\ |
|
293 |
\ libpcre, regex functionality not available" |
|
294 |
_ -> PCRE.makeRegexM str |
|
295 |
return $ FilterRegex str compiled |
|
296 |
|
|
297 |
-- | 'Show' instance: we show the constructor plus the string version |
|
298 |
-- of the regex. |
|
299 |
instance Show FilterRegex where |
|
300 |
show (FilterRegex re _) = "mkRegex " ++ show re |
|
301 |
|
|
302 |
-- | 'Read' instance: we manually read \"mkRegex\" followed by a |
|
303 |
-- string, and build the 'FilterRegex' using that. |
|
304 |
instance Read FilterRegex where |
|
305 |
readsPrec _ str = do |
|
306 |
("mkRegex", s') <- lex str |
|
307 |
(re, s'') <- reads s' |
|
308 |
filterre <- mkRegex re |
|
309 |
return (filterre, s'') |
|
310 |
|
|
311 |
-- | 'Eq' instance: we only compare the string versions of the regexes. |
|
312 |
instance Eq FilterRegex where |
|
313 |
(FilterRegex re1 _) == (FilterRegex re2 _) = re1 == re2 |
|
314 |
|
|
315 |
-- | 'JSON' instance: like for show and read instances, we work only |
|
316 |
-- with the string component. |
|
317 |
instance JSON FilterRegex where |
|
318 |
showJSON (FilterRegex re _) = showJSON re |
|
319 |
readJSON s = do |
|
320 |
re <- readJSON s |
|
321 |
mkRegex re |
|
275 | 322 |
|
276 | 323 |
-- | Name of a field. |
277 | 324 |
type FieldName = String |
Also available in: Unified diff