expressions icon indicating copy to clipboard operation
expressions copied to clipboard

What if I want to use a special match expression

Open misskter opened this issue 4 years ago • 5 comments

I'm glad to provide this plug-in, but when I use this plug-in, my expression may contain some special characters and chinese, such as the '$测试'. Does this plug-in support of this writing method?

This is the code I wrote

 Expression expression = Expression.parse('\$测试');

  var context = {
    '\$测试': 1,
  };
  
  final evaluator = const ExpressionEvaluator();
  var r = evaluator.eval(expression, context);
  print(r);


But I got such a mistake

E/flutter ( 4910): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: end of input expected at 1:2
E/flutter ( 4910): #0      Failure.value (package:petitparser/src/context/failure.dart:15:18)
E/flutter ( 4910): #1      Expression.parse (package:expressions/src/expressions.dart:33:55)
E/flutter ( 4910): #2      main (package:test_demo/test_function.dart:25:38)
E/flutter ( 4910): #3      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:231:25)
E/flutter ( 4910): #4      _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 4910): #5      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 4910): #6      _runZoned (dart:async/zone.dart:1630:10)
E/flutter ( 4910): #7      runZonedGuarded (dart:async/zone.dart:1618:12)
E/flutter ( 4910): #8      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:223:5)
E/flutter ( 4910): #9      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter ( 4910): #10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter ( 4910): 

So how do I call it? If I don't support this way of writing, I think I will consider using English

misskter avatar Oct 26 '20 03:10 misskter

In my case i use '\$response.key'

where my map is

{
  '\$response':{
       'key': 'value'
    }
}

This works fine. Can't say about only $response

apgapg avatar Oct 26 '20 04:10 apgapg

However, the data returned by my business is data that looks like $测试 and its expression may end up like this' $测试-5-10 '

misskter avatar Oct 26 '20 05:10 misskter

@misskter Let me check once again with $

apgapg avatar Oct 26 '20 06:10 apgapg

thank you

misskter avatar Oct 26 '20 06:10 misskter

@misskter

 test('\$response should work fine', () {
      const expression = '\$response';
      final result = evaluateSyncExpression(
        expression: expression,
        map: <String, dynamic>{'\$response': 23},
      );
      expect(result, 23);
    });

This works fine in my case

apgapg avatar Oct 26 '20 09:10 apgapg

special characters in variable names are not supported

rbellens avatar Dec 27 '22 17:12 rbellens