[PATCH] templater: fix cbor() filter to recursively convert smartset to list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] templater: fix cbor() filter to recursively convert smartset to list

Yuya Nishihara
# HG changeset patch
# User Yuya Nishihara <[hidden email]>
# Date 1585148832 -32400
#      Thu Mar 26 00:07:12 2020 +0900
# Node ID 04ec1c9dd3ead2d90a6e4c43d3f9500a5da4a409
# Parent  496868f1030c695db7c68ed5ff558cbbb7ee4b88
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.

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -106,12 +106,17 @@ def basename(path):
     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))
 
 
diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -1616,6 +1616,15 @@ Test cbor filter:
    ]
   ]
 
+  $ 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 "{'<[hidden email]>'|json}\n" -R a -l1
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] templater: fix cbor() filter to recursively convert smartset to list

Augie Fackler-2
queued, thanks

> On Mar 25, 2020, at 12:11, Yuya Nishihara <[hidden email]> wrote:
>
> # HG changeset patch
> # User Yuya Nishihara <[hidden email]>
> # Date 1585148832 -32400
> #      Thu Mar 26 00:07:12 2020 +0900
> # Node ID 04ec1c9dd3ead2d90a6e4c43d3f9500a5da4a409
> # Parent  496868f1030c695db7c68ed5ff558cbbb7ee4b88
> 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.
>
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -106,12 +106,17 @@ def basename(path):
>     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))
>
>
> diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
> --- a/tests/test-template-functions.t
> +++ b/tests/test-template-functions.t
> @@ -1616,6 +1616,15 @@ Test cbor filter:
>    ]
>   ]
>
> +  $ 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 "{'<[hidden email]>'|json}\n" -R a -l1
> _______________________________________________
> Mercurial-devel mailing list
> [hidden email]
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel