
    E6i&                         d dl mZ d dlmZ d dlmZmZmZ ddlm	Z	m
Z ddlmZ d Zdd	Zdd
Z G d de      Z G d de      Zy)    OrderedDictwraps)requestcurrent_apphas_app_context   )Maskapply)unpackc                 4    t        | t              r |        S | S N)
isinstancetype)clss    Z/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/flask_restx/marshalling.pymaker   
   s    #tuJ    Nc                 `   t        | ||||      \  }}|rddlm} g g }	|j                         D ]<  \  }
}|
}t	        |t
              rt        | ||      }nt        |      }t	        ||      }|r2|j                          |	r |xj                  t        |	      z  c_	        g }	|j                  |
| |      }|rwfd}|j                  xs |
} |||       	 |j                  |
| |      }|(||j                  j                  |j                        k(  rn|j                  } |||       U|	j!                  |       r||t#               k(  s|i k(  r*j!                  ||f       ? t%              |rt#              n
t              }|r|rt#        ||fg      n||i}|S |S )  Takes raw data (in the form of a dict, list, object) and a dict of
    fields to output and filters the data based on those fields.

    :param data: the actual object(s) from which the fields are taken from
    :param fields: a dict of whose keys will make up the final serialized
                   response output
    :param envelope: optional key that will be used to envelop the serialized
                     response
    :param bool skip_none: optional key will be used to eliminate fields
                           which value is None or the field's key not
                           exist in data
    :param bool ordered: Wether or not to preserve order


    >>> from flask_restx import fields, marshal
    >>> data = { 'a': 100, 'b': 'foo', 'c': None }
    >>> mfields = { 'a': fields.Raw, 'c': fields.Raw, 'd': fields.Raw }

    >>> marshal(data, mfields)
    {'a': 100, 'c': None, 'd': None}

    >>> marshal(data, mfields, envelope='data')
    {'data': {'a': 100, 'c': None, 'd': None}}

    >>> marshal(data, mfields, skip_none=True)
    {'a': 100}

    >>> marshal(data, mfields, ordered=True)
    OrderedDict([('a', 100), ('c', None), ('d', None)])

    >>> marshal(data, mfields, envelope='data', ordered=True)
    OrderedDict([('data', OrderedDict([('a', 100), ('c', None), ('d', None)]))])

    >>> marshal(data, mfields, skip_none=True, ordered=True)
    OrderedDict([('a', 100)])

    r
   Wildcard	skip_noneorderedr   c                 Z    r||t               k(  s|i k(  ry j                  | |f       y r   )r   append)kvitemsr   s     r   _appendzmarshal.<locals>._appendN   s-    $!)qKM7IQRTW"aV,r   )_marshalfieldsr   r"   r   dictmarshalr   resetexcludesetoutputkey	containerformatdefaultr   r   tuple)datar%   enveloper   maskr   outhas_wildcardsr   keysdkeyvalr,   valuefieldis_wildcardr#   r"   s      `             @r   r'   r'      s   L "$)T7SC$ $	'ID#C#t$cYPS	(9KKMT2!T4A-
  ))+tCC' %T4 I =EU__5K5K!MM6 - "#iiU+  KKemu/ERTLL#u&I$	'L e$+k% e4;+#/0(CC
Jr   c           	         	
 ddl m	 |xs t        |dd      }t        |d|      }|rt        ||d      }t	         t
        t        f      r6 D cg c]  }t        ||       }}|rrt        ||fg      n||i}|d	fS d
d	i	 fd

 fd|j                         D        }r	d |D        }rt        |      n
t        |      }|rrt        ||fg      n||i}|d
   fS c c}w )r   r
   r   __mask__NresolvedTskipr   Fpresentc                 l    t        |      }t        |      rdd<   |j                  |       }| |fS )NTrA   r   )r   r   r+   )r,   r8   r:   r9   r   r1   r5   r   s       r   __format_fieldz _marshal.<locals>.__format_field   s>    S	eX&'+M)$S$8U|r   c              3   x   K   | ]1  \  }}t        |t              r|t        |       fn ||       3 yw)r   N)r   r&   r'   ).0r    r!   rC   r1   r   r   s      r   	<genexpr>z_marshal.<locals>.<genexpr>   sJ       Aq a 
