o
    >Di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   M/var/www/Datamplify/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>)mimulti_type_info_collect_component_types_build_name_map_SchemaGeneratortupleitems)r   r   r   
type_infoscomponent_types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   rF   p   s6   







z)_collect_component_types.<locals>.collectr   )r.   r    r   rE   r   r)   g   s
   
r)   c                 C  s   t | tr| jS t| S r   )r3   r   __name__repr)objr   r   r   
_type_repr   s   rJ   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   )rJ   r   ar   r   r   r"      s    z"_get_class_name.<locals>.<genexpr>[])hasattrrK   rG   join__args__)r#   nameargsr   r   r   _get_class_name   s
   
rU   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#   rK   __doc__ z
zAn enumeration.())rP   getattrr#   textwrapdedentstripr3   r'   r9   r7   r6   
startswithrG   endswith)r    r#   docr   r   r   _get_doc   s   rb   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rS   r   r   r   	normalize   s   z"_build_name_map.<locals>.normalizec                   s    | j  d| j S )N.)
__module____qualname__)r#   rh   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>)setrU   popaddr-   )r/   rm   	conflictsnamesr#   rS   oldr   rl   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%   rc   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    rV   	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#   rg   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arrayr-   maxItemsminItemsc                      g | ]}  |qS r   r   )r   irx   r   r   
<listcomp>#  rq   z._SchemaGenerator.to_schema.<locals>.<listcomp>prefixItemsobjectpropertyNamesadditionalPropertiesmaxPropertiesminPropertiesc                   r   r   r   rL   r   r   r   r   F  rq   c                   s(   i | ]\}}| j j j|j d qS )rg   )r   r   r%   r#   rn   r   r   r   r&   I  s    z._SchemaGenerator.to_schema.<locals>.<dictcomp>c                   r   r   r   )r   rp   r   r   r   r   N  rq   )propertyNamemapping)anyOf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>)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 )Wr3   r'   r:   _merge_jsonextra_json_schemar   rP   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DecimalTyper;   r<   r   r=   lenr>   r?   r@   rA   rB   r   r4   
array_like	tag_fieldtagappendr-   valuesupdatepopitemLiteralTypesortedr9   
setdefaultrG   rb   rU   r8   r   encode_namer   	NODEFAULTr	   default_factorylistdictrr   	bytearray	enumeratereversedforbid_unknown_fieldszipr5   r6   r7   ExtType	TypeError
CustomTyper   NotImplementedError)rx   r    rz   r
   rS   r@   property_namesstructsotherr   subtype	real_typeoptionsr   struct_schemard   ra   r   rv   r8   fieldfield_scheman_trailing_defaultsrC   r   r   r   r      s   W  U  S 









 G 





 ? 

 8  4  0  .  +  (  %  " 

    




 


d
b^\ZU
3-
z_SchemaGenerator.to_schema)Nr   )r%   rc   r   r   r   r   )T)r    rV   rz   r{   r   r   )rG   rj   rk   ry   r   r   r   r   r   r+      s
    
r+   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r.   r1   r   r2   )r#   r   r   r   )r    rV   r   r   )r/   r2   r   rc   )
__future__r   re   r\   collections.abcr   typingr   r   r   rX   r   r'   r	   __all__r
   r   r)   rJ   rU   rb   r*   r+   r   r   r   r   <module>   s$    &
7'


