question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to evaluate string with "-"

See original GitHub issue

Usecase - I’m trying to do nested lookup in Map. Issue - If the “.” separated key I’m searching in the map has “-”, then evaluation fails

public void test() {
    Map attribute = new HashMap<String, Object>() {{
        put("merchant_attributes", new HashMap<String, Object>() {{
            put("c652a326-5243-12ab-a4cd-111111", new HashMap<String, Integer>() {{
                put("num_orders", 2);
            }});
        }});
    }};

    MVEL.eval("merchant_attributes.c652a326-5243-12ab-a4cd-111111.num_orders", attribute); 
}

Evaluating the above test throws an error saying [Error: could not access: c652a126; in class:

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MalcolmOddcommented, Sep 27, 2022

@MalcolmOdd How to escape strings that are starting with a number?

@Test
    public void test() {
        Map attribute = new HashMap<String, Object>() {{
            put("merchant_attributes", new HashMap<String, Object>() {{
                put("c652a326-5243-12ab-a4cd-111111", new HashMap<String, Object>() {{
                    put("1652a326-5243-12ab-a4cd-111112", new HashMap<String, Integer>() {{
                        put("num_orders", 2);
                    }});
                }});
            }});
        }};
        MVEL.eval("merchant_attributes[\"c652a326-5243-12ab-a4cd-111111\"][\"1652a326-5243-12ab-a4cd-111112\"]", attribute);
    }

This evaluates to null, the expectation was to return a object having num_orders attribute within it.

I tried it and it evaluates to a singleton map with num_order=2 your test was ignoring the result but I changed the last line to:

      assertEquals(Collections.singletonMap("num_orders",  2), MVEL.eval("merchant_attributes[\"c652a326-5243-12ab-a4cd-111111\"][\"1652a326-5243-12ab-a4cd-111112\"]", attribute));

When using strings as keys using the square brackets operator, the leading numbers do not matter.

1reaction
MalcolmOddcommented, Sep 26, 2022

The dot operator expects an identifier on the right, and identifiers cannot have dashes (as it is parsed as the minus operator) or start with a number. You can use the square brackets to access it as follows:

      MVEL.eval("merchant_attributes[\"c652a326-5243-12ab-a4cd-111111\"].num_orders", attribute); 

The underscore “_” is valid to use in an identifier

Read more comments on GitHub >

github_iconTop Results From Across the Web

I am unable to evaluate string using eval() - Stack Overflow
I am trying to make a calculator application.I want to evaluate the contents of a TextView and display it as a toast message....
Read more >
Debugger unable to evaluate string interpolation
When trying to use the watch, immediate window or any other option for evaluating expressions when debugging, VS2022 is unable to evaluate simple...
Read more >
Unable to eval expression when debugging #2485 - GitHub
Unable to eval expression when debugging #2485 ... package main import ( "fmt" "strings" ) func WordCount(s string) map[string]int { m ...
Read more >
Unable to evaluate expression because the code is ... - MSDN
I have some code that gets the exception Unable to evaluate expression because the code is optimized or a native frame is on...
Read more >
eval() - JavaScript - MDN Web Docs - Mozilla
The eval() function evaluates JavaScript code represented as a string and ... during evaluation of the code, including SyntaxError if script fails to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found