Skip to content

Commit 221b339

Browse files
committed
Add the text and binary encoding interface methods
Also upgrade golangci-lint to v2.2.2.
1 parent 8db3053 commit 221b339

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

.golangci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ linters:
1212
- testpackage
1313
- varnamelen
1414
- wsl
15+
- wsl_v5
16+
- noinlineerr
1517
settings:
1618
dupword:
1719
ignore:

CHANGELOG.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,30 @@ All notable changes to this project will be documented in this file. It uses the
77
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
88
"Semantic Versioning 2.0.0"
99

10-
## [v0.9.0] — Unreleased
10+
## [v0.10.0] — Unreleased
11+
12+
### ⚡ Improvements
13+
14+
* Added text and binary encoding interface methods to `Path`:
15+
* [encoding.TextMarshaler]
16+
* [encoding.TextUnmarshaler]
17+
* [encoding.BinaryMarshaler]
18+
* [encoding.BinaryUnmarshaler]
19+
Thanks to @rkosegi for the suggestion ([#20])
20+
21+
### 📔 Notes
22+
23+
* Upgraded to `golangci-lint` v2.2.2.
24+
25+
[v0.10.0]: https://github.com/theory/jsonpath/compare/v0.9.0...v0.10.0
26+
[encoding.TextMarshaler]: https://pkg.go.dev/encoding#TextMarshaler
27+
[encoding.TextUnmarshaler]: https://pkg.go.dev/encoding#TextUnmarshaler
28+
[encoding.BinaryMarshaler]: https://pkg.go.dev/encoding#BinaryMarshaler
29+
[encoding.BinaryUnmarshaler]: https://pkg.go.dev/encoding#BinaryUnmarshaler
30+
[#20]: https://github.com/theory/jsonpath/issues/20
31+
"theory/jsonpath#20: Implement encoding.TextUnmarshaler in Path"
32+
33+
## [v0.9.0] — 2025-05-05
1134

1235
### ⚡ Improvements
1336

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ brew-lint-depends:
3838

3939
.PHONY: debian-lint-depends # Install linting tools on Debian
4040
debian-lint-depends:
41-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sudo sh -s -- -b /usr/bin v2.1.5
41+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sudo sh -s -- -b /usr/bin v2.2.2
4242

4343
.PHONY: install-generators # Install Go code generators
4444
install-generators:

path.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ func MustParse(path string) *Path {
3737
return NewParser().MustParse(path)
3838
}
3939

40+
// MarshalText encodes p into UTF-8-encoded text and returns the result.
41+
// Implements [encoding.TextMarshaler].
42+
func (p *Path) MarshalText() ([]byte, error) {
43+
return []byte(p.q.String()), nil
44+
}
45+
46+
// UnmarshalText decodes UTF-8-encoded text into p. Implements
47+
// [encoding.TextUnmarshaler].
48+
func (p *Path) UnmarshalText(data []byte) error {
49+
parsed, err := NewParser().Parse(string(data))
50+
if err != nil {
51+
return err
52+
}
53+
p.q = parsed.q
54+
return nil
55+
}
56+
57+
// MarshalBinary encodes p into UTF-8-encoded bytes and returns the result.
58+
// Implements [encoding.BinaryMarshaler].
59+
func (p *Path) MarshalBinary() ([]byte, error) {
60+
return p.MarshalText()
61+
}
62+
63+
// UnmarshalBinary decodes UTF-8-encoded bytes into p. Implements
64+
// [encoding.BinaryUnmarshaler].
65+
func (p *Path) UnmarshalBinary(data []byte) error {
66+
return p.UnmarshalText(data)
67+
}
68+
4069
// String returns a string representation of p.
4170
func (p *Path) String() string {
4271
return p.q.String()

path_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jsonpath
22

33
import (
4+
"encoding"
45
"encoding/json"
56
"fmt"
67
"os"
@@ -21,6 +22,7 @@ func book(idx int) spec.NormalizedPath {
2122
func TestParseSpecExamples(t *testing.T) {
2223
t.Parallel()
2324
a := assert.New(t)
25+
r := require.New(t)
2426
val := specExampleJSON(t)
2527
store, _ := val["store"].(map[string]any)
2628
books, _ := store["book"].([]any)
@@ -126,9 +128,35 @@ func TestParseSpecExamples(t *testing.T) {
126128
} {
127129
t.Run(tc.name, func(t *testing.T) {
128130
t.Parallel()
131+
132+
// Test parsing.
129133
p := MustParse(tc.path)
130134
a.Equal(p.q, p.Query())
131135
a.Equal(p.q.String(), p.String())
136+
137+
// Test text encoding.
138+
a.Implements((*encoding.TextMarshaler)(nil), p)
139+
a.Implements((*encoding.TextUnmarshaler)(nil), p)
140+
text, err := p.MarshalText()
141+
r.NoError(err)
142+
a.Equal(p.q.String(), string(text))
143+
p.q = nil
144+
r.NoError(p.UnmarshalText(text))
145+
a.Equal(p.q, p.Query())
146+
a.Equal(p.q.String(), p.String())
147+
148+
// Test binary encoding.
149+
a.Implements((*encoding.BinaryMarshaler)(nil), p)
150+
a.Implements((*encoding.BinaryUnmarshaler)(nil), p)
151+
data, err := p.MarshalBinary()
152+
r.NoError(err)
153+
a.Equal(p.q.String(), string(data))
154+
p.q = nil
155+
r.NoError(p.UnmarshalBinary(text))
156+
a.Equal(p.q, p.Query())
157+
a.Equal(p.q.String(), p.String())
158+
159+
// Test execution.
132160
res := p.Select(val)
133161
loc := p.SelectLocated(val)
134162

@@ -333,6 +361,20 @@ func TestParser(t *testing.T) {
333361
a.PanicsWithError(tc.err, func() { parser.MustParse(tc.path) })
334362
}
335363
}
364+
365+
// Test text and binary decoding.
366+
if tc.err == "" {
367+
p = &Path{}
368+
r.NoError(p.UnmarshalText([]byte(tc.path)))
369+
a.Equal(tc.exp, p)
370+
p = &Path{}
371+
r.NoError(p.UnmarshalBinary([]byte(tc.path)))
372+
a.Equal(tc.exp, p)
373+
} else {
374+
p = &Path{}
375+
r.EqualError(p.UnmarshalText([]byte(tc.path)), tc.err)
376+
r.EqualError(p.UnmarshalBinary([]byte(tc.path)), tc.err)
377+
}
336378
})
337379
}
338380
}

0 commit comments

Comments
 (0)