-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a concrete type for JSON pointer
WARNING: This commit includes breaking changes. Declare a jsontext.Pointer type as a named string type. This allows us to implement a Tokens method to conveniently iterate over all the reference tokens in the pointer using the upcoming iterators support. The new functionally is currently not tested by CI, but can be manually tested with: GOEXPERIMENT=rangefunc go.tip test ./...
- Loading branch information
Showing
10 changed files
with
112 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build goexperiment.rangefunc | ||
|
||
package jsontext | ||
|
||
import "iter" | ||
|
||
// Tokens returns an iterator for each reference token in the JSON pointer, | ||
// starting from the first token until the last token (unless stopped early). | ||
// A token is either a JSON object name or an index to a JSON array element | ||
// encoded as a base-10 integer value. | ||
func (p Pointer) Tokens() iter.Seq[string] { | ||
return func(yield func(string) bool) { | ||
for len(p) > 0 { | ||
if !yield(p.nextToken()) { | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build goexperiment.rangefunc | ||
|
||
package jsontext | ||
|
||
import ( | ||
"iter" | ||
"slices" | ||
"testing" | ||
) | ||
|
||
func TestPointerTokens(t *testing.T) { | ||
// TODO(https://go.dev/issue/61899): Use slices.Collect. | ||
collect := func(seq iter.Seq[string]) (x []string) { | ||
for v := range seq { | ||
x = append(x, v) | ||
} | ||
return x | ||
} | ||
|
||
tests := []struct { | ||
in Pointer | ||
want []string | ||
}{ | ||
{in: "", want: nil}, | ||
{in: "a", want: []string{"a"}}, | ||
{in: "~", want: []string{"~"}}, | ||
{in: "/a", want: []string{"a"}}, | ||
{in: "/foo/bar", want: []string{"foo", "bar"}}, | ||
{in: "///", want: []string{"", "", ""}}, | ||
{in: "/~0~1", want: []string{"~/"}}, | ||
} | ||
for _, tt := range tests { | ||
got := collect(tt.in.Tokens()) | ||
if !slices.Equal(got, tt.want) { | ||
t.Errorf("Pointer(%q).Tokens = %q, want %q", tt.in, got, tt.want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters