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

eval() function #38

Closed
LeonB opened this issue Jul 2, 2020 · 3 comments
Closed

eval() function #38

LeonB opened this issue Jul 2, 2020 · 3 comments
Labels
question Further information is requested

Comments

@LeonB
Copy link

LeonB commented Jul 2, 2020

Would it be possible to add an eval() function?

@itchyny
Copy link
Owner

itchyny commented Jul 2, 2020

Would you explain the use case? It evaluates some jq code constructed from the input JSON?

@itchyny
Copy link
Owner

itchyny commented Jul 8, 2020

This is unlikely to be included until jq implements this.

@itchyny itchyny closed this as completed Jul 8, 2020
@itchyny itchyny added the question Further information is requested label Dec 20, 2020
@LeonB
Copy link
Author

LeonB commented Jul 28, 2021

Maybe useful for someone else:

func eval(c interface{}, a []interface{}) gojq.Iter {
	src, ok := a[0].(string)
	if !ok {
		return gojq.NewIter(fmt.Errorf("%v: src is not a string", a[0]))
	}

	gq, err := gojq.Parse(src)
	if err != nil {
		return gojq.NewIter(err)
	}
	gc, err := gojq.Compile(
		gq,
		p.CompilerOptions()...,
	)
	if err != nil {
		return gojq.NewIter(err)
	}

	return gc.Run(c)
}

gojq.WithIterFunction("eval", 1, 1, eval)

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

Successfully merging a pull request may close this issue.

2 participants