Example #1
0
    def apply(self, data):
        """ Apply the given mapping to ``data``, recursively. The return type
        is a tuple of a boolean and the resulting data element. The boolean
        indicates whether any values were mapped in the child nodes of the
        mapping. It is used to skip optional branches of the object graph. """
        if self.visitor.is_object:
            obj = {}
            if self.visitor.parent is None:
                obj['$schema'] = self.visitor.path
            obj_empty = True
            for child in self.children:
                empty, value = child.apply(data)
                if empty and child.optional:
                    continue
                obj_empty = False if not empty else obj_empty

                if child.visitor.name in obj and child.visitor.is_array:
                    obj[child.visitor.name].extend(value)
                else:
                    obj[child.visitor.name] = value
            return obj_empty, obj

        elif self.visitor.is_array:
            empty, value = self.children.apply(data)
            return empty, [value]

        elif self.visitor.is_value:
            return extract_value(self.mapping, self.visitor, data)
Example #2
0
    def apply(self, data):
        """ Apply the given mapping to ``data``, recursively. The return type
        is a tuple of a boolean and the resulting data element. The boolean
        indicates whether any values were mapped in the child nodes of the
        mapping. It is used to skip optional branches of the object graph. """
        if self.visitor.is_object:
            obj = {}
            if self.visitor.parent is None:
                obj['$schema'] = self.visitor.path
            obj_empty = True
            for child in self.children:
                empty, value = child.apply(data)
                if empty and child.optional:
                    continue
                obj_empty = False if not empty else obj_empty

                if child.visitor.name in obj and child.visitor.is_array:
                    obj[child.visitor.name].extend(value)
                else:
                    obj[child.visitor.name] = value
            return obj_empty, obj

        elif self.visitor.is_array:
            empty, value = self.children.apply(data)
            return empty, [value]

        elif self.visitor.is_value:
            return extract_value(self.mapping, self.visitor, data)
Example #3
0
    def apply(self, data):
        """ Apply the given mapping to ``data``, recursively. The return type
        is a tuple of a boolean and the resulting data element. The boolean
        indicates whether any values were mapped in the child nodes of the
        mapping. It is used to skip optional branches of the object graph. """
        if self.bind.is_object:
            obj = {}
            if self.parent is None:
                obj['$schema'] = self.bind.path
            obj_empty = True
            for child in self.children:
                empty, value = child.apply(data)
                if empty and child.optional:
                    continue
                obj_empty = False if not empty else obj_empty

                if child.name in obj and child.bind.is_array:
                    obj[child.name].extend(value)
                else:
                    obj[child.name] = value
            return obj_empty, obj

        elif self.bind.is_array:
            for item in self.bind.items:
                bind = Mapper(self.mapping, self.resolver,
                              schema=item.schema, bind=item,
                              parent=self, name=self.name)
                empty, value = bind.apply(data)
                return empty, [value] if not isinstance(value, list) else value

        elif self.bind.is_value:
            return extract_value(self.mapping, self.bind, data)