a
    còf  ã                   @   s>   d Z ddlZddlmZ G dd„ dejƒZG dd„ deƒ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d |¡S tj | |¡S )NÚexprz{0})ÚhasattrÚformatÚjsonÚJSONEncoderÚdefault)ÚselfÚo© r   úI/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/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: {0})	Ú
isinstanceÚdictÚ__dict__Ústrr   ÚloadsÚ
ValueErrorr   Útype)r   Úvaluer   r   r   Ú__init__7   s
    

zDbDoc.__init__c                 C   s   |   ¡ S ©N)Úas_str©r   r   r   r   Ú__str__?   s    zDbDoc.__str__c                 C   s
   t | jƒS r   )Úreprr   r   r   r   r   Ú__repr__B   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__E   s    zDbDoc.__setitem__c                 C   s
   | j | S r   ©r   )r   r$   r   r   r   Ú__getitem__J   s    zDbDoc.__getitem__c                 C   s
   || j v S r   r&   )r   Úitemr   r   r   Ú__contains__M   s    zDbDoc.__contains__Nc                 C   s.   | j  ¡ }|r||d< nd|v 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*   P   s    

z
DbDoc.copyc                 C   s
   | j  ¡ S )zJReturns the keys.

        Returns:
            `list`: The keys.
        )r   Úkeysr   r   r   r   r+   b   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   j   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   Úobjectr   r   r   r   r   Ú<module>   s   
