
    j                         d dl mZmZmZmZmZ d dlZddlm	Z	 ddl
m
Z
 deeef   dee   ded	   d
eeef   fdZdeeef   dee   dee
j                     ded	   d
eeef   f
dZy)    )DictAnySetListLiteralN   )jsonable_encoder)
anonymizerkwargs_dictfields_to_anonymizeobject_type)spantracereturnc                     t        j                  |       }t        j                  j	                         s|S t        j                  j                         }t        ||||      S )a  
    Encodes and anonymizes the data in the given dictionary based on the specified
    fields using registered anonymizers. If no anonymizers are registered, the
    function simply encodes the dictionary without anonymization.

    Args:
        kwargs_dict: The dictionary containing the data to encode
            and anonymize.
        fields_to_anonymize: The set of fields within the dictionary to
            anonymize.
        object_type: A string indicating the type of object ('span' or 'trace')
            that was used to create the kwargs_dict. This is passed to anonymizers
            to provide context about the source object.

    Returns:
        A dictionary that has been encoded and, if applicable, anonymized.
    )objr   anonymizersr   )r	   encodeopikhookshas_anonymizersget_anonymizersanonymize_encoded_obj)r   r   r   encoded_objr   s        /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/message_processing/encoder_helpers.pyencode_and_anonymizer   	   sU    . #))+6K::%%'**,,.K /	     r   r   c                     t        | t              r-|D ](  }|| v s|D ]  }|j                  | |   ||      | |<    * | S )a  
    Anonymizes specified fields in an encoded dictionary using the provided anonymizers.
    This function iterates over the given set of field names and applies each anonymizer
    to the corresponding field in the dictionary, if present. The anonymizers are expected
    to implement an `anonymize` method that takes the field value, field name, and object type
    as arguments. Only fields present in the dictionary and listed in `fields_to_anonymize`
    are anonymized.

    Args:
        obj: The encoded dictionary whose fields are to be anonymized.
        fields_to_anonymize: A set of field names within the dictionary to anonymize.
        anonymizers: A list of anonymizer instances to apply to each field.
        object_type: A string indicating the type of object ('span' or 'trace'),
            providing context for anonymization.

    Returns:
        The dictionary with specified fields anonymized using the provided anonymizers.
    )
field_namer   )
isinstancedict	anonymize)r   r   r   r   r   anonymizer_instances         r   r   r   -   sY    0 #t-JS +6'&9&C&CJ#-$/ 'D 'C
O ,7 . Jr   )typingr   r   r   r   r   
opik.hooksr    r	   r
   strr   
Anonymizerr    r   r   <module>r*      s    0 0   #!c3h!S! )! 
#s(^	!H"	c3h"S" j++," )	"
 
#s(^"r   