U
    4Je                     @  sd   d Z ddlmZ ddlZddlmZmZmZmZm	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.    )annotationsN)AnyDictKeysViewOptionalUnion   )ProgrammingErrorc                   @  s    e Zd ZdZdddddZdS )ExprJSONEncoderzgA :class:`json.JSONEncoder` subclass, which enables encoding of
    :class:`mysqlx.ExprParser` objects.objectstr)oreturnc                 C  s   t |dr| S tj| |S )Nexpr)hasattrjsonJSONEncoderdefault)selfr    r   0/tmp/pip-unpacked-wheel-7_167w8m/mysqlx/dbdoc.pyr   ,   s    
zExprJSONEncoder.defaultN)__name__
__module____qualname____doc__r   r   r   r   r   r
   (   s   r
   c                   @  s   e Zd ZdZdddddZddd	d
ZddddZddddddZdddddZdddddZ	d"dd dddZ
ddddZddd d!ZdS )#DbDoczRepresents 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.
    zUnion[str, Dict[str, Any]]None)valuer   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__r   r   loads
ValueErrortype)r   r   r   r   r   __init__=   s
    

zDbDoc.__init__r   )r   c                 C  s   |   S N)as_strr   r   r   r   __str__E   s    zDbDoc.__str__c                 C  s
   t | jS r%   )reprr    r'   r   r   r   __repr__H   s    zDbDoc.__repr__r   )indexr   r   c                 C  s   |dkrt d|| j|< d S )N_idzCannot modify _id)r	   r    )r   r+   r   r   r   r   __setitem__K   s    zDbDoc.__setitem__)r+   r   c                 C  s
   | j | S r%   r    )r   r+   r   r   r   __getitem__P   s    zDbDoc.__getitem__bool)itemr   c                 C  s
   || j kS r%   r.   )r   r1   r   r   r   __contains__S   s    zDbDoc.__contains__NzOptional[str])doc_idr   c                 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   r3   Znew_dictr   r   r   r4   V   s    

z
DbDoc.copyzKeysView[str]c                 C  s
   | j  S )zJReturns the keys.

        Returns:
            `list`: The keys.
        )r    keysr'   r   r   r   r5   h   s    z
DbDoc.keysc                 C  s   t j| jtdS )zSerialize :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&   p   s    zDbDoc.as_str)N)r   r   r   r   r$   r(   r*   r-   r/   r2   r4   r5   r&   r   r   r   r   r   3   s   	r   )r   
__future__r   r   typingr   r   r   r   r   errorsr	   r   r
   r   r   r   r   r   <module>   s   