Skip to content

Commit

Permalink
Fix error handling in cmd (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
spyzhov authored Mar 14, 2024
1 parent d59d485 commit 2f5aaea
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3.4.0
uses: golangci/golangci-lint-action@v3.7.0
with:
version: v1.51.2
version: v1.56.2
args: -v

test:
strategy:
matrix:
go-version: [ '1.16.x', '1.17.x', '1.18.x', '1.19.x', '1.20.x' ]
go-version: [ '1.16.x', '1.17.x', '1.18.x', '1.19.x', '1.20.x' , '1.21.x' , '1.22.x' ]
os: [ ubuntu-latest, macos-latest ]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -47,10 +47,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go 1.20.x on ubuntu-latest
- name: Setup Go 1.22.x on ubuntu-latest
uses: actions/setup-go@v3
with:
go-version: '1.20.x'
go-version: '1.22.x'
- name: Setup Dependencies
run: |
go get golang.org/x/tools/cmd/cover
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/[email protected].0
go get github.com/spyzhov/ajson/cmd/[email protected].1
```

Usage:
Expand Down
27 changes: 17 additions & 10 deletions cmd/ajson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/spyzhov/ajson"
)

var version = "v0.9.0"
var version = "v0.9.1"

func usage() {
text := ``
Expand Down Expand Up @@ -61,42 +61,49 @@ func main() {

if cfg.multiline {
reader := bufio.NewReader(input)
line := 1
for {
data, err := reader.ReadBytes('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
mlError(cfg, "unable to read input: %s", err)
mlError(cfg, "unable to read input at line %d: %s", line, err)
}
return
}
apply(cfg, data)
apply(cfg, data, line)
line++
}
} else {
data, err := io.ReadAll(input)
if err != nil {
pFatal("error reading source: %s", err)
}
apply(cfg, data)
apply(cfg, data, 0)
}
}

func apply(cfg config, data []byte) {
func apply(cfg config, data []byte, line int) {
var result *ajson.Node
msg := "error"
if cfg.multiline {
msg = fmt.Sprintf("error at line %d", line)
}

root, err := ajson.Unmarshal(data)
if err != nil {
mlFatal(cfg, "error parsing JSON: %s", err)
mlFatal(cfg, "%s parsing JSON: %s", msg, err)
return
}

var nodes []*ajson.Node
nodes, err = root.JSONPath(cfg.jsonpath)
result = ajson.ArrayNode("", nodes)
if err != nil {
if err != nil { // try to eval
result, err = ajson.Eval(root, cfg.jsonpath)
} else {
result = ajson.ArrayNode("", nodes)
}
if err != nil {
mlFatal(cfg, "jsonpath error: %s", err)
mlFatal(cfg, "jsonpath %s: %s", msg, err)
return
}

Expand All @@ -114,7 +121,7 @@ func apply(cfg config, data []byte) {

data, err = ajson.Marshal(result)
if err != nil {
mlFatal(cfg, "error preparing JSON: %s", err)
mlFatal(cfg, "%s preparing JSON: %s", msg, err)
}
pPrint("%s\n", data)
}
Expand Down
12 changes: 6 additions & 6 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ func ArrayNode(key string, value []*Node) (current *Node) {
}
current.children = make(map[string]*Node, len(value))
if value != nil {
current.value.Store(value)
for i, val := range value {
for i := range value {
var index = i
current.children[strconv.Itoa(i)] = val
val.parent = current
val.index = &index
current.children[strconv.Itoa(index)] = value[index]
value[index].parent = current
value[index].index = &index
}
current.value.Store(value)
}
return
}
Expand All @@ -132,12 +132,12 @@ func ObjectNode(key string, value map[string]*Node) (current *Node) {
dirty: true,
}
if value != nil {
current.value.Store(value)
for key, val := range value {
vkey := key
val.parent = current
val.key = &vkey
}
current.value.Store(value)
} else {
current.children = make(map[string]*Node)
}
Expand Down
2 changes: 1 addition & 1 deletion node_mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,5 @@ func (n *Node) setReference(parent *Node, key *string, index *int) {
temp := *key
n.key = &temp
}
n.index = index
n.index = cptri(index)
}
14 changes: 13 additions & 1 deletion node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func TestNode_Value_Simple(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
data := test.bytes
current := &Node{
_type: test._type,
borders: [2]int{0, len(test.bytes)},
data: &test.bytes,
data: &data,
}
value, err := current.getValue()
if err != nil {
Expand Down Expand Up @@ -1703,6 +1704,17 @@ func TestNode_Inheritors(t *testing.T) {
StringNode("str", "foo"),
},
},
{
name: "parsed array",
node: Must(Unmarshal([]byte(`[{"clientId":"qwerty","advice":{},"success":true}]`))),
expected: []*Node{
ObjectNode("", map[string]*Node{
"clientId": StringNode("clientId", "qwerty"),
"advice": ObjectNode("advice", map[string]*Node{}),
"success": BoolNode("success", true),
}),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit 2f5aaea

Please sign in to comment.