
    Ki              
           d Z ddlmZmZ ddlmZ ddlmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZmZmZmZ  ed      Zed	ed
ee   deeef   dedz  fd       Zededefd       Z G d de      Z G d de      Zy)aJ  Serialization adapter base class for converting ManagedEntry objects to/from store-specific formats.

This module provides the SerializationAdapter ABC that store implementations should use
to define their own serialization strategy. Store-specific adapter implementations
should be defined within their respective store modules.
    )ABCabstractmethod)datetime)AnyLiteralTypeVar)DeserializationErrorSerializationError)bear_enforce)ManagedEntrydump_to_jsonload_from_jsonverify_dictTkeyexpected_type
dictionaryreturnNc                l    || vry t        | |   |      s| d|j                   }t        |      | |   S )Nz must be a )
isinstance__name__	TypeError)r   r   r   msgs       p/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/key_value/shared/utils/serialization.pykey_must_ber      sF    
*jo}5[!7!7 89nc?    valuec                 l    	 t        j                  |       S # t        $ r d|  }t        |      d w xY w)NzInvalid datetime string: message)r   fromisoformat
ValueErrorr	   )r   r   s     r   parse_datetime_strr#      s@    :%%e,, :)%1"3/T9:s    3c                   ~   e Zd ZU dZdZed   dz  ed<   dZed   dz  ed<   ddd	d
ed   dz  ded   dz  ddfdZde	de
fdZedee	ef   dee	ef   fd       Zdee	ef   de
fdZedee	ef   dee	ef   fd       Z	 dddddde
dede	dz  de	dz  dedee	ef   fdZ	 dddddde
dede	dz  de	dz  dede	fdZy)SerializationAdaptera  Base class for store-specific serialization adapters.

    Adapters encapsulate the logic for converting between ManagedEntry objects
    and store-specific storage formats. This provides a consistent interface
    while allowing each store to optimize its serialization strategy.
    	isoformatr&   r   N_date_formatdictstringr)   _value_formatdate_formatvalue_formatr.   r/   r   c                     || _         || _        y N)r(   r,   )selfr.   r/   s      r   __init__zSerializationAdapter.__init__1   s     ()r   json_strc                 >    t        |      }| j                  |      S )z(Convert a JSON string to a ManagedEntry.r4   data)r   	load_dict)r2   r4   loaded_datas      r   	load_jsonzSerializationAdapter.load_json7   s    &4h&G~~;~//r   r8   c                      y)zPrepare data for loading.

        This method is used by subclasses to handle any required transformations before loading the data into a ManagedEntry.N r2   r8   s     r   prepare_loadz!SerializationAdapter.prepare_load=       r   c                 p   | j                  |      }i }| j                  dk(  rFt        |dt              x}rt	        |      |d<   t        |dt              x}rt	        |      |d<   | j                  dk(  r2t        |dt
              x}r||d<   t        |dt
              x}r||d<   d|vrd	}t        |
      |d   }i }t        |t              rt        |      }n+t        |t              rt        |      }nd}t        |
      t        ||j                  d      |j                  d            S )z'Convert a dictionary to a ManagedEntry.r7   r&   
created_at)r   r   )r   
expires_atr   r   zValue field not foundr   r6   objz)Value field is not a string or dictionary)r   rB   rC   )r?   r(   r   strr#   r   r	   r   r   r)   r   r   get)r2   r8   managed_entry_protorB   rC   r   r   managed_entry_values           r   r9   zSerializationAdapter.load_dictC   sL      d +.0+(<sSSzS4FZ4X#L1(<sSSzS4FZ4X#L1
*(<xXXzX4>#L1(<xXXzX4>#L1$)C&s33W.0eS!"0%"@t$"-%"8=C&s33%*..|<*..|<
 	
r   c                      y)zPrepare data for dumping to a dictionary.

        This method is used by subclasses to handle any required transformations before dumping the data to a dictionary.Nr=   r>   s     r   prepare_dumpz!SerializationAdapter.prepare_dumpl   r@   r      )r   
