a
    !f                     @   s   d Z ddlmZ dd Zdd Zdd Zd	d
 Zdd Zdd Zdd Z	eeeeee	dZ
dd ZG dd deZG dd deZG dd deZdS )z0Shared elper functions for BigQuery API classes.    )_datetime_from_microsecondsc                 C   s   | dup|j dkS )z8Check whether 'value' should be coerced to 'field' type.NZNULLABLE)modevaluefield r   Y/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigquery/_helpers.py	_not_null   s    r	   c                 C   s   t | |rt| S dS )z1Coerce 'value' to an int, if set or not nullable.N)r	   intr   r   r   r   _int_from_json   s    
r   c                 C   s   t | |rt| S dS )z2Coerce 'value' to a float, if set or not nullable.N)r	   floatr   r   r   r   _float_from_json   s    
r   c                 C   s   t | |r|  dv S dS )z1Coerce 'value' to a bool, if set or not nullable.)ttrue1N)r	   lowerr   r   r   r   _bool_from_json%   s    
r   c                 C   s   t | |rtdt|  S dS )z5Coerce 'value' to a datetime, if set or not nullable.g    .AN)r	   r   r   r   r   r   r   _datetime_from_json+   s    
r   c                    sv   t | rri }tj| d D ]N\}}t|j  jdkrT fdd|d D } n |d } | ||j< q|S dS )z4Coerce 'value' to a mapping, if set or not nullable.fREPEATEDc                    s   g | ]} |qS r   r   .0item	converterr   r   r   
<listcomp>9       z%_record_from_json.<locals>.<listcomp>vN)r	   zipfields_CELLDATA_FROM_JSON
field_typer   name)r   r   recordZsubfieldcellr   r   r   _record_from_json2   s    


r%   c                 C   s   | S )zNOOP string -> string coercionr   )r   _r   r   r   _string_from_json@   s    r'   )ZINTEGERFLOATZBOOLEAN	TIMESTAMPRECORDSTRINGc                    s   g }| D ]v}g }t ||d D ]P\}tj  jdkrZ| fdd|d D  q| |d  q|t| q|S )z3Convert JSON row data to rows w/ appropriate types.r   r   c                    s   g | ]} |qS r   r   r   r   r   r   r   W   s   z#_rows_from_json.<locals>.<listcomp>r   )r   r    r!   r   appendtuple)rowsZschemaZ	rows_datarowZrow_datar$   r   r   r   _rows_from_jsonO   s    


r0   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )_ConfigurationPropertyzBase property implementation.

    Values will be stored on a `_configuration` helper attribute of the
    property's job instance.

    :type name: string
    :param name:  name of the property
    c                 C   s   || _ d| j f | _d S )Nz_%s)r"   _backing_name)selfr"   r   r   r   __init__i   s    z_ConfigurationProperty.__init__c                 C   s   |du r| S t |j| jS )zDescriptor protocal:  accesstorN)getattr_configurationr2   )r3   instanceownerr   r   r   __get__m   s    z_ConfigurationProperty.__get__c                 C   s   dS )z0Subclasses override to impose validation policy.Nr   r3   r   r   r   r   	_validates   s    z _ConfigurationProperty._validatec                 C   s   |  | t|j| j| dS )zDescriptor protocal:  mutatorN)r;   setattrr6   r2   )r3   r7   r   r   r   r   __set__w   s    
z_ConfigurationProperty.__set__c                 C   s   t |j| j dS )zDescriptor protocal:  deleterN)delattrr6   r2   )r3   r7   r   r   r   
__delete__|   s    z!_ConfigurationProperty.__delete__N)	__name__
__module____qualname____doc__r4   r9   r;   r=   r?   r   r   r   r   r1   _   s   	r1   c                       s(   e Zd ZdZ fddZdd Z  ZS )_TypedPropertyzProperty implementation:  validates based on value type.

    :type name: string
    :param name:  name of the property

    :type property_type: type or sequence of types
    :param property_type: type to be validated
    c                    s   t t| | || _d S )N)superrD   r4   property_type)r3   r"   rF   	__class__r   r   r4      s    z_TypedProperty.__init__c                 C   s    t || jstd| jf dS )ziEnsure that 'value' is of the appropriate type.

        :raises: ValueError on a type mismatch.
        zRequired type: %sN)
isinstancerF   
ValueErrorr:   r   r   r   r;      s    z_TypedProperty._validate)r@   rA   rB   rC   r4   r;   __classcell__r   r   rG   r   rD      s   rD   c                   @   s   e Zd ZdZdd ZdS )_EnumPropertyzPsedo-enumeration class.

    Subclasses must define ``ALLOWED`` as a class-level constant:  it must
    be a sequence of strings.

    :type name: string
    :param name:  name of the property
    c                 C   s   || j vrtd| j dS )zqCheck that ``value`` is one of the allowed values.

        :raises: ValueError if value is not allowed.
        zPass one of: %s, N)ZALLOWEDrJ   joinr:   r   r   r   r;      s    
z_EnumProperty._validateN)r@   rA   rB   rC   r;   r   r   r   r   rL      s   rL   N)rC   Zgcloud._helpersr   r	   r   r   r   r   r%   r'   r    r0   objectr1   rD   rL   r   r   r   r   <module>   s&   
"