templater: make _hybrid provide more list/dict-like methods
authorYuya Nishihara <yuya@tcha.org>
Tue, 04 Apr 2017 22:31:59 +0900
changeset 31882 ba5b74f780d5
parent 31881 31dad7a5b4ed
child 31883 25be03a33f50
templater: make _hybrid provide more list/dict-like methods So the JSON filter works.
mercurial/templatekw.py
tests/test-command-template.t
--- a/mercurial/templatekw.py	Tue Apr 04 22:20:06 2017 +0900
+++ b/mercurial/templatekw.py	Tue Apr 04 22:31:59 2017 +0900
@@ -28,6 +28,7 @@
     and to access raw values:
     - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}"
     - "{get(extras, key)}"
+    - "{files|json}"
     """
 
     def __init__(self, gen, values, makemap, joinfmt):
@@ -43,8 +44,11 @@
         return x in self._values
     def __len__(self):
         return len(self._values)
+    def __iter__(self):
+        return iter(self._values)
     def __getattr__(self, name):
-        if name != 'get':
+        if name not in ('get', 'items', 'iteritems', 'iterkeys', 'itervalues',
+                        'keys', 'values'):
             raise AttributeError(name)
         return getattr(self._values, name)
 
--- a/tests/test-command-template.t	Tue Apr 04 22:20:06 2017 +0900
+++ b/tests/test-command-template.t	Tue Apr 04 22:31:59 2017 +0900
@@ -3392,6 +3392,13 @@
   hg: parse error: get() expects a dict as first argument
   [255]
 
+Test json filter applied to hybrid object:
+
+  $ hg log -r0 -T '{files|json}\n'
+  ["a"]
+  $ hg log -r0 -T '{extras|json}\n'
+  {"branch": "default"}
+
 Test localdate(date, tz) function:
 
   $ TZ=JST-09 hg log -r0 -T '{date|localdate|isodate}\n'