U
    Òecë  ã                   @   s<   d Z ddlZddlmZ G dd„ dejƒZG dd„ dƒZdS )	zImplementation of the DbDoc.é    Né   )ÚProgrammingErrorc                   @   s   e Zd ZdZdd„ ZdS )ÚExprJSONEncoderzgA :class:`json.JSONEncoder` subclass, which enables encoding of
    :class:`mysqlx.ExprParser` objects.c                 C   s   t |dƒr|› S tj | |¡S )NÚexpr)ÚhasattrÚjsonÚJSONEncoderÚdefault)ÚselfÚo© r   ú0/tmp/pip-unpacked-wheel-ix8dg7li/mysqlx/dbdoc.pyr	   (   s    
zExprJSONEncoder.defaultN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   $   s   r   c                   @   sZ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	ddd„Z
dd„ Zdd„ ZdS )ÚDbDoczÏRepresents a generic document in JSON format.

    Args:
        value (object): The value can be a JSON string or a dict.

    Raises:
        ValueError: If ``value`` type is not a basestring or dict.
    c                 C   s@   t |tƒr|| _n*t |tƒr*t |¡| _ntdt|ƒ› ƒ‚d S )NzUnable to handle type: )Ú
isinstanceÚdictÚ__dict__Ústrr   ÚloadsÚ
ValueErrorÚtype)r
   Úvaluer   r   r   Ú__init__9   s
    

zDbDoc.__init__c                 C   s   |   ¡ S ©N)Úas_str©r
   r   r   r   Ú__str__A   s    zDbDoc.__str__c                 C   s
   t | jƒS r   )Úreprr   r   r   r   r   Ú__repr__D   s    zDbDoc.__repr__c                 C   s   |dkrt dƒ‚|| j|< d S )NÚ_idzCannot modify _id)r   r   )r
   Úindexr   r   r   r   Ú__setitem__G   s    zDbDoc.__setitem__c                 C   s
   | j | S r   ©r   )r
   r#   r   r   r   Ú__getitem__L   s    zDbDoc.__getitem__c                 C   s
   || j kS r   r%   )r
   Úitemr   r   r   Ú__contains__O   s    zDbDoc.__contains__Nc                 C   s.   | j  ¡ }|r||d< nd|kr&|d= t|ƒS )ae  Returns a new copy of a :class:`mysqlx.DbDoc` object containing the
        `doc_id` provided. If `doc_id` is not provided, it will be removed from
        new :class:`mysqlx.DbDoc` object.

        Args:
            doc_id (Optional[str]): Document ID

        Returns:
            mysqlx.DbDoc: A new instance of DbDoc containing the _id provided
        r"   )r   Úcopyr   )r
   Zdoc_idZnew_dictr   r   r   r)   R   s    

z
DbDoc.copyc                 C   s
   | j  ¡ S )zJReturns the keys.

        Returns:
            `list`: The keys.
        )r   Úkeysr   r   r   r   r*   d   s    z
DbDoc.keysc                 C   s   t j| jtdS )zÃSerialize :class:`mysqlx.DbDoc` to a JSON formatted ``str``.

        Returns:
            str: A JSON formatted ``str`` representation of the document.

        .. versionadded:: 8.0.16
        )Úcls)r   Údumpsr   r   r   r   r   r   r   l   s    zDbDoc.as_str)N)r   r   r   r   r   r   r!   r$   r&   r(   r)   r*   r   r   r   r   r   r   /   s   	
r   )r   r   Úerrorsr   r   r   r   r   r   r   r   Ú<module>   s   