o
    u©Zhj  ã                   @   sZ   d dl Zd dlmZ d dlmZ d dlmZ G dd„ dƒZG dd„ dƒZG d	d
„ d
ƒZ	dS )é    N)Ú
struct_pb2)Úmaps)Úrepeatedc                   @   ó<   e Zd ZdZdd„ Zddœdefdd„Zd	ejfd
d„Z	dS )Ú	ValueRulezBA rule to marshal between google.protobuf.Value and Python values.c                C   ó
   || _ d S ©N©Ú_marshal©ÚselfÚmarshal© r   úQ/var/www/html/lang_env/lib/python3.10/site-packages/proto/marshal/rules/struct.pyÚ__init__   ó   
zValueRule.__init__N©Úabsentr   c                C   s˜   |  d¡}|dks|rdS |dkrt|jƒS |dkrt|jƒS |dkr(t|jƒS |dkr7| jjt	j
|jdd	S |d
krF| jjt	j|jdd	S td| ƒ‚)aB  Coerce the given value to the appropriate Python type.

        Note that both NullValue and absent fields return None.
        In order to disambiguate between these two options,
        use containment check,
        E.g.
        "value" in foo
        which is True for NullValue and False for an absent value.
        ÚkindÚ
null_valueNÚ
bool_valueÚnumber_valueÚstring_valueÚstruct_valueFr   Ú
list_valuezUnexpected kind: %s)Z
WhichOneofÚboolr   Úfloatr   Ústrr   r
   Ú	to_pythonr   ÚStructr   Ú	ListValuer   Ú
ValueError)r   Úvaluer   r   r   r   r   r      s,   




ýýzValueRule.to_pythonÚreturnc                 C   sÆ   t |tjƒr|S |du rtjddS t |tƒrtj|dS t |ttfƒr,tjt|ƒdS t |tƒr7tj|dS t |tjj	ƒrJtj| j
 tj|¡dS t |tjjƒr]tj| j
 tj|¡dS td	| ƒ‚)
z7Return a protobuf Value object representing this value.Nr   )r   )r   )r   )r   )r   )r   zUnable to coerce value: %r)Ú
isinstancer   ÚValuer   Úintr   r   ÚcollectionsÚabcÚSequencer
   Úto_protor    ÚMappingr   r!   ©r   r"   r   r   r   r*   @   s&   

ÿÿzValueRule.to_proto)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r%   r*   r   r   r   r   r      s
    #r   c                   @   r   )ÚListValueRulezCA rule translating google.protobuf.ListValue and list-like objects.c                C   r   r   r	   r   r   r   r   r   Z   r   zListValueRule.__init__Nr   r   c                C   ó   |rdS t j|j| jdS )z,Coerce the given value to a Python sequence.N©r   )r   ÚRepeatedCompositeÚvaluesr
   ©r   r"   r   r   r   r   r   ]   s
   ÿÿýzListValueRule.to_pythonr#   c                    sN   t |tjƒr|S t |tjƒrtjdd„ |jD ƒdS tj‡ fdd„|D ƒdS )Nc                 S   s   g | ]}|‘qS r   r   ©Ú.0Úvr   r   r   Ú
<listcomp>k   s    z*ListValueRule.to_proto.<locals>.<listcomp>)r5   c                    s   g | ]
}ˆ j  tj|¡‘qS r   ©r
   r*   r   r%   r7   ©r   r   r   r:   o   s    )r$   r   r    r   r4   Úpbr,   r   r<   r   r*   e   s   ÿzListValueRule.to_proto)
r-   r.   r/   r0   r   r   r   r   r    r*   r   r   r   r   r1   W   s
    r1   c                   @   r   )Ú
StructRulez@A rule translating google.protobuf.Struct and dict-like objects.c                C   r   r   r	   r   r   r   r   r   v   r   zStructRule.__init__Nr   r   c                C   r2   )z+Coerce the given value to a Python mapping.Nr3   )r   ÚMapCompositeÚfieldsr
   r6   r   r   r   r   y   s   ÿÿzStructRule.to_pythonr#   c                    sZ   t |tjƒr|S t |tjƒrtjdd„ |j ¡ D ƒdS tj‡ fdd„| ¡ D ƒd}|S )Nc                 S   s   i | ]\}}||“qS r   r   ©r8   Úkr9   r   r   r   Ú
<dictcomp>†   s    z'StructRule.to_proto.<locals>.<dictcomp>)r@   c                    s"   i | ]\}}|ˆ j  tj|¡“qS r   r;   rA   r<   r   r   rC   ‹   s    ÿ)r$   r   r   r   r?   r=   Úitems)r   r"   Zanswerr   r<   r   r*      s   ÿ
ÿÿzStructRule.to_proto)
r-   r.   r/   r0   r   r   r   r   r   r*   r   r   r   r   r>   s   s
    r>   )
Úcollections.abcr'   Zgoogle.protobufr   Zproto.marshal.collectionsr   r   r   r1   r>   r   r   r   r   Ú<module>   s   @