Patchwork [8,of,8,V2] templater: define interface for objects requiring unwrapvalue()

login
register
mail settings
Submitter Yuya Nishihara
Date April 3, 2018, 3:40 p.m.
Message ID <b9a554cd2c3bb5d0701f.1522770010@mimosa>
Download mbox | patch
Permalink /patch/30169/
State Accepted
Headers show

Comments

Yuya Nishihara - April 3, 2018, 3:40 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521288196 -32400
#      Sat Mar 17 21:03:16 2018 +0900
# Node ID b9a554cd2c3bb5d0701f24c67d36e24386b54f6a
# Parent  7404b3400212b00845411af13a69ac753bff6bb2
templater: define interface for objects requiring unwrapvalue()

unwrapvalue() is changed to not return a lazy bytes generator for "wrapped"
types because I want to define the tovalue() interface as such. It's a baby
step to unify unwrapvalue() and _unwrapvalue().
via Mercurial-devel - April 3, 2018, 4:45 p.m.
On Tue, Apr 3, 2018 at 8:40 AM Yuya Nishihara <yuya@tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1521288196 -32400
> #      Sat Mar 17 21:03:16 2018 +0900
> # Node ID b9a554cd2c3bb5d0701f24c67d36e24386b54f6a
> # Parent  7404b3400212b00845411af13a69ac753bff6bb2
> templater: define interface for objects requiring unwrapvalue()
>

 Queued, thanks!

Patch

diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -29,7 +29,11 @@  class TemplateNotFound(error.Abort):
 
 class wrapped(object):
     """Object requiring extra conversion prior to displaying or processing
-    as value"""
+    as value
+
+    Use unwrapvalue(), unwrapastype(), or unwraphybrid() to obtain the inner
+    object.
+    """
 
     __metaclass__ = abc.ABCMeta
 
@@ -42,6 +46,13 @@  class wrapped(object):
         not printable.
         """
 
+    @abc.abstractmethod
+    def tovalue(self, context, mapping):
+        """Move the inner value object out or create a value representation
+
+        A returned value must be serializable by templaterfilters.json().
+        """
+
 # stub for representing a date type; may be a real date type that can
 # provide a readable string value
 class date(object):
@@ -85,6 +96,10 @@  class hybrid(wrapped):
             return gen()
         return gen
 
+    def tovalue(self, context, mapping):
+        # TODO: return self._values and get rid of proxy methods
+        return self
+
     def __contains__(self, x):
         return x in self._values
     def __getitem__(self, key):
@@ -108,8 +123,7 @@  class mappable(wrapped):
     - "{manifest.rev}"
 
     Unlike a hybrid, this does not simulate the behavior of the underling
-    value. Use unwrapvalue(), unwrapastype(), or unwraphybrid() to obtain
-    the inner object.
+    value.
     """
 
     def __init__(self, gen, key, value, makemap):
@@ -133,6 +147,9 @@  class mappable(wrapped):
             return gen()
         return gen
 
+    def tovalue(self, context, mapping):
+        return _unthunk(context, mapping, self._value)
+
 def hybriddict(data, key='key', value='value', fmt=None, gen=None):
     """Wrap data to support both dict-like and string-like operations"""
     prefmt = pycompat.identity
@@ -159,9 +176,9 @@  def unwraphybrid(context, mapping, thing
 
 def unwrapvalue(context, mapping, thing):
     """Move the inner value object out of the wrapper"""
-    if not util.safehasattr(thing, '_value'):
+    if not isinstance(thing, wrapped):
         return thing
-    return thing._value
+    return thing.tovalue(context, mapping)
 
 def wraphybridvalue(container, key, value):
     """Wrap an element of hybrid container to be mappable