
    Ii;                         S r SSKrSSKrSSKrSSKJrJrJrJrJ	r	  SSK
Jr  \\\\4   \\   \\\\S4   r " S S\R"                  5      r " S S\R&                  5      rS	\S
\R*                  4S jrg)a  
Type Adapter to convert Python-native types into CEL structures.

Currently, Atomic Python objects have direct use of types in :mod:`celpy.celtypes`.

Non-Atomic Python objects are characterized by JSON and Protobuf
objects. This module has functions to convert JSON objects to CEL.

The protobuf decoder is TBD.

A more sophisticated type injection capability may be needed to permit
additional types or extensions to :mod:`celpy.celtypes`.
    N)AnyDictListUnioncast)celtypesc            
          ^  \ rS rSrSr\S\R                  S\\R                  \	\
   \\
\
4   \4   4S j5       rS\R                  S\4U 4S jjrS\R                  S\4U 4S jjrSrU =r$ )	CELJSONEncoder&   z
An Encoder to export CEL objects as JSON text.

This is **not** a reversible transformation. Some things are coerced to strings
without any more detailed type marker.
Specifically timestamps, durations, and bytes.

cel_objectreturnc                    [        U [        R                  5      (       a  U (       a  S$ S$ [        U [        R                  5      (       a&  U  Vs/ s H  n[        R                  U5      PM     sn$ [        U [        R                  5      (       aL  U R                  5        VVs0 s H.  u  p#[        R                  U5      [        R                  U5      _M0     snn$ U $ s  snf s  snnf )a  Recursive walk through the CEL object, replacing BoolType with native bool instances.
This lets the :py:mod:`json` module correctly represent the obects
with JSON ``true`` and ``false``.

This will also replace ListType and MapType with native ``list`` and ``dict``.
All other CEL objects will be left intact. This creates an intermediate hybrid
beast that's not quite a :py:class:`celtypes.Value` because a few things have been replaced.
TF)
isinstancer   BoolTypeListTyper
   	to_pythonMapTypeitems)r   itemkeyvalues       3/venv/lib/python3.13/site-packages/celpy/adapter.pyr   CELJSONEncoder.to_python.   s     j("3"344%4050
H$5$566?IJztN,,T2zJJ
H$4$455 #-"2"2"4"4JC ((-~/G/G/NN"4 
  Ks   C"5C"c                 H   > [         TU ]  [        R                  U5      5      $ )zL
Override built-in encode to create proper Python :py:class:`bool` objects.
)superencoder
   r   selfr   	__class__s     r   r   CELJSONEncoder.encodeE   s     w~n66zBCC    c                 n  > [        U[        R                  5      (       a  [        U5      $ [        U[        R                  5      (       a  [        U5      $ [        U[        R
                  5      (       a%  [        R                  " U5      R                  S5      $ [        [        [        TU ]1  U5      5      $ )NASCII)r   r   TimestampTypestrDurationType	BytesTypebase64	b64encodedecoder   JSONr   defaultr   s     r   r,   CELJSONEncoder.defaultK   s    j("8"899z?"
H$9$9::z?"
H$6$677##J/66w??egoj9::r!    )__name__
__module____qualname____firstlineno____doc__staticmethodr   Valuer   r   r   r   boolr   r%   r   r+   r,   __static_attributes____classcell__r   s   @r   r
   r
   &   s      +0cDQTVYQYN\`1`+a ,D DC D;(.. ;T ; ;r!   r
   c                   @   ^  \ rS rSrSrSS\S\S\4U 4S jjjrSrU =r	$ )	CELJSONDecoderV   z
An Encoder to import CEL objects from JSON to the extent possible.

This does not handle non-JSON types in any form. Coercion from string
to TimestampType or DurationType or BytesType is handled by celtype
constructors.
source_wr   c                 8   > [         TU ]  U5      n[        U5      $ N)r   r*   json_to_cel)r   r=   r>   raw_jsonr   s       r   r*   CELJSONDecoder.decode^   s    7>&)8$$r!   r.   r@   )
r/   r0   r1   r2   r3   r%   r   r*   r7   r8   r9   s   @r   r;   r;   V   s'    %S %c %S % %r!   r;   documentr   c           
         [        U [        5      (       a  [        R                  " U 5      $ [        U [        5      (       a  [        R
                  " U 5      $ [        U [        5      (       a  [        R                  " U 5      $ [        U [        5      (       a  [        R                  " U 5      $ U c  g[        U [        [        45      (       a0  [        R                  " U  Vs/ s H  n[        U5      PM     sn5      $ [        U [        5      (       aL  [        R                  " U R!                  5        VVs0 s H  u  p#[        U5      [        U5      _M     snn5      $ [        U ["        R"                  5      (       a  [        R$                  " U 5      $ [        U ["        R&                  5      (       a  [        R(                  " U 5      $ [+        S[-        U 5       SU < 35      es  snf s  snnf )a  Convert parsed JSON object from Python to CEL to the extent possible.

It's difficult to distinguish strings which should be timestamps or durations.

::

    >>> from pprint import pprint
    >>> from celpy.adapter import json_to_cel
    >>> doc = json.loads('["str", 42, 3.14, null, true, {"hello": "world"}]')
    >>> cel = json_to_cel(doc)
    >>> pprint(cel)
    ListType([StringType('str'), IntType(42), DoubleType(3.14), None, BoolType(True), MapType({StringType('hello'): StringType('world')})])
Nzunexpected type z in JSON structure )r   r6   r   r   float
DoubleTypeintIntTyper%   
StringTypetupler   r   rA   r   r   r   datetimer$   	timedeltar&   
ValueErrortype)rD   r   r   r   s       r   rA   rA   c   s    (D!!  **	He	$	$""8,,	Hc	"	"))	Hc	"	"""8,,			Hudm	,	,  +3484[84
 	
 
Hd	#	#DLNNDTUDTjc[{511DTU
 	
 
Hh//	0	0%%h//	Hh00	1	1$$X..+DN+;;Nxl[\\ 5 Vs    G"4!G'
)r3   r(   rL   jsontypingr   r   r   r   r   celpyr   r%   r6   rF   rH   r+   JSONEncoderr
   JSONDecoderr;   r5   rA   r.   r!   r   <module>rU      s       / / T#s(^T#YeS#tCD-;T%% -;`
%T%% 
%&]$ &]8>> &]r!   