collectionversionentryexclude_noner   rM   rN   c                   || j                   dk(  r|j                  n|j                  d}|||d<   |||d<   | j                  dk(  r|j                  |d<   |j
                  |d<   | j                  dk(  r|j                  |d<   |j                  |d<   |r&|j                         D ci c]  \  }}|	|| }}}| j                  |	      S c c}}w )
a  Convert a ManagedEntry to a dictionary.

        Args:
            entry: The ManagedEntry to serialize
            exclude_none: Whether to exclude None values from the output
            key: Optional unsanitized key name to include in the document
            collection: Optional unsanitized collection name to include in the document
            version: Document schema version (default: 1)

        Returns:
            A dictionary representation of the ManagedEntry with optional metadata fields
        r)   )rN   r   r   rM   r&   rB   rC   r   r7   )
r,   value_as_dictvalue_as_jsonr(   created_at_isoformatexpires_at_isoformatrB   rC   itemsrK   )	r2   rO   rP   r   rM   rN   r8   kvs	            r   	dump_dictzSerializationAdapter.dump_dictr   s    . ,0,>,>&,HU((eNaNa 

 ?DK!!+D+!&!;!;D!&!;!;D
*!&!1!1D!&!1!1D%)ZZ\CTQQ]AqDCDC  d ++ Ds   (
C3Cc          	      z    | j                   dk(  rd}t        |      t        | j                  |||||            S )a  Convert a ManagedEntry to a JSON string.

        Args:
            entry: The ManagedEntry to serialize
            exclude_none: Whether to exclude None values from the output
            key: Optional unsanitized key name to include in the document
            collection: Optional unsanitized collection name to include in the document
            version: Document schema version (default: 1)

        Returns:
            A JSON string representation of the ManagedEntry with optional metadata fields
        r   zbdump_json is incompatible with date_format="datetime"; use date_format="isoformat" or dump_dict().)rO   rP   r   rM   rN   rD   )r(   r
   r   rY   )r2   rO   rP   r   rM   rN   r   s          r   	dump_jsonzSerializationAdapter.dump_json   sP    * 
*vC$S))U[^ku  @G  !H  I  	Ir   )T)r   
__module____qualname____doc__r(   r   __annotations__r,   r3   rF   r   r;   r   r)   r   r?   r9   rK   boolintrY   r[   r=   r   r   r%   r%   &   s    =HL'12T9G6<M7+,t3< IT  GM*%&=>E*cjk{c|  @D  dD*	*0# 0, 0 Ac3h ADcN A A
'
d38n '
 '
R }c3h }DcN } } ",,
 !%,,,, ,,
 4Z,, $J,, ,, 
c3h,,b "I
 !%II I
 4ZI $JI I 
Ir   r%   c                        e Zd ZdZdddded   dz  ded	   dz  d
df fdZdeeef   d
eeef   fdZ	deeef   d
eeef   fdZ
 xZS )BasicSerializationAdapterzFBasic serialization adapter that does not perform any transformations.r&   r)   r-   r.   r'   Nr/   r*   r   c                (    t         |   ||       y )Nr-   )superr3   )r2   r.   r/   	__class__s      r   r3   z"BasicSerializationAdapter.__init__   s     	[|Lr   r8   c                     |S r1   r=   r>   s     r   r?   z&BasicSerializationAdapter.prepare_load       r   c                     |S r1   r=   r>   s     r   rK   z&BasicSerializationAdapter.prepare_dump   rh   r   )r   r\   r]   r^   r   r3   r)   rF   r   r?   rK   __classcell__)rf   s   @r   rc   rc      s    P IT  GMM%&=>EMcjk{c|  @D  dDM	M
c3h DcN c3h DcN r   rc   )r^   abcr   r   r   typingr   r   r   key_value.shared.errorsr	   r
   )key_value.shared.type_checking.bear_sprayr   $key_value.shared.utils.managed_entryr   r   r   r   r   rF   typer)   r   r#   r%   rc   r=   r   r   <module>rq      s    $  ( ( L B h hCL C Q DcN TUX\T\   :c :h : :RI3 RIj 4 r   