Skip to content

Commit

Permalink
#35 implemented node.Set and node.SetNode methods (#40)
Browse files Browse the repository at this point in the history
* Implemented `node.Set` and `node.SetNode` methods

* Added `go1.17.x` version to test

* Updated release version

* Added default results for `(*Node)(nil)` methods
  • Loading branch information
spyzhov authored Nov 6, 2021
1 parent 32110c0 commit 376eb0b
Show file tree
Hide file tree
Showing 13 changed files with 844 additions and 46 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
args: -v

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go:
- 1.14.x
- 1.15.x
- 1.16.x
- 1.17.x

env:
- GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
You can download `ajson` cli from the [release page](https://github.com/spyzhov/ajson/releases), or install from the source:

```shell script
go get github.com/spyzhov/ajson/cmd/ajson@v0.5.0
go get github.com/spyzhov/ajson/cmd/ajson@v0.6.0
```

Usage:
Expand Down
35 changes: 34 additions & 1 deletion buffer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ajson

import (
. "github.com/spyzhov/ajson/internal"
"io"
"strings"

. "github.com/spyzhov/ajson/internal"
)

type buffer struct {
Expand Down Expand Up @@ -773,3 +774,35 @@ func str(key string) (string, bool) {
// }
// return unquote(bString, quotes)
}

func numeric2float64(value interface{}) (result float64, err error) {
switch typed := value.(type) {
case float64:
result = typed
case float32:
result = float64(typed)
case int:
result = float64(typed)
case int8:
result = float64(typed)
case int16:
result = float64(typed)
case int32:
result = float64(typed)
case int64:
result = float64(typed)
case uint:
result = float64(typed)
case uint8:
result = float64(typed)
case uint16:
result = float64(typed)
case uint32:
result = float64(typed)
case uint64:
result = float64(typed)
default:
err = unsupportedType(value)
}
return
}
2 changes: 1 addition & 1 deletion cmd/ajson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spyzhov/ajson"
)

var version = "v0.4.2"
var version = "v0.6.0"

func usage() {
text := ``
Expand Down
46 changes: 38 additions & 8 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Error struct {
Index int
Char byte
Message string
Value interface{}
}

// ErrorType is container for reflection type of error
Expand All @@ -24,34 +25,61 @@ const (
WrongRequest
// Unparsed means that json structure wasn't parsed yet
Unparsed
// UnsupportedType means that wrong type was given
UnsupportedType
)

func errorSymbol(b *buffer) error {
c, err := b.current()
symbol, err := b.current()
if err != nil {
c = 0
symbol = 0
}
return Error{
Type: WrongSymbol,
Index: b.index,
Char: symbol,
}
return Error{Type: WrongSymbol, Index: b.index, Char: c}
}

func errorAt(index int, symbol byte) error {
return Error{Type: WrongSymbol, Index: index, Char: symbol}
return Error{
Type: WrongSymbol,
Index: index,
Char: symbol,
}
}

func errorEOF(b *buffer) error {
return Error{Type: UnexpectedEOF, Index: b.index}
return Error{
Type: UnexpectedEOF,
Index: b.index,
}
}

func errorType() error {
return Error{Type: WrongType}
return Error{
Type: WrongType,
}
}

func unsupportedType(value interface{}) error {
return Error{
Type: UnsupportedType,
Value: value,
}
}

func errorUnparsed() error {
return Error{Type: Unparsed}
return Error{
Type: Unparsed,
}
}

func errorRequest(format string, args ...interface{}) error {
return Error{Type: WrongRequest, Message: fmt.Sprintf(format, args...)}
return Error{
Type: WrongRequest,
Message: fmt.Sprintf(format, args...),
}
}

// Error interface implementation
Expand All @@ -63,6 +91,8 @@ func (err Error) Error() string {
return "unexpected end of file"
case WrongType:
return "wrong type of Node"
case UnsupportedType:
return fmt.Sprintf("unsupported type was given: '%T'", err.Value)
case Unparsed:
return "not parsed yet"
case WrongRequest:
Expand Down
55 changes: 55 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,58 @@ func TestError_Error(t *testing.T) {
})
}
}

func Test_unsupportedType(t *testing.T) {
f := 1.
type args struct {
value interface{}
}
tests := []struct {
name string
args args
result string
}{
{
name: "nil",
args: args{
value: nil,
},
result: "unsupported type was given: '<nil>'",
},
{
name: "int",
args: args{
value: int(10),
},
result: "unsupported type was given: 'int'",
},
{
name: "float64",
args: args{
value: float64(10),
},
result: "unsupported type was given: 'float64'",
},
{
name: "*float64",
args: args{
value: &f,
},
result: "unsupported type was given: '*float64'",
},
{
name: "[]float64",
args: args{
value: []float64{1.},
},
result: "unsupported type was given: '[]float64'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := unsupportedType(tt.args.value); err.Error() != tt.result {
t.Errorf("unsupportedType() error = %v, wantErr %v", err, tt.result)
}
})
}
}
6 changes: 6 additions & 0 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ func ParseJSONPath(path string) (result []string, err error) {
// result, _ := ApplyJSONPath(node, commands)
//
func ApplyJSONPath(node *Node, commands []string) (result []*Node, err error) {
if node == nil {
return nil, nil
}
result = make([]*Node, 0)
var (
temporary []*Node
Expand Down Expand Up @@ -585,6 +588,9 @@ func Eval(node *Node, cmd string) (result *Node, err error) {
}

func eval(node *Node, expression rpn, cmd string) (result *Node, err error) {
if node == nil {
return nil, nil
}
var (
stack = make([]*Node, 0)
slice []*Node
Expand Down
14 changes: 12 additions & 2 deletions jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,13 @@ func TestEval(t *testing.T) {
expected: NumericNode("", 18),
wantErr: false,
},
{
name: "nil",
root: nil,
eval: "round(avg($..price)+pi)",
expected: nil,
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -698,11 +705,14 @@ func TestEval(t *testing.T) {
if test.wantErr {
return
}
if result == nil {
if test.expected != nil && result == nil {
t.Errorf("Eval() result in nil")
return
}

if test.expected == nil && result == nil {
return
}
if ok, err := result.Eq(test.expected); !ok {
t.Errorf("result.Eq(): wrong result:\nExpected: %#+v\nActual: %#+v", test.expected, result.value.Load())
} else if err != nil {
Expand Down Expand Up @@ -1431,7 +1441,7 @@ func TestApplyJSONPath(t *testing.T) {
node: nil,
commands: nil,
},
wantResult: make([]*Node, 0),
wantResult: nil,
wantErr: false,
},
{
Expand Down
Loading

0 comments on commit 376eb0b

Please sign in to comment.