Skip to content

Commit 8f8fa7e

Browse files
committed
migrate YAML package to github.com/goccy/go-yaml
1 parent 2c70962 commit 8f8fa7e

File tree

9 files changed

+26
-159
lines changed

9 files changed

+26
-159
lines changed

cli/cli_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/goccy/go-yaml"
910
"github.com/google/go-cmp/cmp"
10-
"gopkg.in/yaml.v3"
1111
)
1212

1313
func init() {

cli/error.go

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"unicode/utf8"
1111

12+
"github.com/goccy/go-yaml"
1213
"github.com/mattn/go-runewidth"
1314

1415
"github.com/itchyny/gojq"
@@ -121,18 +122,14 @@ type yamlParseError struct {
121122
}
122123

123124
func (err *yamlParseError) Error() string {
124-
var line int
125-
msg := strings.TrimPrefix(
126-
strings.TrimPrefix(err.err.Error(), "yaml: "),
127-
"unmarshal errors:\n ")
128-
if _, e := fmt.Sscanf(msg, "line %d: ", &line); e != nil {
129-
return "invalid yaml: " + err.fname
125+
var offset int
126+
var e *yaml.SyntaxError
127+
if errors.As(err.err, &e) {
128+
offset = e.Token.Position.Offset
130129
}
131-
_, msg, _ = strings.Cut(msg, ": ")
132-
msg, _, _ = strings.Cut(msg, "\n")
133-
linestr := getLineByLine(err.contents, line)
130+
linestr, line, column := getLineByOffset(err.contents, offset)
134131
return fmt.Sprintf("invalid yaml: %s:%d\n%s %s",
135-
err.fname, line, formatLineInfo(linestr, line, 0), msg)
132+
err.fname, line, formatLineInfo(linestr, line, column), e.Message)
136133
}
137134

138135
func getLineByOffset(str string, offset int) (linestr string, line, column int) {
@@ -166,24 +163,6 @@ func getLineByOffset(str string, offset int) (linestr string, line, column int)
166163
return
167164
}
168165

169-
func getLineByLine(str string, line int) (linestr string) {
170-
ss := &stringScanner{str, 0}
171-
for {
172-
str, _, ok := ss.next()
173-
if !ok {
174-
break
175-
}
176-
if line--; line == 0 {
177-
linestr = str
178-
break
179-
}
180-
}
181-
if len(linestr) > 64 {
182-
linestr = trimLastInvalidRune(linestr[:64])
183-
}
184-
return
185-
}
186-
187166
func trimLastInvalidRune(s string) string {
188167
for i := len(s) - 1; i >= 0 && i > len(s)-utf8.UTFMax; i-- {
189168
if b := s[i]; b < utf8.RuneSelf {

cli/error_test.go

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -170,74 +170,3 @@ func TestGetLineByOffset(t *testing.T) {
170170
})
171171
}
172172
}
173-
174-
func TestGetLineByLine(t *testing.T) {
175-
testCases := []struct {
176-
str string
177-
line int
178-
linestr string
179-
}{
180-
{
181-
"", 0,
182-
"",
183-
},
184-
{
185-
"abc", -1,
186-
"",
187-
},
188-
{
189-
"abc", 0,
190-
"",
191-
},
192-
{
193-
"abc", 1,
194-
"abc",
195-
},
196-
{
197-
"abc\n", 1,
198-
"abc",
199-
},
200-
{
201-
"abc", 2,
202-
"",
203-
},
204-
{
205-
"abc\n", 2,
206-
"",
207-
},
208-
{
209-
"abc\ndef\nghi", 1,
210-
"abc",
211-
},
212-
{
213-
"abc\ndef\nghi", 2,
214-
"def",
215-
},
216-
{
217-
"abc\rdef\rghi", 2,
218-
"def",
219-
},
220-
{
221-
"abc\r\ndef\r\nghi", 2,
222-
"def",
223-
},
224-
{
225-
"abc\ndef\nghi", 3,
226-
"ghi",
227-
},
228-
{
229-
"abc\ndef\nghi", 4,
230-
"",
231-
},
232-
}
233-
for _, tc := range testCases {
234-
t.Run(fmt.Sprintf("%q,%d", tc.str, tc.line), func(t *testing.T) {
235-
linestr := getLineByLine(tc.str, tc.line)
236-
if linestr != tc.linestr {
237-
t.Errorf("getLineByLine(%q, %d):\n"+
238-
" got: %q\n"+
239-
"expected: %q", tc.str, tc.line, linestr, tc.linestr)
240-
}
241-
})
242-
}
243-
}

cli/inputs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os"
99
"strings"
1010

11-
"gopkg.in/yaml.v3"
11+
"github.com/goccy/go-yaml"
1212

1313
"github.com/itchyny/gojq"
1414
)
@@ -287,7 +287,7 @@ func (i *yamlInputIter) Next() (any, bool) {
287287
i.err = &yamlParseError{i.fname, i.ir.getContents(nil, nil), err}
288288
return i.err, true
289289
}
290-
return normalizeYAML(v), true
290+
return v, true
291291
}
292292

