Ejemplo n.º 1
0
def map_edge_to_ogm(result, props, element, *, mapping=None):
    """Map an edge returned by DB to OGM edge"""
    props.pop('id')
    label = props.pop('label')
    for db_name, value in props.items():
        name, data_type = mapping.db_properties.get(db_name, (db_name, None))
        if data_type:
            value = data_type.to_ogm(value)
        setattr(element, name, value)
    setattr(element, '__label__', label)
    setattr(element, 'id', result.id)
    # Currently not included in graphson
    # setattr(element.source, '__label__', result.outV.label)
    # setattr(element.target, '__label__', result.inV.label)
    sid = result.outV.id
    esid = getattr(element.source, 'id', None)
    if _check_id(sid, esid):
        from goblin.element import GenericVertex
        element.source = GenericVertex()
    tid = result.inV.id
    etid = getattr(element.target, 'id', None)
    if _check_id(tid, etid):
        from goblin.element import GenericVertex
        element.target = GenericVertex()
    setattr(element.source, 'id', sid)
    setattr(element.target, 'id', tid)
    return element
Ejemplo n.º 2
0
 async def _deserialize_result(self, result):
     if isinstance(result, Traverser):
         bulk = result.bulk
         obj = result.object
         if isinstance(obj, (Vertex, Edge)):
             hashable_id = self._get_hashable_id(obj.id)
             current = self.current.get(hashable_id, None)
             if isinstance(obj, Vertex):
                 # why doesn't this come in on the vertex?
                 label = await self._g.V(obj.id).label().next()
                 if not current:
                     current = self.app.vertices.get(label, GenericVertex)()
                 props = await self._get_vertex_properties(obj.id, label)
             if isinstance(obj, Edge):
                 props = await self._g.E(obj.id).valueMap(True).next()
                 if not current:
                     current = self.app.edges.get(
                         props.get(T.label), GenericEdge)()
                     current.source = GenericVertex()
                     current.target = GenericVertex()
             element = current.__mapping__.mapper_func(obj, props, current)
             self.current[hashable_id] = element
             return Traverser(element, bulk)
         else:
             return result
     # Recursive serialization is broken in goblin
     elif isinstance(result, dict):
         for key in result:
             result[key] = self._deserialize_result(result[key])
         return result
     elif isinstance(result, list):
         return [self._deserialize_result(item) for item in result]
     else:
         return result
Ejemplo n.º 3
0
 async def _deserialize_result(self, result):
     if isinstance(result, Traverser):
         bulk = result.bulk
         obj = result.object
         if isinstance(obj, (Vertex, Edge)):
             hashable_id = self._get_hashable_id(obj.id)
             current = self.current.get(hashable_id, None)
             if isinstance(obj, Vertex):
                 props = await self._g.V(obj.id).valueMap(True).next()
                 if not current:
                     current = self.app.vertices.get(
                         props.get('label'), GenericVertex)()
                 else:
                     props = await self._get_vertex_properties(current, props)
             if isinstance(obj, Edge):
                 props = await self._g.E(obj.id).valueMap(True).next()
                 if not current:
                     current = self.app.edges.get(
                         props.get('label'), GenericEdge)()
                     current.source = GenericVertex()
                     current.target = GenericVertex()
             element = current.__mapping__.mapper_func(
                 obj, props, current)
             return Traverser(element, bulk)
         else:
             return result
     elif isinstance(result, dict):
         for key in result:
             result[key] = self._deserialize_result(result[key])
         return result
     elif isinstance(result, list):
         return [self._deserialize_result(item) for item in result]
     else:
         return result
Ejemplo n.º 4
0
def map_edge_to_ogm(result, element, *, mapping=None):
    """Map an edge returned by DB to OGM edge"""
    for db_name, value in result.get('properties', {}).items():
        name, data_type = mapping.db_properties.get(db_name, (db_name, None))
        if data_type:
            value = data_type.to_ogm(value)
        setattr(element, name, value)
    setattr(element, '__label__', result['label'])
    setattr(element, 'id', result['id'])
    setattr(element.source, '__label__', result['outVLabel'])
    setattr(element.target, '__label__', result['inVLabel'])
    sid = result['outV']
    esid = getattr(element.source, 'id', None)
    if _check_id(sid, esid):
        from goblin.element import GenericVertex
        element.source = GenericVertex()
    tid = result['inV']
    etid = getattr(element.target, 'id', None)
    if _check_id(tid, etid):
        from goblin.element import GenericVertex
        element.target = GenericVertex()
    setattr(element.source, 'id', sid)
    setattr(element.target, 'id', tid)
    return element
Ejemplo n.º 5
0
 def _deserialize_result(self, result):
     if isinstance(result, dict):
         if result.get('type', '') in ['vertex', 'edge']:
             hashable_id = self._get_hashable_id(result['id'])
             current = self.current.get(hashable_id, None)
             if not current:
                 element_type = result['type']
                 label = result['label']
                 if element_type == 'vertex':
                     current = self.app.vertices[label]()
                 else:
                     current = self.app.edges[label]()
                     current.source = GenericVertex()
                     current.target = GenericVertex()
             element = current.__mapping__.mapper_func(result, current)
             return element
         else:
             for key in result:
                 result[key] = self._deserialize_result(result[key])
             return result
     elif isinstance(result, list):
         return [self._deserialize_result(item) for item in result]
     else:
         return result