
    XfA                        d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 ddl
mZmZ dZddd'dZdddd(dZd)dZd Zd*dZd+d!Zd,d$Z G d% d&          ZdS )-    )annotationsN)Iterable)AnyOptionalCallable   )inspectto_builtins)schemaschema_componentsschema_hooktyper   r   *Optional[Callable[[type], dict[str, Any]]]returndict[str, Any]c               B    t          | f|          \  \  }}|r||d<   |S )a  Generate a JSON Schema for a given type.

    Any schemas for (potentially) shared components are extracted and stored in
    a top-level ``"$defs"`` field.

    If you want to generate schemas for multiple types, or to have more control
    over the generated schema you may want to use ``schema_components`` instead.

    Parameters
    ----------
    type : type
        The type to generate the schema for.
    schema_hook : callable, optional
        An optional callback to use for generating JSON schemas of custom
        types. Will be called with the custom type, and should return a dict
        representation of the JSON schema for that type.

    Returns
    -------
    schema : dict
        The generated JSON Schema.

    See Also
    --------
    schema_components
    r   z$defs)r   )r   r   out
componentss       H/var/www/piapp/venv/lib/python3.11/site-packages/msgspec/_json_schema.pyr   r      s5    : +D7LLLFSJ "!GJ    #/$defs/{name})r   ref_templatetypesIterable[Any]r   str1tuple[tuple[dict[str, Any], ...], dict[str, Any]]c                  t          j        |           }t          |          }t          |          t	          ||          t          fd|D                       }fd|                                D             }||fS )a  Generate JSON Schemas for one or more types.

    Any schemas for (potentially) shared components are extracted and returned
    in a separate ``components`` dict.

    Parameters
    ----------
    types : Iterable[type]
        An iterable of one or more types to generate schemas for.
    schema_hook : callable, optional
        An optional callback to use for generating JSON schemas of custom
        types. Will be called with the custom type, and should return a dict
        representation of the JSON schema for that type.
    ref_template : str, optional
        A template to use when generating ``"$ref"`` fields. This template is
        formatted with the type name as ``template.format(name=name)``. This
        can be useful if you intend to store the ``components`` mapping
        somewhere other than a top-level ``"$defs"`` field. For example, you
        might use ``ref_template="#/components/{name}"`` if generating an
        OpenAPI schema.

    Returns
    -------
    schemas : tuple[dict]
        A tuple of JSON Schemas, one for each type in ``types``.
    components : dict
        A mapping of name to schema for any shared components used by
        ``schemas``.

    See Also
    --------
    schema
    c              3  B   K   | ]}                     |          V  d S N	to_schema).0tgens     r   	<genexpr>z$schema_components.<locals>.<genexpr>_   s/      99CMM!$$999999r   c                P    i | ]"\  }}|                              |d           #S )Fr!   )r#   clsr$   r%   name_maps      r   
<dictcomp>z%schema_components.<locals>.<dictcomp>a   s>       393s}}Q..  r   )mimulti_type_info_collect_component_types_build_name_map_SchemaGeneratortupleitems)	r   r   r   
type_infoscomponent_typesschemasr   r%   r)   s	          @@r   r   r   0   s    N #E**J.z::O//H
8[,
?
?C9999j99999G    =L=R=R=T=T  J Jr   r2   Iterable[mi.Type]dict[Any, mi.Type]c                :    i fd| D ]} |           S )zFind all types in the type tree that are "nameable" and worthy of being
    extracted out into a shared top-level components mapping.

    Currently this looks for Struct, Dataclass, NamedTuple, TypedDict, and Enum
    types.
    c                   t          | t          j        t          j        t          j        t          j        f          r1| j        vr$| | j        <   | j        D ]} |j                   d S d S t          | t          j	                  r| | j        <   d S t          | t          j
                  r | j                   d S t          | t          j                  r | j                   d S t          | t          j                  r| j        D ]} |           d S t          | t          j                  r" | j                    | j                   d S t          | t          j                  r| j        D ]} |           d S d S r    )
isinstancer+   
StructTypeTypedDictTypeDataclassTypeNamedTupleTyper(   fieldsr   EnumTypeMetadataCollectionType	item_type	TupleType
item_typesDictTypekey_type
value_type	UnionTyper   )r$   fstcollectr   s      r   rK   z)_collect_component_types.<locals>.collectp   s   r/1A2CTU
 
 	 uJ&&$%
