o
    ;hA                     @  s   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dZdddd/ddZd0ddZdd  Zd1d"d#Zd2d&d'Zd3d*d+ZG d,d- d-ZdS )4    )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                C  s$   t | f|d\\}}|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
components r   G/var/www/html/venv/lib/python3.10/site-packages/msgspec/_json_schema.pyr
      s   r
   #/$defs/{name})r   ref_templatetypesIterable[Any]r   str1tuple[tuple[dict[str, Any], ...], dict[str, Any]]c                  s\   t | }t|}t|t|| t fdd|D } fd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  s    | ]}  |V  qd S N	to_schema).0t)genr   r   	<genexpr>_   s    z$schema_components.<locals>.<genexpr>c                   s"   i | ]\}}|   |d qS )Fr   )r   clsr    r!   name_mapr   r   
<dictcomp>a   s    z%schema_components.<locals>.<dictcomp>)miZmulti_type_info_collect_component_types_build_name_map_SchemaGeneratortupleitems)r   r   r   
type_infoscomponent_typesZschemasr   r   r$   r   r   0   s   
'r   r-   Iterable[mi.Type]dict[Any, mi.Type]c                   s(   i  fdd | D ]} | q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                   s
  t | tjtjtjtjfr&| jvr"| | j< | jD ]	} |j qd S d S t | tj	r3| | j< d S t | tj
r@ | j d S t | tjrM | j d S t | tjr_| jD ]} | qVd S t | tjrq | j  | j d S t | tjr| jD ]} | qzd 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   r   r   rD   p   s6   







z)_collect_component_types.<locals>.collectr   )r-   r    r   rC   r   r(   g   s
   
r(   c                 C  s   t | tr| jS t| S r   )r1   r   __name__repr)objr   r   r   
_type_repr   s   rH   r#   c                 C  s>   t | dr| jj}ddd | jD }| d| dS | jS )N
__origin__z, c                 s  s    | ]}t |V  qd S r   )rH   r   ar   r   r   r"      s    z"_get_class_name.<locals>.<genexpr>[])hasattrrI   rE   join__args__)r#   nameargsr   r   r   _get_class_name   s
   
rS   r    mi.Typec                 C  s   t | dsJ t| jd| j}t|dd}|sdS t|d}t| tjr/|dkr-dS |S t| tj	tj
frH||j drH|drHdS |S )	Nr#   rI   __doc__ z
zAn enumeration.())rN   getattrr#   textwrapdedentstripr1   r'   r7   r5   r4   
startswithrE   endswith)r    r#   docr   r   r   _get_doc   s   r`   r.   dict[Any, str]c                   s   dd   fdd}t  }i }| D ]+} t|}||v r-||}|| ||||< ||v r8||||< q|||< qdd | 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                 S  s   t dd| S )Nz[^a-zA-Z0-9.\-_]_)resubrQ   r   r   r   	normalize   s   z"_build_name_map.<locals>.normalizec                   s    | j  d| j S )N.)
__module____qualname__)r#   rf   r   r   fullname   s   z!_build_name_map.<locals>.fullnamec                 S  s   i | ]\}}||qS r   r   r   kvr   r   r   r&          z#_build_name_map.<locals>.<dictcomp>)setrS   popaddr,   )r.   rk   	conflictsnamesr#   rQ   oldr   rj   r   r)      s   


r)   c                   @  s(   e Zd Z		ddd	d
ZddddZdS )r*   Nr   r%   ra   r   r   r   r   c                 C  s   || _ || _|| _d S r   )r%   r   r   )selfr%   r   r   r   r   r   __init__   s   
z_SchemaGenerator.__init__Tr    rT   	check_refboolr   r   c                   s~	  i }t |tjrt||j}|j}t |tjs|r3t|dr3 j|j	 }r3 j
j|d|d< |S t |tjtjfr?	 |S t |tjrKd|d< |S t |tjrWd|d< |S t |tjtjfrt |tjrhdnd|d< |jd	urv|j|d
< |jd	ur|j|d< |jd	ur|j|d< |jd	ur|j|d< |jd	ur|j|d< |S t |tjrd|d< |jd	ur|j|d< |jd	ur|j|d< |jd	ur|j|d< |S t |tjtjtjfrd|d< d|d< |jd	urd|jd d  |d< |jd	urd|jd d  |d< |S t |tjrd|d< |jdu rd|d< |S t |tj r9d|d< |jdu r-d|d< |S |jdu r7d|d< |S t |tj!rJd|d< d|d< |S t |tj"r[d|d< d|d< |S t |tj#rld|d< d |d< |S t |tj$r}d|d< d!|d< |S t |tj%rd"|d< t |j&tjs '|j&|d#< |jd	ur|j|d$< |jd	ur|j|d%< |S t |tj(rd"|d< t)|j* |d%< |d$< |j*rڇ fd&d'|j*D |d(< d|d#< |S t |tj+rCd)|d< t |j, }tjri }|jd	ur|j|d< |jd	ur	|j|d< |jd	ur|j|d< |r||d*< t |j-tjs+ '|j-|d+< |jd	ur6|j|d,< |jd	urA|j|d-< |S t |tj.ri }g }d	}	|j/D ]0}
|
}t |tjrh|j}t |tjs^t |tj0r}|j1s}|j2}	|||j3< qS|4|
 qS fd.d'|D }t)|dkrŇ fd/d0|5 D } fd1d'|6 D |	|d2d3}|r|4| ||d4< |S |7| |S t)|d5kr|8 \}}
