U
    ZfD                     @   s   d dl mZmZmZmZ d dlZd dlZd dlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZ d dlmZmZ ede	de
dedededededededed	ed
iZdd ZdddZdd Zdd Zdd Zdd Z dd Z!dd Z"dd Z#dS )    )absolute_importdivisionprint_functionunicode_literalsN)BooleanBytesFloat32Float64Int32Int64ListString	TimestampUInt32UInt64Voidis_list_typeis_struct_typeis_user_defined_type)	fmt_camel
fmt_pascalbooleanstringnumberArrayr   voidc                 C   s,   t | tjrt| dS tj| ddS d S )Nu   )indent)
isinstancesix	text_typereprlstripjsondumps)o r'   =/tmp/pip-unpacked-wheel-vqb3mfeu/stone/backends/js_helpers.pyfmt_obj,   s    r)    c                 C   s   d|dkr|ndt | f S )z4
    Converts the error type into a JSDoc type.
    z%s.<%s>r*   Error)fmt_type)	data_typeZwrap_error_inr'   r'   r(   fmt_error_type4   s    r.   c                 C   sR   t | rtd| jj| jf S t| jd}t| rJ|dt| j	 d 7 }|S dS )zg
    Returns the JSDoc name for the given data type.
    (Does not attempt to enumerate subtypes.)
    z%s%sZObjectz.<>N)
r   r   	namespacename_base_type_tableget	__class__r   r,   r-   )r-   Z
fmted_typer'   r'   r(   fmt_type_name>   s    r5   c                 C   sb   t | rV|  rVg }|  }|D ]\}}|t| q |  rN|t|  t|S t| S dS )ze
    Returns a JSDoc annotation for a data type.
    May contain a union of enumerated subtypes.
    N)r   Zhas_enumerated_subtypesZget_all_subtypes_with_tagsappendr5   Zis_catch_allfmt_jsdoc_union)r-   Zpossible_typesZpossible_subtypes_subtyper'   r'   r(   r,   L   s    r,   c                 C   s&   t | dkrdd|  d S | d S )z:
    Returns a JSDoc union of the given type strings.
       (|)r   )lenjoin)Ztype_stringsr'   r'   r(   r7   ]   s    r7   c                 C   s"   |dkrt | S t | d| S )Nr:   zV{})r   format)r1   versionr'   r'   r(   fmt_funcd   s    rB   c                 C   s&   |dkrd | ||S d | |S d S )Nr:   z	{}/{}_v{}z{}/{})r@   )Znamespace_name
route_nameZroute_versionr'   r'   r(   fmt_urlj   s    rD   c                 C   s   t | S )N)r   )r1   r'   r'   r(   fmt_varq   s    rE   c                 C   sL   i }| j D ]<}t|j|jd}||kr>|| }td|||||< q
dS )z
    Check name conflicts among generated route definitions. Raise a runtime exception when a
    conflict is encountered.
    )rA   z.There is a name conflict between {!r} and {!r}N)ZroutesrB   r1   rA   RuntimeErrorr@   )r0   Zroute_by_nameZrouterC   Zother_router'   r'   r(   check_route_name_conflictu   s    

rG   )r*   )$
__future__r   r   r   r   r$   r    Zstone.irr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   Zstone.backends.helpersr   r   r2   r)   r.   r5   r,   r7   rB   rD   rE   rG   r'   r'   r'   r(   <module>   sL   D            

