changeset 44596:7333e8bb9781

templater: fix cbor() filter to recursively convert smartset to list The previous attempt, e3e44e6e7245 "templater: fix cbor() filter to accept smartset", was incomplete since obj may be a collection containing a smartset. This works around the problem by converting smartsets recursively. Another option is to teach cborutil how to encode a smartset. That should be okay, but I hesitated to add "import smartset" to cborutil.py as the cborutil is pretty generic.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 26 Mar 2020 00:07:12 +0900
parents 47c1226463a1
children e62052d0f377
files mercurial/templatefilters.py tests/test-template-functions.t
diffstat 2 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Mon Mar 23 15:14:42 2020 -0700
+++ b/mercurial/templatefilters.py	Thu Mar 26 00:07:12 2020 +0900
@@ -106,12 +106,17 @@
     return os.path.basename(path)
 
 
+def _tocborencodable(obj):
+    if isinstance(obj, smartset.abstractsmartset):
+        return list(obj)
+    return obj
+
+
 @templatefilter(b'cbor')
 def cbor(obj):
     """Any object. Serializes the object to CBOR bytes."""
-    if isinstance(obj, smartset.abstractsmartset):
-        # cborutil is stricter about type than json() filter
-        obj = list(obj)
+    # cborutil is stricter about type than json() filter
+    obj = pycompat.rapply(_tocborencodable, obj)
     return b''.join(cborutil.streamencode(obj))
 
 
--- a/tests/test-template-functions.t	Mon Mar 23 15:14:42 2020 -0700
+++ b/tests/test-template-functions.t	Thu Mar 26 00:07:12 2020 +0900
@@ -1616,6 +1616,15 @@
    ]
   ]
 
+  $ hg log -T "{dict(foo=revset('.'))|cbor}" -R a -l1 | "$PYTHON" "$TESTTMP/decodecbor.py"
+  [
+   {
+    'foo': [
+     10
+    ]
+   }
+  ]
+
 json filter should escape HTML tags so that the output can be embedded in hgweb:
 
   $ hg log -T "{'<foo@example.org>'|json}\n" -R a -l1