-
Notifications
You must be signed in to change notification settings - Fork 23
Use a concrete type for JSON pointer #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at this point I would personally use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At present, the |
||
|
||
package jsontext | ||
|
||
import "iter" | ||
|
||
// Tokens returns an iterator over the reference tokens 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 | ||
} | ||
} | ||
} | ||
} |
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) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.