##// END OF EJS Templates
templater: add shorthand for building a dict like {"key": key}...
Yuya Nishihara -
r31928:277b3e2d default
parent child Browse files
Show More
@@ -545,10 +545,20 b' def date(context, mapping, args):'
545 # i18n: "date" is a keyword
545 # i18n: "date" is a keyword
546 raise error.ParseError(_("date expects a date information"))
546 raise error.ParseError(_("date expects a date information"))
547
547
548 @templatefunc('dict([key=value...])', argspec='**kwargs')
548 @templatefunc('dict([[key=]value...])', argspec='*args **kwargs')
549 def dict_(context, mapping, args):
549 def dict_(context, mapping, args):
550 """Construct a dict from key-value pairs."""
550 """Construct a dict from key-value pairs. A key may be omitted if
551 a value expression can provide an unambiguous name."""
551 data = util.sortdict()
552 data = util.sortdict()
553
554 for v in args['args']:
555 k = findsymbolicname(v)
556 if not k:
557 raise error.ParseError(_('dict key cannot be inferred'))
558 if k in data or k in args['kwargs']:
559 raise error.ParseError(_("duplicated dict key '%s' inferred") % k)
560 data[k] = evalfuncarg(context, mapping, v)
561
552 data.update((k, evalfuncarg(context, mapping, v))
562 data.update((k, evalfuncarg(context, mapping, v))
553 for k, v in args['kwargs'].iteritems())
563 for k, v in args['kwargs'].iteritems())
554 return templatekw.hybriddict(data)
564 return templatekw.hybriddict(data)
@@ -3424,6 +3424,21 b' Test dict constructor:'
3424 $ hg log -r 0 -T '{dict()|json}\n'
3424 $ hg log -r 0 -T '{dict()|json}\n'
3425 {}
3425 {}
3426
3426
3427 $ hg log -r 0 -T '{dict(rev, node=node|short)}\n'
3428 rev=0 node=f7769ec2ab97
3429 $ hg log -r 0 -T '{dict(rev, node|short)}\n'
3430 rev=0 node=f7769ec2ab97
3431
3432 $ hg log -r 0 -T '{dict(rev, rev=rev)}\n'
3433 hg: parse error: duplicated dict key 'rev' inferred
3434 [255]
3435 $ hg log -r 0 -T '{dict(node, node|short)}\n'
3436 hg: parse error: duplicated dict key 'node' inferred
3437 [255]
3438 $ hg log -r 0 -T '{dict(1 + 2)}'
3439 hg: parse error: dict key cannot be inferred
3440 [255]
3441
3427 $ hg log -r 0 -T '{dict(x=rev, x=node)}'
3442 $ hg log -r 0 -T '{dict(x=rev, x=node)}'
3428 hg: parse error: dict got multiple values for keyword argument 'x'
3443 hg: parse error: dict got multiple values for keyword argument 'x'
3429 [255]
3444 [255]
General Comments 0
You need to be logged in to leave comments. Login now