google / cel-spec

Common Expression Language -- specification and binary representation
https://cel.dev
Apache License 2.0
2.67k stars 216 forks source link

pseudo numeric values in map keys #238

Closed willie68 closed 2 years ago

willie68 commented 2 years ago

the problem: i have the following structure:

{
    "context": {
        "data": {
            "1234": {
                "username": "Willie"
            }
        }
    },
    "expression": "data.1234.username == \"Willie\""
}

expression is the part which is compiled, the context is the context for the evaluation. As you can see, there is a map key with a string "1234", but you can't evaluate with a expression like the one in the expression. You will get an ERROR: <input>:1:5: Syntax error: mismatched input '.1234' expecting <EOF>\n | data.1234.username == \"Willie\"\n | ....^ When i prefix the number with "a" in both parts, it's working. So i think the problem is related to the map key 1234 which should be a string but seems to be evaluated as a number. I can't change the value of 1234, because its dynamically generated nor the expression, which is a user input? How can i resolve this?

TristonianJones commented 2 years ago

Since 1234 is not a valid identifier it can't be used as one. If you need to use a non-identifier as an index into a map, use the index syntax instead: data['1234'].username == 'Willie'