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 while printing panic value: type gojq.Operator #272

Open
ceving opened this issue Feb 28, 2025 · 1 comment
Open

panic while printing panic value: type gojq.Operator #272

ceving opened this issue Feb 28, 2025 · 1 comment

Comments

@ceving
Copy link

ceving commented Feb 28, 2025

I would like to dump jq.Query:

package main

import (
	"fmt"
	"log"
	jq "github.com/itchyny/gojq"
	"os"
)

func ReadJsonQueryFile(fname string) (*jq.Query, error) {
	data, err := os.ReadFile(fname)
	if err != nil {
		return nil, fmt.Errorf("Can not read JSON query file %q", fname, err)
	}
	query, err := jq.Parse(string(data))
	if err != nil {
		return nil, fmt.Errorf("Can not parse JSON query file %q", fname, err)
	}
	return query, nil
}

func DumpQuery(q *jq.Query) {
	fmt.Printf("Meta: %+v\n", q.Meta)
	fmt.Printf("Imports: %+v\n", q.Imports)
	fmt.Printf("FuncDefs: %+v\n", q.FuncDefs)
	fmt.Printf("Term: %+v\n", q.Term)
	fmt.Printf("Left: %+v\n", q.Left)
	fmt.Printf("Op: %+v\n", q.Op)
	fmt.Printf("Right: %+v\n", q.Right)
	fmt.Printf("Func: %+v\n", q.Func)
}

func main() {
	q, err := ReadJsonQueryFile(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	DumpQuery(q)
}

When I run the code, it panics:

$ go1.24.0 run jqv.go <(echo '$var')
Meta: <nil>
Imports: []
FuncDefs: []
Term: $var
Left: <nil>
fatal error: panic while printing panic value: type gojq.Operator

I think it should not do that.

@itchyny
Copy link
Owner

itchyny commented Feb 28, 2025

An undocumented assumption that user who is trying to traverse gojq.Query should know is that q.Term and q.{Left, Op, Right} are exclusive, and if q.Term is not nil, you shouldn't access the others. And zero value of Operator is not implemented because this will never be printed under this assumption. Even I don't know how to print the zero value of Operator. How do you expect it to be printed? If you still want to debug the Query struct value, use %#v formatter. gojq.Operator implements fmt.GoStringer interface because printing the struct with Go syntax format is sometimes useful for debugging.

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