comparison mercurial/templateutil.py @ 37165:0fb28899e81a

templater: factor out unwrapastype() from evalastype() So ParseError of unwrapastype() can be caught reliably.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 23 Mar 2018 20:43:55 +0900
parents 9ab3491f84c2
children 54355c243042
comparison
equal deleted inserted replaced
37164:9ab3491f84c2 37165:0fb28899e81a
75 - "{manifest}" 75 - "{manifest}"
76 - "{manifest % '{rev}:{node}'}" 76 - "{manifest % '{rev}:{node}'}"
77 - "{manifest.rev}" 77 - "{manifest.rev}"
78 78
79 Unlike a hybrid, this does not simulate the behavior of the underling 79 Unlike a hybrid, this does not simulate the behavior of the underling
80 value. Use unwrapvalue() or unwraphybrid() to obtain the inner object. 80 value. Use unwrapvalue(), unwrapastype(), or unwraphybrid() to obtain
81 the inner object.
81 """ 82 """
82 83
83 def __init__(self, gen, key, value, makemap): 84 def __init__(self, gen, key, value, makemap):
84 if gen is not None: 85 if gen is not None:
85 self.gen = gen # generator or function returning generator 86 self.gen = gen # generator or function returning generator
338 thing = func(context, mapping, data, default=data) 339 thing = func(context, mapping, data, default=data)
339 else: 340 else:
340 thing = func(context, mapping, data) 341 thing = func(context, mapping, data)
341 return stringify(thing) 342 return stringify(thing)
342 343
343 _evalfuncbytype = { 344 _unwrapfuncbytype = {
344 bytes: evalstring, 345 bytes: stringify,
345 int: evalinteger, 346 int: unwrapinteger,
346 } 347 }
347 348
348 def evalastype(context, mapping, arg, typ): 349 def unwrapastype(thing, typ):
349 """Evaluate given argument and coerce its type""" 350 """Move the inner value object out of the wrapper and coerce its type"""
350 try: 351 try:
351 f = _evalfuncbytype[typ] 352 f = _unwrapfuncbytype[typ]
352 except KeyError: 353 except KeyError:
353 raise error.ProgrammingError('invalid type specified: %r' % typ) 354 raise error.ProgrammingError('invalid type specified: %r' % typ)
354 return f(context, mapping, arg) 355 return f(thing)
355 356
356 def runinteger(context, mapping, data): 357 def runinteger(context, mapping, data):
357 return int(data) 358 return int(data)
358 359
359 def runstring(context, mapping, data): 360 def runstring(context, mapping, data):