openuado / niet

Parse/Read yaml or json files directly in your shell (sh, bash, ksh, ...)
https://pypi.org/project/niet/
MIT License
39 stars 7 forks source link

WIP Add deletion feature #59

Closed dj4ngo closed 2 years ago

4383 commented 5 years ago

Some feedback:

You've some issues in your code:

 $ echo '{"one": 1, "two": 2, "three": 3, "four": {"five": 5, "six": 6, "seven": {"eight": 8, "nine": 9}}}' | \
> niet  .two -d .six -d seven.eight
Traceback (most recent call last):
  File "/home/hberaud/.local/share/virtualenvs/niet-R7vGB2rB/bin/niet", line 10, in <module>
    sys.exit(main())
  File "/home/hberaud/dev/perso/niet/niet/__init__.py", line 188, in main
    result = actions.delete(result, to_delete)
  File "/home/hberaud/dev/perso/niet/niet/actions.py", line 6, in delete
    input_dict = delete(input_dict, path)
  File "/home/hberaud/dev/perso/niet/niet/actions.py", line 28, in delete
    if cur_search in input_dict:

I receive cur_search=[".six", "seven.eight"] and input_dict=2, after 1 iteration of your recursive calls I got cur_search="six" and input_dict=2.

The issue here is that you consider always your input as a python dict, but with this use case we work on the subset two only and so we only got the value of this key (2), so I think you need to skip deletion when input_dict is not a dict.

4383 commented 5 years ago

Else it seem to work fine with standard use case:

$ echo '{"one": 1, "two": 2, "three": 3, "four": {"five": 5, "six": 6, "seven": {"eight": 8, "nine": 9}}}' | \
> niet  . -d .four.six -d .four.seven.eight
{
    "one": 1,
    "two": 2,
    "three": 3,
    "four": {
        "five": 5,
        "seven": {
            "nine": 9
        }
    }
}
$ echo '{"one": 1, "two": 2, "three": 3, "four": {"five": 5, "six": 6, "seven": {"eight": 8, "nine": 9}}}' | \
> niet  .four -d six -d seven.eight 
{
    "five": 5,
    "seven": {
        "nine": 9
    }
4383 commented 5 years ago

Another things that need to be explicit in the documentation of this function is that we can only remove entire keys from a given structure.

I mean we couldn't remove an element from a list where the list is the value of a given key.

Example:

$ echo '{"one": 1, "two": 2, "three": 3, "four": {"five": 5, "six": [1, 2, 3], "seven": {"eight": 8, "nine": 9}}}' | \
> niet  .four -d "four.six[0]"
{
    "five": 5,
    "six": [
        1,
        2,
        3
    ],
    "seven": {
        "eight": 8,
        "nine": 9
    }
}

In the previous example users can expect to can remove the index 0 from the node four.six where six is a list.

It can become possible by adapting the search algorithm and by introducing a similar syntax than jmespath.

4383 commented 2 years ago

Do you still want to work on that? Else I think we can abandon this pull request.

4383 commented 2 years ago

Abandoning this PR as it didn't showed us recent activity