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

Select for Update/Delete #1

Closed
gkorland opened this issue Apr 24, 2019 · 11 comments
Closed

Select for Update/Delete #1

gkorland opened this issue Apr 24, 2019 · 11 comments
Labels
question Further information is requested

Comments

@gkorland
Copy link
Contributor

gkorland commented Apr 24, 2019

Is there a way to select for update/delete?

let result = selector
    .path("$..[?(@.age >= 30)]").unwrap()
    .value(&json_obj).delete() 
@freestrings
Copy link
Owner

Hi,

APIs for handling for "mut json_obj" are not yet supported. I am developing query and transform functions for "&json_obj" first. For example,

selector
     .select_then()?
     .map(|mut v | { .. })
     .map_as(|v: result::Result<Person, String>| { .. })?
     .take_as::<Person>()?;

@freestrings freestrings added the question Further information is requested label May 6, 2019
@gkorland
Copy link
Contributor Author

gkorland commented May 7, 2019

Any progress on that? Do you need any help here?

@freestrings
Copy link
Owner

Hi, sorry for the late response. I didn’t notice your comment. the "map" and the "map_as" function merged in master.

@gkorland
Copy link
Contributor Author

Thanks! Are you planing to add a way to mutate the JSON based on jsonpath?

@freestrings
Copy link
Owner

yes, i have a plan to write the APIs to mutate the JSON.
It will look roughly like this:

selector.path().value().map(..)
  .fork()    
    .map(..)
    .merge()

or

selector.path(..).value().map(..)
    .into_mut()
        .delete(path)
        .replace(path, obj)
        .insert(path, obj)
        .commit()

do you have better idea?

@gkorland
Copy link
Contributor Author

gkorland commented May 16, 2019

what will .into_mut() do? clone to muted?

@freestrings
Copy link
Owner

I keep thinking about "into_mut" but the basic meaning of "into_mut" is to clone the current context.

let json = json!({
    b: {
        c: 1
    }
})

without "into_mut"

selector
    .value(&json)
    .path("$.a.b.c")
    .delete()
    .get()

==>
{
    b: {}
}

with "into_mut"

selector
    .value(&json)
    .path("$.a.b.c")
    .into_mut() // create Mutable { .. }
    .delete()
    .get() // Mutable's get()

>>
{
    b: {}
}


selector
    .value(&json)
    .path("$.a.c.d")
    .into_mut()
    .delete()
    .end() // destroy Mutable
    .get() // original value

>>
{
    b: {
        c: 1
    }
}


selector
    .value(&json)
    .path("$.a.c.d")
    .into_mut()
    .delete()
    .commit() //
    .get()

>>
{
    b: {}
}

@gkorland
Copy link
Contributor Author

Looks good!
My original thought was to create another struct called Decorator that will be a variant of Selector and will get mutable reference of the doc, this way you won't have to clone it.

@freestrings
Copy link
Owner

freestrings commented May 20, 2019

Thanks for comment.
i think that the "without into_mut" example is solution for your question. like your first comment. i will add "Modifiable(?) trait" first.

let json = json!({
    b: {
        c: 1
    }
})

use Modifiable;

let a = selector
    .value(&json)
    .path("$.b.c")
    .delete()
    .get()

>>
print(a)
{
    b: {}
}

print(json)
{
    b: {
        c: 1
    }
}

@gkorland
Copy link
Contributor Author

OK, I'll be glad to test it once you have it ready, my deepest concern is about performance.
Same with Selector select if it could have been a slice of the original JSON and not a copy that could have been much more efficient

@gkorland
Copy link
Contributor Author

Great!!

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

No branches or pull requests

2 participants