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

Support for dot notation variables and nested context_map #117

Open
Pacheco95 opened this issue Dec 31, 2022 · 5 comments
Open

Support for dot notation variables and nested context_map #117

Pacheco95 opened this issue Dec 31, 2022 · 5 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@Pacheco95
Copy link

Is it possible to access variables in context by dot notation like this?

use evalexpr::*;

fn main() {

    let ctx = context_map! {
        "a" => context_map! {
            "b" => 4
        }
    };

    assert_eq!(
        eval_with_context("a.b", ctx),
        Ok(4.into())
    );
}
@ISibboI
Copy link
Owner

ISibboI commented Mar 17, 2023

No, that is not possible. I was thinking about implementing something like this at some point, but I think I would rather leave that up to a pull request.

@ISibboI ISibboI added enhancement New feature or request question Further information is requested and removed enhancement New feature or request labels Mar 17, 2023
@ISibboI ISibboI added the help wanted Extra attention is needed label Apr 13, 2023
@sweihub
Copy link

sweihub commented Aug 21, 2023

I think we can expand this feature to a native JSON support, I will try to make a PR later, this will a big and useful feature.

proposal

eval("
    enemy = { x: 100, y: 200, z: get_z() };
    // return the coordinate
   (enemy.x, enemy.y, enemy.z)
")

@odyslam
Copy link

odyslam commented Nov 3, 2023

This is how I am doing this currently:

    fn add_value_to_context(
        &self,
        prefix: &str,
        value: &serde_json::Value,
        context: &mut HashMapContext,
    ) -> Result<(), EvalexprError> {
        match value {
            serde_json::Value::Object(obj) => {
                for (key, value) in obj {
                    let new_key = if prefix.is_empty() {
                        key.to_string()
                    } else {
                        format!("{}.{}", prefix, key)
                    };
                    self.add_value_to_context(&new_key, value, context)?;
                }
            }
      ...

And I can access a variable as such:

'object.nested.number == 5'

@CITGuru
Copy link

CITGuru commented Aug 23, 2024

@odyslam do you have a pr for this implementation or can you point to where this code is implemented

@neilskilling-overskilling

Hi @odyslam did you make any further progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants