Skip to content

Commit

Permalink
implement input_filename/0 function (close #90)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jul 14, 2021
1 parent 6c1c0d8 commit 80e93a1
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cli/cli.go
Expand Up @@ -225,6 +225,14 @@ Synopsis:
gojq.WithVariables(cli.argnames),
gojq.WithFunction("debug", 0, 0, cli.funcDebug),
gojq.WithFunction("stderr", 0, 0, cli.funcStderr),
gojq.WithFunction("input_filename", 0, 0,
func(interface{}, []interface{}) interface{} {
if fname := iter.Name(); fname != "" {
return fname
}
return nil
},
),
gojq.WithInputIter(iter),
)
if err != nil {
Expand Down
41 changes: 39 additions & 2 deletions cli/inputs.go
Expand Up @@ -64,6 +64,7 @@ func (ir *inputReader) getContents(offset *int64, line *int) string {
type inputIter interface {
gojq.Iter
io.Closer
Name() string
}

type jsonInputIter struct {
Expand Down Expand Up @@ -114,6 +115,10 @@ func (i *jsonInputIter) Close() error {
return nil
}

func (i *jsonInputIter) Name() string {
return i.fname
}

type nullInputIter struct {
err error
}
Expand All @@ -135,6 +140,10 @@ func (i *nullInputIter) Close() error {
return nil
}

func (i *nullInputIter) Name() string {
return ""
}

type filesInputIter struct {
newIter func(io.Reader, string) inputIter
fnames []string
Expand Down Expand Up @@ -186,13 +195,21 @@ func (i *filesInputIter) Close() error {
return nil
}

func (i *filesInputIter) Name() string {
if i.file != nil {
return i.file.Name()
}
return ""
}

type rawInputIter struct {
scanner *bufio.Scanner
fname string
err error
}

func newRawInputIter(r io.Reader, _ string) inputIter {
return &rawInputIter{scanner: bufio.NewScanner(r)}
func newRawInputIter(r io.Reader, fname string) inputIter {
return &rawInputIter{scanner: bufio.NewScanner(r), fname: fname}
}

func (i *rawInputIter) Next() (interface{}, bool) {
Expand All @@ -214,6 +231,10 @@ func (i *rawInputIter) Close() error {
return nil
}

func (i *rawInputIter) Name() string {
return i.fname
}

type streamInputIter struct {
stream *jsonStream
ir *inputReader
Expand Down Expand Up @@ -262,6 +283,10 @@ func (i *streamInputIter) Close() error {
return nil
}

func (i *streamInputIter) Name() string {
return i.fname
}

type yamlInputIter struct {
dec *yaml.Decoder
ir *inputReader
Expand Down Expand Up @@ -296,6 +321,10 @@ func (i *yamlInputIter) Close() error {
return nil
}

func (i *yamlInputIter) Name() string {
return i.fname
}

type slurpInputIter struct {
iter inputIter
err error
Expand Down Expand Up @@ -334,6 +363,10 @@ func (i *slurpInputIter) Close() error {
return nil
}

func (i *slurpInputIter) Name() string {
return i.iter.Name()
}

type slurpRawInputIter struct {
iter inputIter
err error
Expand Down Expand Up @@ -371,3 +404,7 @@ func (i *slurpRawInputIter) Close() error {
}
return nil
}

func (i *slurpRawInputIter) Name() string {
return i.iter.Name()
}
89 changes: 89 additions & 0 deletions cli/test.yaml
Expand Up @@ -4134,6 +4134,95 @@
"debug/0"
"stderr/0"
- name: input_filename function with stdin input
args:
- 'input_filename'
input: '0 1 2'
expected: |
"<stdin>"
"<stdin>"
"<stdin>"
- name: input_filename function with null input option
args:
- -n
- 'input_filename'
input: '0'
expected: |
null
- name: input_filename function with raw strings input option
args:
- --raw-input
- 'input_filename'
input: |
1
2
3
expected: |
"<stdin>"
"<stdin>"
"<stdin>"
- name: input_filename function with slurp option
args:
- --slurp
- 'input_filename'
input: |
1
2
3
expected: |
"<stdin>"
- name: input_filename function with input and slurp option
args:
- --raw-input
- --slurp
- 'input_filename'
input: |
1
2
3
expected: |
"<stdin>"
- name: input_filename function with json file arguments
args:
- 'input_filename'
- 'testdata/1.json'
- 'testdata/2.json'
input: '0'
expected: |
"testdata/1.json"
"testdata/2.json"
- name: input_filename function with yaml input option
args:
- --yaml-input
- 'input_filename'
input: '0'
expected: |
"<stdin>"
- name: input_filename function with yaml file arguments
args:
- --yaml-input
- 'input_filename'
- 'testdata/1.yaml'
input: '0'
expected: |
"testdata/1.yaml"
"testdata/1.yaml"
"testdata/1.yaml"
- name: input_filename in builtins
args:
- 'builtins[] | select(test("input_filename"))'
input: 'null'
expected: |
"input_filename/0"
- name: halt function
args:
- 'def f($n): if $n > 0 then $n, f($n - 1) else halt end; f(.), .'
Expand Down

0 comments on commit 80e93a1

Please sign in to comment.