o
    `šj±  ã                   @   s,   d dl mZ d dlmZ G dd„ deƒZdS )é   )Ú
ArrayField)ÚStructFieldc                       s¦   e Zd Zedd„ ƒZedd„ ƒZd!‡ fdd„	Zedd	„ ƒZd
d„ Z	edd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zd"dd„Zd"dd„Zdd „ Z‡  ZS )#ÚStructArrayFieldc                 C   sT   d|vrd S t |d ƒdksJ dƒ‚|  |¡}|d u rd S i |¥d|i¥}t |¡S )NÚfieldsé    zUnexpected empty fields in jsonÚtype)ÚlenÚparse_array_typer   Ú
parse_json)ÚclsÚjsonÚtype_strÚ
inner_json© r   úh/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/protocol/schemas/fields/struct_array.pyÚparse_inner_type   s   

z!StructArrayField.parse_inner_typec                 C   s"   |   |¡}|d ur| ||dS d S )N©Úarray_of)r   )r   r   Ú
inner_typer   r   r   r
      s   
ÿzStructArrayField.parse_jsonNc                    sX   |d u r|   |¡}|d usJ dƒ‚tƒ j||d d |_ttdd„ t| jƒƒd ƒ| _d S )Nz$json does not contain a StructArray!r   c                 S   s   | d j  d¡S )Nr   ÚmapKey)Ú_jsonÚget)Úxr   r   r   Ú<lambda>"   s    z+StructArrayField.__init__.<locals>.<lambda>)	r   ÚsuperÚ__init__Ú_nullable_versionsÚnextÚfilterÚ	enumerateÚ_fieldsÚmap_key)Úselfr   r   ©Ú	__class__r   r   r      s   
 zStructArrayField.__init__c                 C   s   | j dd … S )Né   )Ú	_type_str©r"   r   r   r   r   $   s   zStructArrayField.type_strc                 C   s   dS )NTr   r'   r   r   r   Úis_struct_array(   s   z StructArrayField.is_struct_arrayc                 C   ó   | j jS ©N)r   r   r'   r   r   r   r   +   ó   zStructArrayField.fieldsc                 C   ó   | j  |¡S r*   )r   Útagged_fields©r"   Úversionr   r   r   r-   /   ó   zStructArrayField.tagged_fieldsc                 C   r,   r*   )r   Úuntagged_fieldsr.   r   r   r   r1   2   r0   z StructArrayField.untagged_fieldsc                 C   s
   | j  ¡ S r*   )r   Úhas_data_classr'   r   r   r   r2   5   ó   
zStructArrayField.has_data_classc                 C   r,   r*   )r   Úset_data_class)r"   Ú
data_classr   r   r   r4   8   r0   zStructArrayField.set_data_classc                 C   r)   r*   )r   r5   r'   r   r   r   r5   ;   r+   zStructArrayField.data_classc                 O   s   | j |i |¤ŽS r*   )r5   )r"   ÚargsÚkwr   r   r   Ú__call__?   s   zStructArrayField.__call__Fc                 C   sú   ddl m} | j}| d¡}	|r | ||	|¡ | |d|	 ¡ n| |d|	 ¡ | |d¡ | |d|	 ¡ | |d| ¡ | |d	¡ |d
 }
| |
d| ¡ | d¡}| d¡}| |
d||	f ¡ |j|||
d
 |||d | |
d||f ¡ d S )Nr   ©ÚUnsignedVarInt32Únz%s -= 1z$%s = unpack_from(">i", data, pos)[0]zpos += 4zif %s == -1:z    %s = Noneúelse:ú    z%s = []ÚidxÚobjzfor %s in range(%s):©r/   ÚcompactÚtaggedz    %s.append(%s))Úcodecsr:   r   Únext_varÚemit_decode_fromÚemit)r"   ÚctxÚvar_nameÚindentr/   rA   rB   r:   Úinner_structr;   Úinner_indentr>   Úitemr   r   r   rE   B   s(   


ÿz!StructArrayField.emit_decode_fromc              
   C   sš  ddl m} | j}| |¡}	|r0| d¡}
| |d|
||f ¡ | ||
|¡ | |d| ¡ n.| |d¡ | |d| ¡ | |d¡ | |d	¡ | |d
¡ | |d| ¡ | |d	¡ |d }| d¡}t|	ƒdkrú| |d||f ¡ |d }| |d|||f ¡ | |d||	d j	|||	d j	f ¡ |	d j||||||d |rà| d¡}| 
|¡|j|< | |d¡ | |d¡ | |d|||f ¡ | |d¡ | |d¡ | |d¡ d S |d u rø| |d¡ | |d¡ | |d¡ d S d S | d¡}| |d||||f ¡ | |d||f ¡ | |d||f ¡ |j|||d  |||d!d" | |d
¡ | |d||f ¡ |j|||d  |||d#d" d S )$Nr   r9   Úanz)%s = len(%s) + 1 if %s is not None else 0zif %s is not None:é   zif %s is None:z!    pack_into('>i', buf, pos, -1)z    pos += 4r<   z&    pack_into('>i', buf, pos, len(%s))r=   Úsizfor %s in %s:z$if isinstance(%s, tuple): %s = %s[0]z"elif hasattr(%s, "%s"): %s = %s.%sr   r@   Útfzout.pos = posz'# tagged fields for single-field structz#%s.encode_into(%s, out, version=%d)zpos = out.poszbuf = out.bufz_cap = len(buf)zbuf[pos] = 0zpos += 1Úlstz-%s = %s if isinstance(%s, list) else list(%s)z#if %s and isinstance(%s[0], tuple):z    for %s in %s:z        T)r/   rA   rB   Útuple_accessF)rC   r:   r   r1   rD   rF   Úemit_encode_intoÚemit_reserver   Únamer-   Úglobs)r"   rG   Úval_exprrI   r/   rA   rB   r:   rJ   r   rM   ÚguardÚitem_varÚscalar_indentÚtf_varÚlist_varr   r   r   rS   X   sn   


ÿÿ
ý
þ
þz!StructArrayField.emit_encode_intoc                 C   s
   d| j  S )NzStructArrayField(%s))r   r'   r   r   r   Ú__repr__’   r3   zStructArrayField.__repr__r*   )NFF)Ú__name__Ú
__module__Ú__qualname__Úclassmethodr   r
   r   Úpropertyr   r(   r   r-   r1   r2   r4   r5   r8   rE   rS   r]   Ú__classcell__r   r   r#   r   r      s*    






:r   N)Úarrayr   Ústructr   r   r   r   r   r   Ú<module>   s    