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

panic with --values #117

Open
cben opened this issue Dec 5, 2023 · 1 comment
Open

panic with --values #117

cben opened this issue Dec 5, 2023 · 1 comment

Comments

@cben
Copy link

cben commented Dec 5, 2023

I'm probably misunderstanding the purpose of --values flag, but I was trying it on various JSON inputs and saw Go panics in some cases 💥 (and nothing in others).
Here are pretty small reproducers (on latest master), which are weirdly sensitive to whitespace:

$ printf '{"foo": 3}' | ./gron --values
$ printf '{"foo":\n3}' | ./gron --values
$ printf '{"foo":\n  3}' | ./gron --values
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.gronValues({0xc000196120?, 0xc00019a010?}, {0xc000192190?, 0x75e498?}, 0x1?)
	/home/beni/gron/main.go:411 +0x2af
main.main()
	/home/beni/gron/main.go:178 +0x6cd
$ printf '{"foo": 3\n}' | ./gron --values
$ printf '{"foo": 3\n  }' | ./gron --values
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.gronValues({0xc00007c180?, 0xc000016050?}, {0xc00001a2a8?, 0x75e498?}, 0x1?)
	/home/beni/gron/main.go:411 +0x2af
main.main()
	/home/beni/gron/main.go:178 +0x6cd
@kousu
Copy link

kousu commented May 10, 2024

I saw this too and was confused. For example if I'm trying to extract some disk details:

$ nvme list -o json | gron -v
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.gronValues({0xc000126070?, 0xc00012a010?}, {0xc000142170?, 0x7555d8?}, 0x1?)
	github.com/tomnomnom/gron/main.go:411 +0x27f
main.main()
	github.com/tomnomnom/gron/main.go:178 +0x6bc

The solution is to run gron twice in a row:

$ nvme list -o json | gron | gron -v
/dev/nvme0n1
        
0
0
                                        
1
0
Non-Volatile memory controller: Toshiba Corporation NVMe SSD Controller
1
                    
0
/dev/nvme1n1
        
1
0
                                        
1
0
Non-Volatile memory controller: Toshiba Corporation NVMe SSD Controller
1
                    
0

I think this was probably missed because in practice you'd never use gron by itself, it's meant to be combined with grep, so you'd do something like:

nvme list -o json | gron | grep SerialNumber | gron --values

This is alluded to in the comments:

gron/main.go

Lines 401 to 404 in badf401

// gronValues prints just the scalar values from some input gron statements
// without any quotes or anything of that sort; a bit like jq -r
// e.g. json[0].user.name = "Sam"; -> Sam
func gronValues(r io.Reader, w io.Writer, opts int) (int, error) {

but not in the docs and it threw me off too. Maybe it can be explained with an example, or mentioning "Print just the values of gron-format assignments"?

And also it would be better not to panic at all. If the input format is wrong maybe that can become an error message explaining that the format is wrong.

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

2 participants