Skip to content

Commit

Permalink
interp: Use gojqextra.NonUpdatableTypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Dec 2, 2021
1 parent d4b2d58 commit 1b7b2f9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pkg/interp/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (bv Buffer) JQValueToGoJQ() interface{} {
return buf.String()
}
func (bv Buffer) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "buffer"}
return gojqextra.NonUpdatableTypeError{Key: fmt.Sprintf("%v", key), Typ: "buffer"}
}

func (bv Buffer) Display(w io.Writer, opts Options) error {
Expand Down
14 changes: 7 additions & 7 deletions pkg/interp/testdata/value_array.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,19 @@ mp3> .headers._unknown | ., type, length?
false
"boolean"
mp3> .headers.a = 1
error: cannot update key a for array
error: update key a cannot be applied to: array
mp3> .headers[0] = 1
error: cannot update key 0 for array
error: update key 0 cannot be applied to: array
mp3> .headers.a |= empty
error: expected an object with key "a" but got: array
mp3> .headers[0] |= empty
error: cannot update key 0 for array
error: update key 0 cannot be applied to: array
mp3> .headers | setpath(["a"]; 1)
error: cannot update key a for array
error: update key a cannot be applied to: array
mp3> .headers | setpath([0]; 1)
error: cannot update key 0 for array
error: update key 0 cannot be applied to: array
mp3> .headers | delpaths([["a"]])
error: cannot update key a for array
error: update key a cannot be applied to: array
mp3> .headers | delpaths([[0]])
error: cannot update key 0 for array
error: update key 0 cannot be applied to: array
mp3> ^D
12 changes: 6 additions & 6 deletions pkg/interp/testdata/value_object.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ mp3> .headers[0].flags._unknown | ., type, length?
false
"boolean"
mp3> .headers[0].flags.a = 1
error: cannot update key a for object
error: update key a cannot be applied to: object
mp3> .headers[0].flags[0] = 1
error: expected an array with index 0 but got: object
mp3> .headers[0].flags.a |= empty
error: cannot update key a for object
error: update key a cannot be applied to: object
mp3> .headers[0].flags[0] |= empty
error: expected an array with index 0 but got: object
mp3> .headers[0].flags | setpath(["a"]; 1)
error: cannot update key a for object
error: update key a cannot be applied to: object
mp3> .headers[0].flags | setpath([0]; 1)
error: cannot update key 0 for object
error: update key 0 cannot be applied to: object
mp3> .headers[0].flags | delpaths([["a"]])
error: cannot update key a for object
error: update key a cannot be applied to: object
mp3> .headers[0].flags | delpaths([[0]])
error: cannot update key 0 for object
error: update key 0 cannot be applied to: object
mp3> ^D
13 changes: 2 additions & 11 deletions pkg/interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ func (err expectedExtkeyError) Error() string {
return "expected a extkey but got: " + err.Key
}

type notUpdateableError struct {
Typ string
Key string
}

func (err notUpdateableError) Error() string {
return fmt.Sprintf("cannot update key %s for %s", err.Key, err.Typ)
}

// TODO: redo/rename
// used by _isDecodeValue
type DecodeValue interface {
Expand Down Expand Up @@ -503,7 +494,7 @@ func (v ArrayDecodeValue) JQValueSlice(start int, end int) interface{} {
return vs
}
func (v ArrayDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "array"}
return gojqextra.NonUpdatableTypeError{Key: fmt.Sprintf("%v", key), Typ: "array"}
}
func (v ArrayDecodeValue) JQValueEach() interface{} {
props := make([]gojq.PathValue, len(*v.Compound.Children))
Expand Down Expand Up @@ -572,7 +563,7 @@ func (v StructDecodeValue) JQValueKey(name string) interface{} {
return nil
}
func (v StructDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "object"}
return gojqextra.NonUpdatableTypeError{Key: fmt.Sprintf("%v", key), Typ: "object"}
}
func (v StructDecodeValue) JQValueEach() interface{} {
props := make([]gojq.PathValue, len(*v.Compound.Children))
Expand Down

0 comments on commit 1b7b2f9

Please sign in to comment.