|4 '|
 ||d4< |S ||d4< |S t |tj9rt:|j6|d6< |S t |tj;r |<d7|j	j= t>| }r|<d8| t:d9d: |j	D |d6< |S t |tj0r|<d7t?|j	 t>| }r=|<d8| g }g }g }|j2d	ur^|4|j2 |4|j2 |4d6|j3gi |j@D ]@} '|j}|jArt|4|jB n!|jCtjDurtE|jCdd;|d<< n|jFtGtHtItJfv r|F |d<< |4|jB |4| qa|j1rd=}tKtL|j@D ]\}}|jAr nqd"|d< ||d(< t)|| |d%< |jMrt)||d$< |S d)|d< tHtN|||d>< ||d?< |jMrd|d+< |S t |tjOtjPtjQfr}|<d7t?|j	 t>| }r|<d8| g }g }g }|j@D ]/} '|j}|jAr0|4|jB n|jCtjDur@tE|jCdd;|d<< |4|jB |4| qt |tjQrjd"|d< ||d(< t)||d%< t)||d$< |S d)|d< tHtN|||d>< ||d?< |S t |tjRrtSd@t |tjTr jUrzt U|j	|}W n
 tVy   Y nw |stSdA|j	dB|S tSdC|)Dz!Converts a Type to a json-schema.r#   re   z$refnullr   booleanintegernumberNminimumZexclusiveMinimummaximumZexclusiveMaximumZ
multipleOfstringZ	maxLengthZ	minLengthpatternbase64ZcontentEncoding         Tz	date-timeformattimeFzpartial-timedatedurationuuiddecimalarrayr,   ZmaxItemsZminItemsc                      g | ]}  |qS r   r   )r   irv   r   r   
<listcomp>#  ro   z._SchemaGenerator.to_schema.<locals>.<listcomp>ZprefixItemsobjectZpropertyNamesZadditionalPropertiesZmaxPropertiesZminPropertiesc                   r   r   r   rJ   r   r   r   r   F  ro   c                   s(   i | ]\}}| j j j|j d qS )re   )r   r   r%   r#   rl   r   r   r   r&   I  s    z._SchemaGenerator.to_schema.<locals>.<dictcomp>c                   r   r   r   )r   rn   r   r   r   r   N  ro   )ZpropertyNamemapping)anyOfZdiscriminatorr   r   enumtitledescriptionc                 s  s    | ]}|j V  qd S r   )value)r   er   r   r   r"   b  s    z-_SchemaGenerator.to_schema.<locals>.<genexpr>)Z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 )Wr1   r'   r8   Z_merge_jsonZextra_json_schemar   rN   r%   getr#   r   r   ZAnyTypeZRawTypeNoneTypeZBoolTypeZIntTypeZ	FloatTypegegtleltZmultiple_ofZStrType
max_lengthZ
min_lengthr   Z	BytesTypeZByteArrayTypeZMemoryViewTypeZDateTimeTypetzZTimeTypeZDateTypeZTimeDeltaTypeZUUIDTypeZDecimalTyper9   r:   r   r;   lenr<   r=   r>   r?   r@   r   r2   Z
array_like	tag_fieldtagappendr,   valuesupdatepopitemZLiteralTypesortedr7   
setdefaultrE   r`   rS   r6   r   Zencode_namer   Z	NODEFAULTr	   default_factorylistdictrp   	bytearray	enumeratereversedZforbid_unknown_fieldszipr3   r4   r5   ExtType	TypeErrorZ
CustomTyper   NotImplementedError)rv   r    rx   r
   rQ   r>   Zproperty_namesstructsotherr   subtypeZ	real_typeoptionsr   Zstruct_schemarb   r_   r   rt   r6   fieldZfield_schemaZn_trailing_defaultsrA   r   r   r   r      s   W  U  S 









 G 





 ? 

 8  4  0  .  +  (  %  " 

    




 


d
b^\ZU
3-
z_SchemaGenerator.to_schema)Nr   )r%   ra   r   r   r   r   )T)r    rT   rx   ry   r   r   )rE   rh   ri   rw   r   r   r   r   r   r*      s
    
r*   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r-   r/   r   r0   )r#   r   r   r   )r    rT   r   r   )r.   r0   r   ra   )
__future__r   rc   rZ   collections.abcr   typingr   r   r   rV   r   r'   r	   __all__r
   r   r(   rH   rS   r`   r)   r*   r   r   r   r   <module>   s$    &
7'


