# HG changeset patch # User Yuya Nishihara # Date 1491228829 -32400 # Node ID 277b3e2d711bd7ddc11be0ecc1004c3fe0e75f65 # Parent 2abc556dbe924df6f43fdae9f776a765a9212586 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))'. diff -r 2abc556dbe92 -r 277b3e2d711b mercurial/templater.py --- 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) diff -r 2abc556dbe92 -r 277b3e2d711b tests/test-command-template.t --- 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]