293293
func (i *yamlInputIter) Close() error {

cli/marshaler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"io"
66
"strings"
77

8-
"gopkg.in/yaml.v3"
8+
"github.com/goccy/go-yaml"
99
)
1010

1111
type marshaler interface {
@@ -37,12 +37,12 @@ type yamlMarshaler struct {
3737
}
3838

3939
func (m *yamlMarshaler) marshal(v any, w io.Writer) error {
40-
enc := yaml.NewEncoder(w)
40+
indent := 2
4141
if i := m.indent; i != nil {
42-
enc.SetIndent(*i)
43-
} else {
44-
enc.SetIndent(2)
42+
indent = *i
4543
}
44+
_, isArray := v.([]any) // https://github.com/goccy/go-yaml/issues/291
45+
enc := yaml.NewEncoder(w, yaml.Indent(indent), yaml.IndentSequence(!isArray))
4646
if err := enc.Encode(v); err != nil {
4747
return err
4848
}

cli/test.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7336,11 +7336,11 @@
73367336
foo: 2021-01-02
73377337
bar: 2021-01-02t10:11:12Z
73387338
baz: "2021-01-02"
7339-
expected: | # it is impossible to keep the original timestamp strings
7339+
expected: |
73407340
{
7341-
"bar": "2021-01-02T10:11:12Z",
7341+
"bar": "2021-01-02t10:11:12Z",
73427342
"baz": "2021-01-02",
7343-
"foo": "2021-01-02T00:00:00Z"
7343+
"foo": "2021-01-02"
73447344
}
73457345
73467346
- name: yaml input option with slurp option
@@ -7397,7 +7397,7 @@
73977397
error: |
73987398
invalid yaml: <stdin>:2
73997399
2 | 10
7400-
^ could not find expected ':'
7400+
^ non-map value is specified
74017401
74027402
- name: yaml input option multiple errors
74037403
args:
@@ -7410,7 +7410,7 @@
74107410
error: |
74117411
invalid yaml: <stdin>:3
74127412
3 | foo: 3
7413-
^ mapping key "foo" already defined at line 1
7413+
^ mapping key "foo" already defined at [1:1]
74147414
74157415
- name: yaml file input error
74167416
args:
@@ -7420,7 +7420,7 @@
74207420
error: |
74217421
invalid yaml: testdata/2.yaml:5
74227422
5 | a: b: c
7423-
^ mapping values are not allowed in this context
7423+
^ mapping value is not allowed in this context
74247424
74257425
- name: yaml file input error in dos format
74267426
args:
@@ -7430,7 +7430,7 @@
74307430
error: |
74317431
invalid yaml: testdata/3.yaml:5
74327432
5 | a: b: c
7433-
^ mapping values are not allowed in this context
7433+
^ mapping value is not allowed in this context
74347434
74357435
- name: yaml file input error in mac format
74367436
args:
@@ -7440,7 +7440,7 @@
74407440
error: |
74417441
invalid yaml: testdata/4.yaml:5
74427442
5 | a: b: c
7443-
^ mapping values are not allowed in this context
7443+
^ mapping value is not allowed in this context
74447444
74457445
- name: yaml output option
74467446
args:

cli/yaml.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module github.com/itchyny/gojq
33
go 1.22
44

55
require (
6+
github.com/goccy/go-yaml v1.17.1
67
github.com/google/go-cmp v0.5.4
78
github.com/itchyny/timefmt-go v0.1.6
89
github.com/mattn/go-isatty v0.0.20
910
github.com/mattn/go-runewidth v0.0.15
10-
gopkg.in/yaml.v3 v3.0.1
1111
)
1212

1313
require (

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
2+
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
13
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
24
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
35
github.com/itchyny/timefmt-go v0.1.6 h1:ia3s54iciXDdzWzwaVKXZPbiXzxxnv1SPGFfM/myJ5Q=
@@ -14,7 +16,3 @@ golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
1416
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1517
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
1618
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
17-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
18-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
20-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)