jj icon indicating copy to clipboard operation
jj copied to clipboard

Multiple changes to the same file in one run

Open tim-kos opened this issue 6 years ago • 4 comments

Hey there,

Thank you for an awesome tool! For my use case I need to do multiple small modifications to the same json document and I'd like to do them in one call to the binary.

So for example, instead of:

jj-v1.2.1 -v 500 -O -i my_file.json -o my_file.json bytes_received
jj-v1.2.1 -v 1500 -O -i my_file.json -o my_file.json bytes_expected
jj-v1.2.1 -v 75.62 -O -i my_file.json -o my_file.json upload_duration

I'd like to do something like this:

jj-v1.2.1 -v 500,1500,75.62 -O -i my_file.json -o my_file.json bytes_received,bytes_expected,upload_duration

Reasoning is, less child processes (which are costly in node.js, which I am using) and reading in the same json file only once in Go instead of multiple times (https://github.com/tidwall/jj/blob/master/cmd/jj/main.go#L151) and also saving it only once to disk instead of multiple times (https://github.com/tidwall/jj/blob/master/cmd/jj/main.go#L246).

Is this something that you'd consider implementing, or maybe even accepting a PR for it? 😱

tim-kos avatar Aug 28 '18 09:08 tim-kos

This is a good idea.

I recommend using standard command line args for each key/value rather than comma-delimited, because there's the possibility that commas are a part of the JSON key it's associated value, and we should avoid any additional escaping.

jj -v 500 -v 1500 -v 75.62 -O -i my_file.json -o my_file.json bytes_received bytes_expected upload_duration

This way the jj command will recognize that there are three updates occuring to the JSON file and expect three keys.

How does this sound?

tidwall avatar Aug 31 '18 17:08 tidwall

Good propasal, @tidwall. What should happen if someone if trying to updates multiple properties via stdin/file and does not supply the values using -v? Should that be prohibited?

Acconut avatar Sep 05 '18 19:09 Acconut

Yeah. I think that should be prohibited. Multiple keys for update/set operations only. Unless we want to accept multiple keys for a get operations and return the values as a JSON array.

tidwall avatar Sep 05 '18 19:09 tidwall

Temporary solution - edit for your requirements:

jj_v() {
 __val="$1";
 __key="$2";
 jj -v "$__val" "$__key" | (test -n "$3" && f "${@:3}" || cat)
}

echo '{"name":{"last":"Smith"}}' | jj_v Andy name.first Dandy name.middle outputs:

{
  "name": {
    "middle": "Dandy",
    "first": "Andy",
    "last": "Smith"
  }
}

nick-bull avatar Feb 11 '19 20:02 nick-bull