You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2019/06/12 15:00:55 UTC

[GitHub] [airflow] ashb commented on a change in pull request #4743: [AIRFLOW-3871] render Operators template fields recursively

ashb commented on a change in pull request #4743: [AIRFLOW-3871] render Operators template fields recursively
URL: https://github.com/apache/airflow/pull/4743#discussion_r292963159
 
 

 ##########
 File path: airflow/models/baseoperator.py
 ##########
 @@ -644,9 +648,23 @@ def render_template_from_field(self, attr, content, context, jinja_env):
                 k: rt("{}[{}]".format(attr, k), v, context)
                 for k, v in list(content.items())}
         else:
-            result = content
+            result = self._render_nested_template_fields(content, context)
         return result
 
+    def _render_nested_template_fields(self, content, context):
 
 Review comment:
   I think this diff should work:
   
   
   ```diff
   diff --git a/airflow/models/baseoperator.py b/airflow/models/baseoperator.py
   index b71f3c4ca1..3e081a5e3d 100644
   --- a/airflow/models/baseoperator.py
   +++ b/airflow/models/baseoperator.py
   @@ -632,7 +633,10 @@ def render_template_from_field(self, attr, content, context, jinja_env):
            Renders a template from a field. If the field is a string, it will
            simply render the string and return the result. If it is a collection or
            nested set of collections, it will traverse the structure and render
   -        all elements in it. If the field has another type, it will return it as it is.
   +        all elements in it. For any other type, it will recursively render attributes
   +        listed in its 'template_fields' attribute (class or instance level attribute)
   +        when this 'template_fields' is defined only.
   +        Finally returns the rendered field.
            """
            rt = self.render_template
            if isinstance(content, six.string_types):
   @@ -644,9 +648,23 @@ def render_template_from_field(self, attr, content, context, jinja_env):
                    k: rt("{}[{}]".format(attr, k), v, context)
                    for k, v in list(content.items())}
            else:
   -            result = content
   +            result = self._render_nested_template_fields(content, context, seen_oids=set())
            return result
    
   +    def _render_nested_template_fields(self, content, context, seen_oids):
   +        if id(content) not in seen_oids:
   +            seen_oids.add(id(content))
   +            try:
   +                nested_template_fields = content.template_fields
   +            except AttributeError:
   +                # content has no inner template fields
   +                return content
   +
   +            for field in nested_template_fields:
   +                rendered = self.render_template(field, getattr(content, field), context, seen_oids=seen_oids)
   +                setattr(content, field, rendered)
   +        return content
   +
        def render_template(self, attr, content, context):
            """
            Renders a template either from a file or directly in a field, and returns
   ```
   
   But I may have missed something.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services