o
    6dz*                     @   s  d dl mZmZmZmZ d dlZd dlmZmZm	Z	 d dl
mZmZmZmZ d dlmZmZmZmZmZ dZer<d dlZd dlZeedZejdd	d
Zejdddedd ejdddedd ejdddedd ejdddedd ejdddedd G dd deZdS )    )absolute_importdivisionprint_functionunicode_literalsN)is_struct_typeis_union_typeis_void_type)basefmt_serial_typeSwiftBaseBackendundocumented)check_route_name_conflict	fmt_classfmt_funcfmt_varfmt_typeFargparsezswift-client-backendzGenerates a Swift class with an object for each namespace, and in each namespace object, a method for each route. This class assumes that the swift_types backend was used with the same output directory.)progdescriptionz-mz--module-nameTzSThe name of the Swift module to generate. Please exclude the .swift file extension.)requiredtypehelpz-cz--class-namez~The name of the Swift class that contains an object for each namespace, and in each namespace object, a method for each route.z-tz--transport-client-namez;The name of the Swift class that manages network API calls.z-yz--client-argszFThe client-side route arguments to append to each route by style type.z-zz--style-to-requestz?The dict that maps a style type to a Swift request object name.c                   @   sN   e Zd ZdZe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 )SwiftBackendaE  
    Generates Swift client base that implements route interfaces.

    Examples:

    ```
    open class ExampleClientBase {
        /// Routes within the namespace1 namespace. See Namespace1 for details.
        open var namespace1: Namespace1!
        /// Routes within the namespace2 namespace. See Namespace2 for details.
        open var namespace2: Namespace2!

        public init(client: ExampleTransportClient) {
            self.namespace1 = Namespace1(client: client)
            self.namespace2 = Namespace2(client: client)
        }
    }
    ```

    Here, `ExampleTransportClient` would contain the implementation of a handwritten,
    project-specific networking client. Additionally, the `Namespace1` object would
    have as its methods all routes in the `Namespace1` namespace. A hypothetical 'copy'
    enpoding might be implemented like:

    ```
    open func copy(fromPath fromPath: String, toPath: String) ->
                     ExampleRequestType<Namespace1.CopySerializer, Namespace1.CopyErrorSerializer> {
        let route = Namespace1.copy
        let serverArgs = Namespace1.CopyArg(fromPath: fromPath, toPath: toPath)
        return client.request(route, serverArgs: serverArgs)
    }
    ```

    Here, ExampleRequestType is a project-specific request type, parameterized by response and
    error serializers.
    c              	   C   s   |j  D ]'}t|j}|jr,| d| | | W d    n1 s'w   Y  q| d| jj	 | 
| W d    d S 1 sHw   Y  d S )Nz{}Routes.swiftz{}.swift)
namespacesvaluesr   nameroutesZoutput_to_relative_pathformat_generate_routesargsmodule_name_generate_client)selfapi	namespacens_class r&   rC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\stone/backends/swift_client.pygenerate{   s   
"zSwiftBackend.generatec              	   C   s4  |  t | d |   | d| jjx g }|j D ]}|j	r0|
|jt|jf q |D ]\}}| d|| | d|| q3|   | jd| dd| jjfgd |D ]\}}| d	|| qcW d    n1 s{w   Y  W d    d S W d    d S 1 sw   Y  d S )
Nzimport Alamofirezopen class {}z=/// Routes within the {} namespace. See {}Routes for details.zopen var {}: {}Routes!zpublic initclient{})r   z"self.{} = {}Routes(client: client))emit_rawr	   emitblockr   r   
class_namer   r   r   appendr   r   function_block
_func_argstransport_client_name)r"   r#   Znamespace_fieldsr$   vartypr&   r&   r'   r!      s6   



"zSwiftBackend._generate_clientc              	   C   s   t | t|j}| t | d|j | d|K | d| jj	 dd| jj	fg}| 
d| | | d W d    n1 sMw   Y  |   |jD ]}| || qYW d    d S 1 smw   Y  d S )Nz/// Routes for the {} namespacezopen class {}Routeszpublic let client: {}r)   r*   initzself.client = client)r   r   r   r+   r	   r,   r   r-   r   r2   r0   r1   r   _generate_route)r"   r$   r%   r   router&   r&   r'   r      s   


