Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving of nested custom context Map<String, Object> not working. #154

Closed
gethrok opened this issue Jul 29, 2024 · 2 comments · Fixed by #155
Closed

Resolving of nested custom context Map<String, Object> not working. #154

gethrok opened this issue Jul 29, 2024 · 2 comments · Fixed by #155

Comments

@gethrok
Copy link

gethrok commented Jul 29, 2024

Hi,
I came across use case, where I want to use JSON as custom context. For example:

{
  "number1": 2,
  "number2": 3,
  "nested": {
    "nestedValue": "hello",
    "nested2": {
      "nestedValue2": "hello2"
    }
  }
}

Then we add it to the config as:

Configuration config = new Configuration();
Map<String, Object> jsonDeserialized = (Map<String, Object>)JSON.deserializeUntyped(inp.exampleJson);
config.customContext = jsonDeserialized;

However the nested values are not resolved. So accessing for example @nested.nestedValue will result in error. I have found out, that in the Interpreter class you are resolving Map<Object,Object> however in this case it's Map<String,Object> so the exception
"Error executing get expression: could not determine the value of:" will be fired.
My current fix is to simply add the Map<String, Object> to the Interpreter resolution. Works fine even with multiple levels of nested values.

} else if (result instanceof Map<String, Object>) {
    if (!getExpr.arguments.isEmpty()) {
        throw new Exceptions.RuntimeException(
            getExpr.field,
            'Error executing expression. Get expressions on Maps must not have arguments.'
        );
    }

    Map<String, Object> castMap = (Map<String, Object>) result;
    if (castMap.containsKey(getExpr.field.lexeme)) {
        return castMap.get(getExpr.field.lexeme);
    }

    throw new Exceptions.RuntimeException(
        getExpr.field,
        'Error executing get expression: the map does not contain a key with the name: ' + getExpr.field.lexeme
    );
}
@cesarParra
Copy link
Owner

Hi @gethrok

Looks good to me!

Let me put that fix in and release a new version

@cesarParra
Copy link
Owner

cesarParra commented Jul 29, 2024

Fix is in. In case of using the managed package it is in version >=1.10 (04tRb000001SS2PIAW)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants