o
    Mfj                     @   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   \/var/www/html/analyze/labelStudio/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)
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>)r6   c                    s   g | ]
} j tj|qS r   r
   r+   r   r&   r8   r   r   r   r;   o   s    )r%   r   r!   r   r5   pbr-   r   r=   r   r+   e   s   zListValueRule.to_proto)
r.   r/   r0   r1   r   r   r   r   r!   r+   r   r   r   r   r2   W   s
    r2   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   r3   )z+Coerce the given value to a Python mapping.Nr4   )r   MapCompositefieldsr
   r7   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   r9   kr:   r   r   r   
<dictcomp>   s    z'StructRule.to_proto.<locals>.<dictcomp>)rA   c                    s"   i | ]\}}| j tj|qS r   r<   rB   r=   r   r   rD      s    )r%   r   r    r   r@   r>   items)r   r#   answerr   r=   r   r+      s   
zStructRule.to_proto)
r.   r/   r0   r1   r   r   r   r   r    r+   r   r   r   r   r?   s   s
    r?   )
collections.abcr(   google.protobufr   proto.marshal.collectionsr   r   r   r2   r?   r   r   r   r   <module>   s   @