"zSwiftBackend._generate_routesc                    s   |j }t|}t|r  j||d} fdd|jD }||fS t|rSt|jdt	|jt	|jfg}t|j|j
rE |j
 jndt	|jfg}||fS t|rYg nd|fg}g }||fS )N)r$   c                    s6   g | ]}|j rt|j|j r |j  jntfqS r&   )docr   r   process_doc_docfr   ).0fr"   r&   r'   
<listcomp>   s    
z0SwiftBackend._get_route_args.<locals>.<listcomp>{}.{}zThe {} unionrequest)arg_data_typer   r   _struct_init_argsfieldsr   r   r   r   r   r8   r9   r:   r   )r"   r$   r7   Z	data_typearg_typearg_listdoc_listr&   r=   r'   _get_route_args   s*   
zSwiftBackend._get_route_argsNc              	   C   s  |  ||\}}|pg }|pg }t|j}t|j|j}	|jr(| |j| j}
nd	|	}
| j
|
ddd | d || D ]\}}d	||d urK|nt}| j
|ddd q>| d d}|	t|jt|j}| j
|ddd dd		t|j|	fg}g }d
g}|D ]\}}}|||f |||f |||f qt|j}t|j}| | | jd	|	| j|ddd	|||do | d	t|j|	 t|jrdd | |jD }|dd	|| |fg7 }| d	|| | nt|jr| d	t|jj t|js|dg7 }||7 }d	| j|dd}| | W d    n	1 s7w   Y  |   d S )NzThe {} routez/// x   )prefixwidthz///z- parameter {}: {}zx - returns: Through the response callback, the caller will receive a `{}` object on success or a `{}` object on failure.r7   r?   )r7   r7   z@discardableResult open func {}F)Zforce_firstz
{}<{}, {}>)r   return_typezlet route = {}.{}c                 S   s   g | ]\}}||fqS r&   r&   )r;   r   _r&   r&   r'   r>      s    z,SwiftBackend._emit_route.<locals>.<listcomp>
serverArgsz{}({})zlet serverArgs = {}({})zlet serverArgs = {})rM   rM   zreturn client.request({})T)Znot_init)rG   r   rA   r   r   versionr8   r9   r:   r   Zemit_wrapped_textr,   r   Zresult_data_typeZerror_data_typer   r/   r
   #_maybe_generate_deprecation_warningr0   r1   r   rB   r   r   r   )r"   r$   r7   req_obj_name
extra_args
extra_docsrE   rF   rD   	func_nameZ	route_docr   r8   Z	param_docoutputZ	func_argsclient_argsZreturn_argsvaluer4   Zrtypeetyper   txtr&   r&   r'   _emit_route   sd   








zSwiftBackend._emit_routec                 C   sZ   |j r+dt|j|j}|j jr!|dt|j jj|j jj7 }| d| d S d S )Nz{} is deprecated.z Use {}.z(@available(*, unavailable, message:"{}"))
deprecatedr   r   r   rN   Zbyr,   )r"   r7   msgr&   r&   r'   rO     s   z0SwiftBackend._maybe_generate_deprecation_warningc                 C   s   |j d}t| jj}t| jj}|| vr%| ||||  d S || D ]#}t	|\}}|| }	dd |D }
dd |D }| |||	|
| q)d S )Nstylec                 S   s   g | ]
}t |d d qS )N)tupler;   Z	type_datar&   r&   r'   r>         z0SwiftBackend._generate_route.<locals>.<listcomp>c                 S   s   g | ]
}|d  |d fqS )r   r]   r&   r_   r&   r&   r'   r>     r`   )
attrsgetjsonloadsr   rU   style_to_requestkeysrY   r^   )r"   r$   r7   Z
route_typerU   re   Z	args_dataZreq_obj_keyZtype_data_listrP   rQ   rR   r&   r&   r'   r6   	  s   zSwiftBackend._generate_route)NN)__name__
__module____qualname____doc___cmdline_parserZcmdline_parserr(   r!   r   rG   rY   rO   r6   r&   r&   r&   r'   r   S   s    %

@r   )
__future__r   r   r   r   rc   Zstone.irr   r   r   Zstone.backends.swiftr	   r
   r   r   Zstone.backends.swift_helpersr   r   r   r   r   Z_MYPYtyping	importlibimport_modulestrr   ArgumentParserrk   add_argumentr   r&   r&   r&   r'   <module>   sd    	