U
    Zfj                     @   s   d dl mZmZmZmZ d dlmZ d dlm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mZmZ d dl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dZd%ddZd&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_literals)Backend)ApiNamespace)BooleanBytesFloat32Float64Int32Int64ListString	TimestampUInt32UInt64Voidis_aliasis_list_typeis_struct_typeis_map_typeis_user_defined_type	fmt_camelbooleanstringnumberArrayr   voidN c                 C   s   d|dkr|ndt | |f S )z
    Converts the error type into a TypeScript type.
    inside_namespace should be set to the namespace that the reference
    occurs in, or None if this parameter is not relevant.
    z%s<%s>r    Error)fmt_type)	data_typeinside_namespaceZwrap_error_in r%   >/tmp/pip-unpacked-wheel-vqb3mfeu/stone/backends/tsd_helpers.pyfmt_error_type,   s    r'   c                 C   s   t | st| r4| j|kr | jS d| jj| jf S nbt| jd}t| rd|dt| j	| d 7 }n.t
| rt| jd}t| j|}d||f }|S dS )z
    Produces a TypeScript type name for the given data type.
    inside_namespace should be set to the namespace that the reference
    occurs in, or None if this parameter is not relevant.
    z%s.%sZObject<>r   z{[key: %s]: %s}N)r   r   	namespacename_base_type_tableget	__class__r   r"   r#   r   key_data_typefmt_type_namevalue_data_type)r#   r$   Z
fmted_typer/   r1   r%   r%   r&   r0   7   s    
r0   c                 C   s   t | |d S )z
    Produces a TypeScript type name for the meta-type that refers to the given
    struct, which belongs to an enumerated subtypes tree. This meta-type contains the
    .tag field that lets developers discriminate between subtypes.
    Z	Reference)r0   )r#   r$   r%   r%   r&   fmt_polymorphic_type_referenceL   s    
r2   c                 C   sh   t | rZ|  rZg }|  }|D ]\}}|t|| q |  rR|t| | t|S t| |S dS )z
    Returns a TypeScript type annotation for a data type.
    May contain a union of enumerated subtypes.
    inside_namespace should be set to the namespace that the type reference
    occurs in, or None if this parameter is not relevant.
    N)r   Zhas_enumerated_subtypesZget_all_subtypes_with_tagsappendr2   Zis_catch_all	fmt_unionr0   )r#   r$   Zpossible_typesZpossible_subtypes_subtyper%   r%   r&   r"   X   s    r"   c                 C   s   t | dkrd| S | d S )z2
    Returns a union type of the given types.
       |r   )lenjoin)Ztype_stringsr%   r%   r&   r4   j   s    r4   c                 C   s"   |dkrt | S t | d| S )Nr7   zV{})r   format)r+   versionr%   r%   r&   fmt_funcp   s    r=   c                 C   s   t | S Nr   )r+   r%   r%   r&   fmt_varv   s    r?   c                 C   s   |dkr.|}d|kr*| dk	r*| j d | }|S |dkrjd|krX|dd\}}t|}nd}t||d S |dkr|d	d\}}d
||f S |dkr|S |dkr|S td| dS )z.
    Processes a documentation reference.
    type.Nroute:r7   z()link z[%s]{@link %s}valfieldzUnknown doc ref tag %r)r+   splitintr=   rsplitRuntimeError)Zcur_namespacetagrF   Zfq_valr<   anchorrD   r%   r%   r&   fmt_tagy   s&    
rN   c                 C   sJ   i }| j D ]:}t|j|j}||kr<|| }td|||||< q
dS )z
    Check name conflicts among generated route definitions. Raise a runtime exception when a
    conflict is encountered.
    z.There is a name conflict between {!r} and {!r}N)Zroutesr=   r+   r<   rK   r;   )r*   Zroute_by_namerB   Z
route_nameZother_router%   r%   r&   check_route_name_conflict   s    

rO   c                 C   s<   |  }|sd S |D ]}| dj||jd q|   d S )NzIimport * as {namespace_name} from '{module_name_prefix}{namespace_name}';)module_name_prefixZnamespace_name)Zget_imported_namespacesemitr;   r+   )backendr*   rP   Zimported_namespacesnsr%   r%   r&   *generate_imports_for_referenced_namespaces   s    rT   c                 C   s   | j | j S r>   )
data_typesaliases)r*   r%   r%   r&   get_data_types_for_namespace   s    rW   )Nr    )N)N)N))
__future__r   r   r   r   Zstone.backendr   Zstone.ir.apir   Zstone.irr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zstone.backends.helpersr   r,   r'   r0   r2   r"   r4   r=   r?   rN   rO   rT   rW   r%   r%   r%   r&   <module>   sP   L            



