@@ -25,6 +25,11 @@ pub struct Expression {
2525 accessors : Vec < Accessor > ,
2626}
2727
28+ fn node_to_string ( node : & Node , statement_bytes : & [ u8 ] ) -> Result < String , DscError > {
29+ let text = node. utf8_text ( statement_bytes) ?;
30+ Ok ( text. to_string ( ) )
31+ }
32+
2833impl Expression {
2934 /// Create a new `Expression` instance.
3035 ///
@@ -41,11 +46,11 @@ impl Expression {
4146 let Some ( function) = expression. child_by_field_name ( "function" ) else {
4247 return Err ( DscError :: Parser ( t ! ( "parser.expression.functionNodeNotFound" ) . to_string ( ) ) ) ;
4348 } ;
44- debug ! ( "{}" , t!( "parser.expression.parsingFunction" , name = function : { : ?} ) ) ;
49+ debug ! ( "{}" , t!( "parser.expression.parsingFunction" , name = node_to_string ( & function, statement_bytes ) ? : { : ?} ) ) ;
4550 let function = Function :: new ( statement_bytes, & function) ?;
4651 let mut accessors = Vec :: < Accessor > :: new ( ) ;
4752 if let Some ( accessor) = expression. child_by_field_name ( "accessor" ) {
48- debug ! ( "{}" , t!( "parser.expression.parsingAccessor" , name = accessor : { : ?} ) ) ;
53+ debug ! ( "{}" , t!( "parser.expression.parsingAccessor" , name = node_to_string ( & accessor, statement_bytes ) ? : { : ?} ) ) ;
4954 if accessor. is_error ( ) {
5055 return Err ( DscError :: Parser ( t ! ( "parser.expression.accessorParsingError" ) . to_string ( ) ) ) ;
5156 }
@@ -57,30 +62,39 @@ impl Expression {
5762 let accessor_kind = accessor. kind ( ) ;
5863 let value = match accessor_kind {
5964 "memberAccess" => {
60- debug ! ( "{}" , t!( "parser.expression.parsingMemberAccessor" , name = accessor : { : ?} ) ) ;
65+ debug ! ( "{}" , t!( "parser.expression.parsingMemberAccessor" , name = node_to_string ( & accessor, statement_bytes ) ? : { : ?} ) ) ;
6166 let Some ( member_name) = accessor. child_by_field_name ( "name" ) else {
6267 return Err ( DscError :: Parser ( t ! ( "parser.expression.memberNotFound" ) . to_string ( ) ) ) ;
6368 } ;
6469 let member = member_name. utf8_text ( statement_bytes) ?;
6570 Accessor :: Member ( member. to_string ( ) )
6671 } ,
6772 "index" => {
68- debug ! ( "{}" , t!( "parser.expression.parsingIndexAccessor" , index = accessor : { : ?} ) ) ;
73+ debug ! ( "{}" , t!( "parser.expression.parsingIndexAccessor" , index = node_to_string ( & accessor, statement_bytes ) ? : { : ?} ) ) ;
6974 let Some ( index_value) = accessor. child_by_field_name ( "indexValue" ) else {
7075 return Err ( DscError :: Parser ( t ! ( "parser.expression.indexNotFound" ) . to_string ( ) ) ) ;
7176 } ;
77+ debug ! ( "{}" , t!( "parser.expression.indexValue" , value = node_to_string( & index_value, statement_bytes) ? : { : ?} , kind = index_value. kind( ) ) ) ;
7278 match index_value. kind ( ) {
7379 "number" => {
7480 let value = index_value. utf8_text ( statement_bytes) ?;
75- let value = serde_json:: from_str ( value) ?;
76- Accessor :: Index ( value)
81+ let number: i64 = value. parse ( ) . map_err ( |_| DscError :: Parser ( t ! ( "parser.expression.indexNotValid" ) . to_string ( ) ) ) ?;
82+ Accessor :: Index ( Value :: Number ( number. into ( ) ) )
83+ } ,
84+ "propertyName" => {
85+ let Some ( string_node) = index_value. child_by_field_name ( "string" ) else {
86+ return Err ( DscError :: Parser ( t ! ( "parser.expression.propertyNameNotString" ) . to_string ( ) ) ) ;
87+ } ;
88+ let value = string_node. utf8_text ( statement_bytes) ?;
89+ debug ! ( "{}" , t!( "parser.expression.propertyNameValue" , value = value : { : ?} ) ) ;
90+ Accessor :: Index ( Value :: String ( value. to_string ( ) ) )
7791 } ,
7892 "expression" => {
7993 let expression = Expression :: new ( statement_bytes, & index_value) ?;
8094 Accessor :: IndexExpression ( expression)
8195 } ,
8296 _ => {
83- return Err ( DscError :: Parser ( t ! ( "parser.expression.invalidAccessorKind " , kind = accessor_kind ) . to_string ( ) ) ) ;
97+ return Err ( DscError :: Parser ( t ! ( "parser.expression.invalidIndexValueKind " , kind = index_value . kind ( ) ) . to_string ( ) ) ) ;
8498 } ,
8599 }
86100 } ,
@@ -186,6 +200,21 @@ impl Expression {
186200 return Err ( DscError :: Parser ( t ! ( "parser.expression.indexOnNonArray" ) . to_string ( ) ) ) ;
187201 }
188202 }
203+ else if index. is_string ( ) {
204+ let index = index. as_str ( ) . ok_or_else ( || DscError :: Parser ( t ! ( "parser.expression.indexNotValid" ) . to_string ( ) ) ) ?;
205+ if let Some ( object) = value. as_object ( ) {
206+ if !object. contains_key ( index) {
207+ return Err ( DscError :: Parser ( t ! ( "parser.expression.memberNameNotFound" , member = index) . to_string ( ) ) ) ;
208+ }
209+ if is_secure {
210+ value = convert_to_secure ( & object[ index] ) ;
211+ } else {
212+ value = object[ index] . clone ( ) ;
213+ }
214+ } else {
215+ return Err ( DscError :: Parser ( t ! ( "parser.expression.accessOnNonObject" ) . to_string ( ) ) ) ;
216+ }
217+ }
189218 else if !index. is_null ( ) {
190219 return Err ( DscError :: Parser ( t ! ( "parser.expression.invalidIndexType" ) . to_string ( ) ) ) ;
191220 }
0 commit comments