Mercurial > public > mercurial-scm > hg-stable
diff mercurial/templateutil.py @ 40952:4591c9791a82
templatefuncs: specialize "no match" value of search() to allow % operation
If Python had Maybe or Option, the type of the search() result would be
Option<Mapping>, which can be considered as a 0/1 container of a Mapping.
So it makes sense that {search(r'no match pattern', x) % "whatever"} is
mapped to an empty string.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 12 Dec 2018 22:45:02 +0900 |
parents | 8fa26f3baf30 |
children | 832c59d1196e |
line wrap: on
line diff
--- a/mercurial/templateutil.py Wed Dec 12 22:19:57 2018 +0900 +++ b/mercurial/templateutil.py Wed Dec 12 22:45:02 2018 +0900 @@ -495,6 +495,19 @@ def tovalue(self, context, mapping): return super(mappingdict, self).tovalue(context, mapping)[0] +class mappingnone(wrappedvalue): + """Wrapper for None, but supports map operation + + This represents None of Optional[mappable]. It's similar to + mapplinglist([]), but the underlying value is not [], but None. + """ + + def __init__(self): + super(mappingnone, self).__init__(None) + + def itermaps(self, context): + return iter([]) + class mappedgenerator(wrapped): """Wrapper for generator of strings which acts as a list