
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr  SSK	J
r
  SSKrS rS	 rS
 rS rS rS rS rS rS rS rg)zAFunctions for parsing SQL query parameters from the command line.    )absolute_import)division)unicode_literalsN)
exceptions)filesc           
      @   [         R                  " U 5       n[        R                  " U5      nU Vs/ s H0  n[        R
                  " [        UR                  5       5      5      PM2     nn[        R                  " U5      sSSS5        $ s  snf ! , (       d  f       g= f)zIReads a JSON file specified by path and returns its contents as a string.N)	r   
FileReaderjsonloadcollectionsOrderedDictsorteditemsdumps)pathparameters_file
parametersparamresultss        ;lib/googlecloudsdk/api_lib/dataflow/sql_query_parameters.pyParseParametersFiler      sz    ?+J EODN5u{{} 56J   ::g  s   B7B
)B
B
Bc                 x    / nU  H  nUR                  [        U5      5        M     [        R                  " U5      $ )zParses a list of parameters.

Arguments:
  parameters: A list of parameter strings with the format name:type:value,
    for example min_word_count:INT64:250.

Returns:
  A JSON string containing the parameters.
)append_ParseParameterr
   r   )r   r   	parameters      r   ParseParametersListr   '   s3     'iNN?9-. 	G	    c                 t    U R                  SS5      n[        U5      S:w  a  [        R                  " S5      eU$ )N:      zQuery parameters must be of the form: "name:type:value", ":type:value", or "name::value". An empty name produces a positional parameter. An empty type produces a STRING parameter.)splitlenr   Error)param_stringr"   s     r   _SplitParamr&   7   s>    


S!
$%Z1_


	56 6
 
,r   c                     [        U 5      u  p[        U 5      u  p#[        R                  " 5       nU(       a  XS'   X$S'   X4S'   U$ )NnameparameterTypeparameterValue)r&   _ParseParameterTypeAndValuer   r   )r%   r(   	type_dict
value_dictresults        r   r   r   B   sJ    "<0$5lC)""$&	6N%'		-r   c                 ^    [        U 5      u  pU(       d  Sn[        U5      nU[        X25      4$ )zCParse a string of the form <recursive_type>:<value> into each part.STRING)r&   _ParseParameterType_ParseParameterValue)r%   type_stringvalue_stringr,   s       r   r+   r+   M   s2    ),7+	K!+.)	(A	AAr   c                    SU R                  5       0nU R                  5       R                  S5      (       a<  U R                  S5      (       a&  [        R                  " S[        U SS 5      4S/5      nU R                  S5      (       a<  U R                  S5      (       a&  [        R                  " S	[        U S
S 5      4S/5      nU (       d  [        R                  " S5      eU$ )zGParse a parameter type string into a JSON dict for the DF SQL launcher.typezARRAY<>	arrayType   )r6   ARRAYzSTRUCT<structTypes   )r6   STRUCTzQuery parameter missing type)	upper
startswithendswithr   r   r1   _ParseStructTyper   r$   )r3   r,   s     r   r1   r1   V   s    {((*+)##H--+2F2Fs2K2K''	)+a*;<=?P) I I&&;+?+?+D+D''-*:;q;L*M*O);)= >I 



9
::	r   c                     / n[        U 5       H;  u  p#[        R                  " SU4S[        U5      4/5      nUR	                  U5        M=     U$ )z9Parse a Struct QueryParameter type into a JSON dict form.r(   r6   )_StructTypeSplitr   r   r1   r   )r3   subtypesr(   sub_typeentrys        r   rB   rB   f   sV    ((5nd##fd^&,.A(.K%L%N OEOOE 6 
/r   c              #     #    U (       a  U R                  SS5      S   nSU;   au  SnSn[        UR                  S5      [        U 5      5       H%  nX   S:X  a  US-  nX   S:X  a  US-  nUS:X  d  M%    O   US:w  a  [        R
                  " S5      eU SUS-    nU [        U5      S-   S n UR                  SS5      n[        U5      S:w  a  [        R
                  " S	5      eUv   U (       a  M  gg7f)
zCYields single field-name, sub-types tuple from a StructType string.,r    r   <r7   zMalformatted struct typeNr!   z'Struct parameter missing name for field)r"   rangefindr#   r   r$   )r3   	next_spanangle_countisplitss        r   rD   rD   p   s     !!#q)!,I
ik
aY^^C(#k*:;!>S 

+>S 

+!
 < 
	9::fq1u%ic)nq012K__T1%F
6{aFGG
L' 	s   A,C12A;C1/C1c                 d     [        U [        5      $ ! [         a    [        U [        5      s $ f = f)N)
isinstanceunicode	NameErrorstr)vals    r   	_IsStringrW      s0     c7##	  c3 s    //c                    SU ;   a  [        U5      (       a   US:X  a  SS0$ [        R                  " U5      n[        R                  " [        UR                  5       5      5      n[        R                  " U S    Vs/ s H  o"S   US   4PM     sn5      n[        R                  " 5       n[        R                  " U5       H  u  pV[        X5   U5      XE'   M     SU0$ SU ;   aS  [        U5      (       a   US:X  a  SS0$ [        R                  " U5      nU Vs/ s H  n[        U S   U5      PM     nnSU0$ S	US:w  a  U0$ S0$ s  snf s  snf )
aW  Parse a parameter value of type `type_dict` from value_input.

Arguments:
  type_dict: The JSON-dict type as which to parse `value_input`.
  value_input: Either a string representing the value, or a JSON dict for
    array and value types.

Returns:
  A dict with one of value, arrayValues, or structValues populated depending
  on the type.

r<   NULLstructValuesNr(   r6   r8   arrayValuesvalue)
rW   r
   loadsr   r   r   r   six	iteritemsr2   )r,   value_inputxtype_mapvalues
field_namer\   s          r   r2   r2      s_    i		%%JJ{+k))&1B1B1D*EFK&&(1-(@((@16AfI(@( H $$&F"}}[9/0DeLf  :F##I		t$$JJ{+kALALAY{3Q7   6""
+"7;	BBT	BB!(s   ;EE)__doc__
__future__r   r   r   r   r
   googlecloudsdk.api_lib.dataflowr   googlecloudsdk.core.utilr   r^   r   r   r&   r   r+   r1   rB   rD   rW   r2    r   r   <module>rj      sW    H &  '   6 * 
 B 0 #Cr   