GD!y'BCAq!	"s   7:c              3   X   K   | ]"  \  }}|	|t               k7  s|i k7  s||f $ y wr   r   )rE   r    r!   s      r   rF   z_marshal.<locals>.<genexpr>   s3      
q!!{}:LQRVXQXQF
s   
***	*)r%   r   getattr
apply_maskr   listr0   r'   r   r"   r&   )r1   r%   r2   r   r3   r   dr4   r"   r   rC   r5   s   `  ` `   @@@r   r$   r$   p   s   N !476:t4DVZ0FFDt4$u&QUVAwq&IwGVV4;+#/0(CCEz&M LLN	E 
$
 !(+e
T%[C07kHc?+,h_i(((? Ws   C/c                        e Zd ZdZ	 ddZd Zy)marshal_witha+  A decorator that apply marshalling to the return values of your methods.

    >>> from flask_restx import fields, marshal_with
    >>> mfields = { 'a': fields.Raw }
    >>> @marshal_with(mfields)
    ... def get():
    ...     return { 'a': 100, 'b': 'foo' }
    ...
    ...
    >>> get()
    OrderedDict([('a', 100)])

    >>> @marshal_with(mfields, envelope='data')
    ... def get():
    ...     return { 'a': 100, 'b': 'foo' }
    ...
    ...
    >>> get()
    OrderedDict([('data', OrderedDict([('a', 100)]))])

    >>> mfields = { 'a': fields.Raw, 'c': fields.Raw, 'd': fields.Raw }
    >>> @marshal_with(mfields, skip_none=True)
    ... def get():
    ...     return { 'a': 100, 'b': 'foo', 'c': None }
    ...
    ...
    >>> get()
    OrderedDict([('a', 100)])

    see :meth:`flask_restx.marshal`
    Nc                 `    || _         || _        || _        || _        t	        |d      | _        y)z
        :param fields: a dict of whose keys will make up the final
                       serialized response output
        :param envelope: optional key that will be used to envelop the serialized
                         response
        Tr?   N)r%   r2   r   r   r   r3   )selfr%   r2   r   r3   r   s         r   __init__zmarshal_with.__init__   s.      "D)	r   c                 2     t               fd       }|S )Nc                      | i |}	j                   }t               r6t        j                  d   }t        j
                  j                  |      xs |}t        |t              rJt        |      \  }}}t        |	j                  	j                  	j                  |	j                        ||fS t        |	j                  	j                  	j                  |	j                        S )NRESTX_MASK_HEADER)r3   r	   r   configr   headersgetr   r0   r   r'   r%   r2   r   r   )
argskwargsrespr3   mask_headerr1   coderU   frO   s
           r   wrapperz&marshal_with.__call__.<locals>.wrapper   s    d%f%D99D )001DE**;7?4$&&,Tl#dG   $++t}}dnndDLL r   r   rO   r\   r]   s   `` r   __call__zmarshal_with.__call__   s     	q	 
	2 r   NFNF__name__
__module____qualname____doc__rP   r_    r   r   rM   rM      s    B JO*r   rM   c                       e Zd ZdZd Zd Zy)marshal_with_fieldaP  
    A decorator that formats the return values of your methods with a single field.

    >>> from flask_restx import marshal_with_field, fields
    >>> @marshal_with_field(fields.List(fields.Integer))
    ... def get():
    ...     return ['1', 2, 3.0]
    ...
    >>> get()
    [1, 2, 3]

    see :meth:`flask_restx.marshal_with`
    c                 L    t        |t              r |       | _        y|| _        y)zP
        :param field: a single field with which to marshal the output.
        N)r   r   r:   )rO   r:   s     r   rP   zmarshal_with_field.__init__  s     eT"DJDJr   c                 2     t               fd       }|S )Nc                       | i |}t        |t              r-t        |      \  }}}j                  j	                  |      ||fS j                  j	                  |      S r   )r   r0   r   r:   r.   )rW   rX   rY   r1   r[   rU   r\   rO   s         r   r]   z,marshal_with_field.__call__.<locals>.wrapper(  s]    d%f%D$&&,Tl#dGzz((.g==::$$T**r   r   r^   s   `` r   r_   zmarshal_with_field.__call__'  s     	q	+ 
	+ r   Nra   rf   r   r   rh   rh     s    
r   rh   r`   )collectionsr   	functoolsr   flaskr   r   r	   r3   r   r   rI   utilsr   r   r'   r$   objectrM   rh   rf   r   r   <module>rq      sE    #  7 7 + ]@N)bK6 K\" "r   