changeset 31928:277b3e2d711b

templater: add shorthand for building a dict like {"key": key} Like field init shorthand of Rust. This is convenient for building a JSON object from selected keywords. This means dict() won't support Python-like dict(iterable) syntax because it's ambiguous. Perhaps it could be implemented as 'mapdict(xs % (k, v))'.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 03 Apr 2017 23:13:49 +0900
parents 2abc556dbe92
children bf6b44da1d8e
files mercurial/templater.py tests/test-command-template.t
diffstat 2 files changed, 27 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Sat Apr 08 23:33:32 2017 +0900
+++ b/mercurial/templater.py	Mon Apr 03 23:13:49 2017 +0900
@@ -545,10 +545,20 @@
         # i18n: "date" is a keyword
         raise error.ParseError(_("date expects a date information"))
 
-@templatefunc('dict([key=value...])', argspec='**kwargs')
+@templatefunc('dict([[key=]value...])', argspec='*args **kwargs')
 def dict_(context, mapping, args):
-    """Construct a dict from key-value pairs."""
+    """Construct a dict from key-value pairs. A key may be omitted if
+    a value expression can provide an unambiguous name."""
     data = util.sortdict()
+
+    for v in args['args']:
+        k = findsymbolicname(v)
+        if not k:
+            raise error.ParseError(_('dict key cannot be inferred'))
+        if k in data or k in args['kwargs']:
+            raise error.ParseError(_("duplicated dict key '%s' inferred") % k)
+        data[k] = evalfuncarg(context, mapping, v)
+
     data.update((k, evalfuncarg(context, mapping, v))
                 for k, v in args['kwargs'].iteritems())
     return templatekw.hybriddict(data)
--- a/tests/test-command-template.t	Sat Apr 08 23:33:32 2017 +0900
+++ b/tests/test-command-template.t	Mon Apr 03 23:13:49 2017 +0900
@@ -3424,6 +3424,21 @@
   $ hg log -r 0 -T '{dict()|json}\n'
   {}
 
+  $ hg log -r 0 -T '{dict(rev, node=node|short)}\n'
+  rev=0 node=f7769ec2ab97
+  $ hg log -r 0 -T '{dict(rev, node|short)}\n'
+  rev=0 node=f7769ec2ab97
+
+  $ hg log -r 0 -T '{dict(rev, rev=rev)}\n'
+  hg: parse error: duplicated dict key 'rev' inferred
+  [255]
+  $ hg log -r 0 -T '{dict(node, node|short)}\n'
+  hg: parse error: duplicated dict key 'node' inferred
+  [255]
+  $ hg log -r 0 -T '{dict(1 + 2)}'
+  hg: parse error: dict key cannot be inferred
+  [255]
+
   $ hg log -r 0 -T '{dict(x=rev, x=node)}'
   hg: parse error: dict got multiple values for keyword argument 'x'
   [255]