
    <i              
       &   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dS )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                    || vrd S t          | |         |          s| d|j         }t          |          | |         S )Nz must be a )
isinstance__name__	TypeError)r   r   r   msgs       /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/key_value/shared/utils/serialization.pykey_must_ber      sT    
*tjo}55 99!799nnc?    valuec                 t    	 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#      sS    :%e,,, : : :1%11"3///T9:s    !7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dS )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                "    || _         || _        d S N)r(   r,   )selfr.   r/   s      r   __init__zSerializationAdapter.__init__1   s     ()r   json_strc                 N    t          |          }|                     |          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&G&G~~;~///r   r8   c                     dS )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                    |                      |          }i }| j        dk    rXt          |dt                    x}rt	          |          |d<   t          |dt                    x}rt	          |          |d<   | j        dk    r<t          |dt
                    x}r||d<   t          |dt
                    x}r||d<   d|vrd	}t          |
          |d         }i }t          |t                    rt          |          }n8t          |t                    rt          |          }nd}t          |
          t          ||                    d          |                    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   s      d ++.0++(<sSSSSz Y4FZ4X4X4X#L1(<sSSSSz Y4FZ4X4X4X#L1
**(<xXXXXz ?4>#L1(<xXXXXz ?4>#L1$)C&s3333W.0eS!! 	4"0%"@"@"@t$$ 	4"-%"8"8"8=C&s3333%*..|<<*..|<<
 
 
 	
r   c                     dS )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   T   )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d
 |                                D             }| 	                    |          S )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   Nr   rM   r&   rB   rC   r   c                     i | ]
\  }}|||S r1   r=   ).0kvs      r   
<dictcomp>z2SerializationAdapter.dump_dict.<locals>.<dictcomp>   s    CCCTQQ]Aq]]]r   r7   )
r,   value_as_dictvalue_as_jsonr(   created_at_isoformatexpires_at_isoformatrB   rC   itemsrK   )r2   rO   rP   r   rM   rN   r8   s          r   	dump_dictzSerializationAdapter.dump_dictr   s    . ,0,>&,H,HU((eNa 
  

 ?DK!!+D++!&!;D!&!;D
**!&!1D!&!1D 	DCCTZZ\\CCCD  d +++r   c          	          | j         dk    rd}t          |          t          |                     |||||                    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   r\   )r2   rO   rP   r   rM   rN   r   s          r   	dump_jsonzSerializationAdapter.dump_json   se    * 
**vC$S)))U[^ku  @G  !H  !H  I  I  I  	Ir   )T)r   
__module____qualname____doc__r(   r   __annotations__r,   r3   rF   r   r;   r   r)   r   r?   r9   rK   boolintr\   r^   r=   r   r   r%   r%   &   s          =HL'12T9GGG6<M7+,t3<<< IT  GM* * *%&=>E*cjk{c|  @D  dD*	* * * *0# 0, 0 0 0 0 Ac3h ADcN A A A ^A
'
d38n '
 '
 '
 '
 '
R }c3h }DcN } } } ^} ",,
 !%,, ,, ,,,, ,,
 4Z,, $J,, ,, 
c3h,, ,, ,, ,,b "I
 !%I I II I
 4ZI $JI I 
I I I 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                N    t                                          ||           d S )Nr-   )superr3   )r2   r.   r/   	__class__s      r   r3   z"BasicSerializationAdapter.__init__   s(     	[|LLLL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   rk   r   )r   r_   r`   ra   r   r3   r)   rF   r   r?   rK   __classcell__)ri   s   @r   rf   rf      s       PP IT  GMM M M%&=>EMcjk{c|  @D  dDM	M M M M M M
c3h DcN    c3h DcN        r   rf   )ra   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%   rf   r=   r   r   <module>rt      s    $ # # # # # # #       ( ( ( ( ( ( ( ( ( ( L L L L L L L L B B B B B B h h h h h h h h h h h hGCLL C Q DcN TUX\T\     :c :h : : : :RI RI RI RI RI3 RI RI RIj     4     r   