josephburnett / jd

JSON diff and patch
MIT License
826 stars 38 forks source link

Unexpected set differences with setkeys #73

Closed duallain closed 4 months ago

duallain commented 5 months ago

I noticed this odd behavior, and figured it might be helpful to you to have a simple reproduction. It's very possible I just don't understand how these commands should work, if that's the case, a few words in the readme on how setkeys should work would be lovely.

While running this command, with the two example files below, I would expect there to be no diff at all, but as you can see from the output there is a diff. My understanding of the setkeys option is that anything not listed there should be ignored for the purpose of diffing two json files.

❯ jd -set -setkeys "column-name"  examplea.json exampleb.json
@ ["rules",["set","setkeys=column-name"],{"column-name":"columna","rule-id":"17000100"},"rule-id"]
- "17000100"
+ "19000100"
@ ["rules",["set","setkeys=column-name"],{"column-name":"columnb","rule-id":"18000200"},"rule-id"]
- "18000200"
+ "20000200"

examplea.json:

{
    "rules": [
        {
            "column-name": "columna",
            "rule-id": "17000100"
        },
        {
            "column-name": "columnb",
            "rule-id": "18000200"
        }
    ]
}

exampleb.json:

{
    "rules": [
        {
            "column-name": "columna",
            "rule-id": "19000100"
        },
        {
            "column-name": "columnb",
            "rule-id": "20000200"
        }
    ]
}
jd -version
jd version 1.8.1
josephburnett commented 4 months ago

@duallain the setkeys option determines how the identity of an object should be determined. So -setkeys "column-name" means "two objects are the same one if the value under column-name is the same". So you can flip around the order of the rules but you'll get the same result. jd will compare the columna objects and provide a diff. And it will compare the columnb objects and provide a diff.

I can see how the name "setkeys" could be confusing! I've been thinking of adding a feature to ignore / include certain fields and calling it "masking". I think that's what you were expecting. What could I call "setkeys" instead that would be more clear? How about "idkeys"? I'm working on a v2 API so I can rename it there.

duallain commented 4 months ago

Ahh, I think I understand.

I think the name is ok, all the alternatives seem clunky as well. Not that sure it helps much to rename, but I do like including id in the name, that does help. Maybe objectIDKeys (or setIDKeys)?

Maybe a few more words in the readme to explain what's going on would help. I made a quick PR to try and capture this understanding -- https://github.com/josephburnett/jd/pull/75

🤞 I've correctly understood what you're saying, and wrote it up reasonably as well. Closing this issue since I think you took care of my request to understand already.