Comments (12)
@itchyny Thanks!
from gojq.
I'm working on this in #46, and you will be able to define your internal function as follows. Any thoughts?
package main
import (
"fmt"
"log"
"github.com/itchyny/gojq"
)
var compilerOptions = []gojq.CompilerOption{
// gojq.WithFunction({function name}, {arity (argument count)}, {function})
gojq.WithFunction("f", 0, func(x interface{}, _ []interface{}) interface{} {
if x, ok := x.(int); ok {
return x * 2
}
return fmt.Errorf("f cannot be applied to: %v", x)
}),
gojq.WithFunction("g", 1, func(x interface{}, xs []interface{}) interface{} {
if x, ok := x.(int); ok {
if y, ok := xs[0].(int); ok {
return x + y
}
}
return fmt.Errorf("g cannot be applied to: %v, %v", x, xs)
}),
}
func main() {
query, err := gojq.Parse(".[] | f | g(3)")
if err != nil {
log.Fatalln(err)
}
code, err := gojq.Compile(query, compilerOptions...)
if err != nil {
log.Fatalln(err)
}
iter := code.Run([]interface{}{1, 2, 3, 4, 5})
for {
v, ok := iter.Next()
if !ok {
break
}
fmt.Printf("%v\n", v)
}
}
from gojq.
Added some comments in the PR. API looks good to me.
from gojq.
I do support optional arguments in my hack, but now when i think about it that might be bad and the correct way would be to use multiple WithFunction
with different artity and then maybe share go code between them?
from gojq.
Rebased and fixed my things on top of your branch, seems to work fine 👍
from gojq.
I do support optional arguments in my hack, but now when i think about it that might be bad and the correct way would be to use multiple WithFunction with different artity and then maybe share go code between them?
That's exactly what I was concerned. And supporting same function name with different arities introduces a bit hacky code. I'm wondering making the second argument of WithFunction to []int
, the list of acceptable arities.
from gojq.
When I give up supporting adding the functions with the same name, the code would be clean. For example, using gojq.WithFunction("f", []int{0, 1}, F)
and gojq.WithFunction("f", []int{2, 3}, G)
. Supporting all the f/0 to f/3 is difficult.
from gojq.
I think in most cases you want min/max arg count range? gojq.WithFunction("f", [2]int{0, 1}, F)
or gojq.WithFunction("f", 0, 1, F)
? not very pretty.
I guess you don't want to support varargs?
Sorry could you clarify what you mean by "Supporting all the f/0 to f/3 is difficult."? hard to do in the gojq implementation or hard to write custom functions that behave well?
from gojq.
I think in most cases you want min/max arg count range? gojq.WithFunction("f", [2]int{0, 1}, F) or gojq.WithFunction("f", 0, 1, F)? not very pretty.
I like the idea of giving both min/max arity. It will cover the use case without duplicating the function implementation with the same name.
Sorry could you clarify what you mean by "Supporting all the f/0 to f/3 is difficult."? hard to do in the gojq implementation or hard to write custom functions that behave well?
I was thinking of overwriting the former definition by the latter when the function name conflicts. But I can implement the case by the same way I used to do within the previous implementation accepting single arity. So user don't need to worry about this (see this test for sample).
Thanks for your valuable feedback, I will merge the branch in a few days.
from gojq.
Nice! glad I can help. And thank you for gojq, very useful and i've learned a lot while reading the code.
Did you see my comment about passing along customFuncs
to def and modules? #46 (comment)
from gojq.
Did you see my comment about passing along customFuncs to def and modules?
Yes, I was to push my changes after writing tests.
from gojq.
@itchyny 👍
from gojq.
Related Issues (20)
- Difference between these two functions HOT 3
- path(getpath()) roundtrip doesn't work for array iterators - .[] HOT 3
- Raw output HOT 1
- gojq -nr '["\u0000"] | @tsv' HOT 6
- Sub and gsub functions HOT 2
- first/1 and --stream HOT 9
- can u provide gojq rpm package ? HOT 1
- concurrency issue detected with go1.19 HOT 2
- undocumented discrepancy re test/1 HOT 2
- gohq won't compile with the latest golang:alpine on ARM64 HOT 6
- Inability to provide user-defined indexing funtion HOT 5
- Panic in `repeatString` when repeating an empty string more than `9223372036854775807 - 512` times HOT 1
- Only getting `gojq: compile error: function not defined: keys_unsorted/0` HOT 3
- normalizeNumbers panic HOT 12
- go 1.20.1 has security issues HOT 2
- Non-integer decimal numbers lose precision HOT 2
- "For paths starting with $ORIGIN/, the path of the jq executable is substituted for $ORIGIN." HOT 7
- Support for context.Context in func(any, []any) any HOT 5
- Import statement for importing the contents of a file HOT 4
- Support "if" without "else" clause HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gojq.