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

Custom @escape functions #271

Open
myaaaaaaaaa opened this issue Dec 20, 2024 · 4 comments
Open

Custom @escape functions #271

myaaaaaaaaa opened this issue Dec 20, 2024 · 4 comments

Comments

@myaaaaaaaaa
Copy link

Something like this?

gojq.Compile(
	query,
	gojq.WithEscaper("goprint", func(x any) string {
		return fmt.Sprint(x)
	}),
)
$ "abcde" | split("") |
	@json    "json:    \(.)",
	@sh      "sh:      \(.)",
	@goprint "goprint: \(.)"

json:    ["a","b","c","d","e"]
sh:      'a' 'b' 'c' 'd' 'e'
goprint: [a b c d e]
@wader
Copy link
Contributor

wader commented Dec 20, 2024

For reference, jaq implements it like this:

$ jaq -n 'def @upcase: ascii_upcase; "world" | @upcase "hello \(.)"'
"hello WORLD"

@myaaaaaaaaa
Copy link
Author

For reference, jaq implements it like this:

$ jaq -n 'def @upcase: ascii_upcase; "world" | @upcase "hello \(.)"'
"hello WORLD"

Pretty cool - although at that point, I think we may as well just redefine the @foo syntax to be syntactic sugar for a normal function call:

-"world" | @upcase "hello \(.)"
+"world" | "hello \(. | upcase)"

@wader
Copy link
Contributor

wader commented Dec 25, 2024

@myaaaaaaaaa now jqjq does it the same way at jaq 🥳 but as you say even more general would be nice but then would we extend the the standard library with all the existing formats as functions minus the "@"? 🤔

@myaaaaaaaaa
Copy link
Author

but then would we extend the the standard library with all the existing formats as functions minus the "@"? 🤔

Yes, that would be one of the pitfalls of this method - mainly the redundant text - tostring and json - tojson functions.

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