15! $ $AGAFOOOO '&$ $2;'' 	 !Jqu2;'' 	GAFOOOOO2,-- 
	GAK     2<(( 	l   2;'' 	GAJGAL!!!!!2<(( 	g  	 	 r    )r2   r$   rK   r   s     @@r   r-   r-   g   sO     J     0   



r   c                X    t          | t                    r| j        nt          |           S r    )r9   r   __name__repr)objs    r   
_type_reprrQ      s#    %c400?3<<d3ii?r   r(   c                    t          | d          r8| j        j        }d                    d | j        D                       }| d| dS | j        S )N
__origin__z, c              3  4   K   | ]}t          |          V  d S r    )rQ   )r#   as     r   r&   z"_get_class_name.<locals>.<genexpr>   s(      ==1A======r   [])hasattrrS   rN   join__args__)r(   nameargss      r   _get_class_namer]      s_    sL!! !~&yy=======      <r   r$   mi.Typec                   t          | d          sJ t          | j        d| j                  }t          |dd          }|sdS t          j        |                              d          }t          | t          j                  r	|dk    rdS nZt          | t          j	        t          j
        f          r4|                    |j         d          r|                    d          rdS |S )	Nr(   rS   __doc__ z
zAn enumeration.())rX   getattrr(   textwrapdedentstripr9   r+   r?   r=   r<   
startswithrN   endswith)r$   r(   docs      r   _get_docrk      s    1e
!%qu
-
-C
#y"
%
%C r
/#


$
$V
,
,C!R[!! ###2 $	A)2+;<	=	= >>S\,,,-- 	#,,s2C2C 	2Jr   r3   dict[Any, str]c                R   d fd}t                      }i }| D ]n} t          |                    }||v r8|                    |          }|                    |           || ||          <   ||v r|| ||          <   i|||<   od |                                D             S )zA mapping from nameable subcomponents to a generated name.

    The generated name is usually a normalized version of the class name. In
    the case of conflicts, the name will be expanded to also include the full
    import path.
    c                .    t          j        dd|           S )Nz[^a-zA-Z0-9.\-_]_)resubr[   s    r   	normalizez"_build_name_map.<locals>.normalize   s    v)3555r   c                8     | j          d| j                   S )N.)
__module____qualname__)r(   rs   s    r   fullnamez!_build_name_map.<locals>.fullname   s&    yCN??S-=??@@@r   c                    i | ]\  }}||	S rL   rL   )r#   kvs      r   r*   z#_build_name_map.<locals>.<dictcomp>   s    +++TQAq+++r   )setr]   popaddr1   )r3   rx   	conflictsnamesr(   r[   oldrs   s          @r   r.   r.      s    6 6 6A A A A A IE 	 	y--..5==))D//CMM$#&E((3-- 9#&E((3--  E$KK++U[[]]++++r   c                  &    e Zd Z	 	 ddd	ZdddZdS )r/   Nr   r)   rl   r   r   r   r   c                0    || _         || _        || _        d S r    )r)   r   r   )selfr)   r   r   s       r   __init__z_SchemaGenerator.__init__   s!     !&(r   Tr$   r^   	check_refboolr   r   c                z    i }t          |t          j                  r;t          j        ||j                  }|j        }t          |t          j                  ;|rQt          |d          rA j                            |j	                  x}r  j
                            |          |d<   |S t          |t          j        t          j        f          rngt          |t          j                  rd|d<   nFt          |t          j                  rd|d<   n%t          |t          j        t          j        f          rxt          |t          j                  rdnd|d<   |j        
|j        |d
<   |j        
|j        |d<   |j        
|j        |d<   |j        
|j        |d<   |j        
|j        |d<   
nt          |t          j                  r:d|d<   |j        
|j        |d<   |j        
|j        |d<   |j        
|j        |d<   
n3t          |t          j        t          j        t          j        f          r@d|d<   d|d<   |j        d|j        dz   dz  z  |d<   |j        d|j        dz   dz  z  |d<   	nt          |t          j                  rd|d<   |j        du rd|d<   	nt          |t          j                   r%d|d<   |j        du rd|d<   	nd|j        du rd|d<   	nTt          |t          j!                  rd|d<   d|d<   	n.t          |t          j"                  rd|d<   d|d<   	nt          |t          j#                  rd|d<   d |d<   nt          |t          j$                  rd|d<   d!|d<   nt          |t          j%                  red"|d<   t          |j&        t          j                  s '                    |j&                  |d#<   |j        
|j        |d$<   |j        
|j        |d%<   n=t          |t          j(                  rEd"|d<   tS          |j*                  x|d%<   |d$<   |j*        r fd&|j*        D             |d'<   d|d#<   nt          |t          j+                  rd(|d<   t          |j,        x}t          j                  r<i }|j        
|j        |d<   |j        
|j        |d<   |j        
|j        |d<   |r||d)<   t          |j-        t          j                  s '                    |j-                  |d*<   |j        
|j        |d+<   |j        
|j        |d,<   nt          |t          j.                  ri }g }d	}	|j/        D ]}
|
}t          |t          j                  r!|j        }t          |t          j                  !t          |t          j0                  r|j1        s|j2        }	|||j3        <   r|4                    |
            fd-|D             }tS          |          dk    r{ fd.|5                                D             } fd/|6                                D             |	|d0d1}|r|4                    |           ||d2<   n|7                    |           ntS          |          d3k    rF|8                                \  }}
