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

Multiple changes to the same file in one run #16

Open
tim-kos opened this issue Aug 28, 2018 · 4 comments
Open

Multiple changes to the same file in one run #16

tim-kos opened this issue Aug 28, 2018 · 4 comments

Comments

@tim-kos
Copy link

tim-kos commented Aug 28, 2018

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? 😱

@tidwall
Copy link
Owner

tidwall commented Aug 31, 2018

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?

@Acconut
Copy link

Acconut commented Sep 5, 2018

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?

@tidwall
Copy link
Owner

tidwall commented Sep 5, 2018

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.

@nick-bull
Copy link

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"
  }
}

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

No branches or pull requests

4 participants