Skip to content

Commit

Permalink
fix optional operator with undefined format string
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jan 16, 2021
1 parent 2b498be commit b33e199
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
3 changes: 0 additions & 3 deletions cli/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ func (err *compileError) Error() string {
}

func (err *compileError) ExitCode() int {
if err, ok := err.err.(interface{ ExitCode() int }); ok {
return err.ExitCode()
}
return exitCodeCompileErr
}

Expand Down
9 changes: 9 additions & 0 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4514,6 +4514,15 @@
format not defined: @0
exit_code: 5

- name: format strings not defined error with optional operator
args:
- -n
- '@foo?, @bar"\(0|debug)"?, 1'
expected: |
1
error: |
["DEBUG:",0]
- name: destructuring alternative operator
args:
- '.[] | . as {a:$a} ?// [$a] ?// $a | $a'
Expand Down
14 changes: 9 additions & 5 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1218,13 +1218,17 @@ func (c *compiler) compileUnary(e *Unary) error {
}

func (c *compiler) compileFormat(fmt string, str *String) error {
if f := formatToFunc(fmt); f != nil {
if str == nil {
return c.compileFunc(f)
f := formatToFunc(fmt)
if f == nil {
f = &Func{
Name: "format",
Args: []*Query{{Term: &Term{Type: TermTypeString, Str: &String{Str: fmt[1:]}}}},
}
return c.compileString(str, f)
}
return &formatNotFoundError{fmt}
if str == nil {
return c.compileFunc(f)
}
return c.compileString(str, f)
}

func formatToFunc(fmt string) *Func {
Expand Down
4 changes: 0 additions & 4 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ func (err *formatNotFoundError) Error() string {
return fmt.Sprintf("format not defined: %s", err.n)
}

func (err *formatNotFoundError) ExitCode() int {
return 5
}

type formatCsvTsvRowError struct {
typ string
v interface{}
Expand Down

0 comments on commit b33e199

Please sign in to comment.