|4                     '                    |
                     ||d2<   n]||d2<   nVt          |t          j9                  rtu          |j6                  |d4<   n#t          |t          j;                  rj|<                    d5|j	        j=                   t}          |          x}r|<                    d6|           tu          d7 |j	        D                       |d4<   nt          |t          j0                  r2|<                    d5t          |j	                             t}          |          x}r|<                    d6|           g }g }g }|j2        Q|4                    |j2                   |4                    |j2                   |4                    d4|j3        gi           |j@        D ]} '                    |j                  }|jA        r|4                    |jB                   ne|jC        t          jD        urt          |jC        d8          |d9<   n8|jF        t          t          t          t          fv r|F                                |d9<   |4                    |jB                   |4                    |           |j1        rld:}t          t          |j@                            D ]\  }}|jA        r nd"|d<   ||d'<   tS          |          |z
  |d%<   |jM        rtS          |          |d$<   nd(|d<   t          t          ||                    |d;<   ||d<<   |jM        rd|d*<   nRt          |t          jO        t          jP        t          jQ        f          rj|<                    d5t          |j	                             t}          |          x}r|<                    d6|           g }g }g }|j@        D ]} '                    |j                  }|jA        r|4                    |jB                   n,|jC        t          jD        urt          |jC        d8          |d9<   |4                    |jB                   |4                    |           t          |t          jQ                  r/d"|d<   ||d'<   tS          |          |d%<   tS          |          |d$<   nd(|d<   t          t          ||                    |d;<   ||d<<   nt          |t          jR                  rt          d=          t          |t          jT                  ra jU        r?	 t          j         U                    |j	                  |          }n# t          $ r Y nw xY w|st          d>|j	        d?          nt          d@|          |S )Az!Converts a Type to a json-schema.r(   rr   z$refnullr   booleanintegernumberNminimumexclusiveMinimummaximumexclusiveMaximum
multipleOfstring	maxLength	minLengthpatternbase64contentEncoding         Tz	date-timeformattimeFzpartial-timedatedurationuuiddecimalarrayr1   maxItemsminItemsc                :    g | ]}                     |          S rL   r!   )r#   ir   s     r   
<listcomp>z._SchemaGenerator.to_schema.<locals>.<listcomp>#  s%    (Q(Q(Qq):):(Q(Q(Qr   prefixItemsobjectpropertyNamesadditionalPropertiesmaxPropertiesminPropertiesc                :    g | ]}                     |          S rL   r!   )r#   rU   r   s     r   r   z._SchemaGenerator.to_schema.<locals>.<listcomp>F  s%    888Qt~~a((888r   c                n    i | ]1\  }}|j                             j        |j                            2S )rr   )r   r   r)   r(   )r#   rz   r{   r   s      r   r*   z._SchemaGenerator.to_schema.<locals>.<dictcomp>I  sL       1 t(//T]155I/JJ  r   c                :    g | ]}                     |          S rL   r!   )r#   r{   r   s     r   r   z._SchemaGenerator.to_schema.<locals>.<listcomp>N  s%    JJJAdnnQ//JJJr   )propertyNamemapping)anyOfdiscriminatorr   r   enumtitledescriptionc              3  $   K   | ]}|j         V  d S r    )value)r#   es     r   r&   z-_SchemaGenerator.to_schema.<locals>.<genexpr>b  s$      #;#;AG#;#;#;#;#;#;r   )str_keysdefaultr   
propertiesrequiredz-json-schema doesn't support msgpack Ext typeszGenerating JSON schema for custom types requires either:
- specifying a `schema_hook`
- annotating the type with `Meta(extra_json_schema=...)`

type z is not supportedz!json-schema doesn't support type )Wr9   r+   r@   _merge_jsonextra_json_schemar   rX   r)   getr(   r   r   AnyTypeRawTypeNoneTypeBoolTypeIntType	FloatTypegegtleltmultiple_ofStrType
max_length
min_lengthr   	BytesTypeByteArrayTypeMemoryViewTypeDateTimeTypetzTimeTypeDateTypeTimeDeltaTypeUUIDTypeDecimalTyperA   rB   r"   rC   lenrD   rE   rF   rG   rH   r   r:   
array_like	tag_fieldtagappendr1   valuesupdatepopitemLiteralTypesortedr?   
setdefaultrN   rk   r]   r>   r   encode_namer   	NODEFAULTr
   default_factorylistdictr|   	bytearray	enumeratereversedforbid_unknown_fieldszipr;   r<   r=   ExtType	TypeError
CustomTyper   NotImplementedError)r   r$   r   r   r[   rF   property_namesstructsotherr   subtype	real_typeoptionsr   struct_schemaro   rj   r   r   r>   fieldfield_scheman_trailing_defaultsrI   s   `                       r   r"   z_SchemaGenerator.to_schema   s   !#BK(( 	^FA,?@@FA BK(( 	  	E** 	}((///t !%!2!9!9t!9!D!Dva"*bj122 U	G2;'' S	G#F6NN2;'' Q	G&F6NNBJ566 O	G*4Q
*C*CQYYF6Nt$%Dy!t-.T)*t$%Dy!t-.T)*}('(}|$2:&& C	G%F6N|'&'l{#|'&'l{#y$$%Iy!BL"*:B<MNOO {	G%F6N(0F$%|'&'AL1,<+B&C{#|'&'AL1,<+B&C{#2?++ t	G%F6Ntt||#.x 2;'' p	G%F6Ntt||#)x  #1x 2;'' j	G%F6N%F82+,, g	G%F6N)F82;'' d	G%F6N%F82>** a	G%F6N(F82,-- ^	G$F6Nak2:66 >"&.."="=w|'%&\z"|'%&\z"2<(( V	G$F6N69!,6G6GGF:
!3| ((Q(Q(Q(QAL(Q(Q(Q}%"'w2;'' P	G%F6Naj0("*== 	=13&22:2EN;/&22:2EN;/#/080@N9-! =.<F?+alBJ77 N151M1M-.|'*+,'|'*+,'2<(( }	GGEI7 * *#	 BK88 / )I !BK88 /i77 *	@T * ) 3I-6GIM**LL))))8888%888G7||q      '  
 KJJJ9I9IJJJ6?G%T%T! !  1NN=111&-F7OOMM-0000W""$__..
7t~~g66777")w")w2>** Y	G#AH--F6NN2;'' W	Ggqu~666qkk!s 6!!-555##;#;QU#;#;#;;;F6NN2=)) R	Ggqu'='=>>>qkk!s 6!!-555HEF{&,,,Q[)))vw/000 	, 	,#~~ej99> FOOE$56666]",66.9%-RV.W.W.WL++*tT3	.JJJ.3.C.C.E.EL+U.///l++++| ;&'#.78J8J.K.K  *'z !(v(.}%%([[3F%Fz"* 5),VF:&!)v'+Cv,>,>'?'?|$%-z"* ;5:F12B,b.>@QRSS *	Ggqu'='=>>>qkk!s 6!!-555EFH , ,#~~ej99> XOOE$56666]",66.9%-RV.W.W.WL+U.///l++++!R.// .!(v(.}%%(]]z"%([[z""!)v'+Cv,>,>'?'?|$%-z""2:&& 	GKLLL2=)) 	G ^D,<,<QU,C,CVLLFF*   D 7 E	7 7 7   EEEFFFs   -o> >
p
p)Nr   )r)   rl   r   r   r   r   )T)r$   r^   r   r   r   r   )rN   rv   rw   r   r"   rL   r   r   r/   r/      sW         CG,	) ) ) ) )d d d d d d dr   r/   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r2   r5   r   r6   )r(   r   r   r   )r$   r^   r   r   )r3   r6   r   rl   )
__future__r   rp   re   collections.abcr   typingr   r   r   ra   r	   r+   r
   __all__r   r   r-   rQ   r]   rk   r.   r/   rL   r   r   <module>r      sc   " " " " " " 				  $ $ $ $ $ $ * * * * * * * * * * ( ( ( ( ( ( ( (
) MQ           L ?C(	4 4 4 4 4 4n$ $ $ $N@ @ @       , , , ,<o o o o o o o o o or   