这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func (b *PathBuffer) With(s string) string {
return tmp
}

// WithIndex is short for push index, convert to string, and pop. This is useful
// when you want the location of the index given a path buffer as a prefix.
//
// pb.Push("foo")
// pb.WithIndex(1) // return foo[1]
func (b *PathBuffer) WithIndex(i int) string {
b.PushIndex(i)
tmp := b.String()
b.Pop()
return tmp
}

// Len returns the length of the current path.
func (b *PathBuffer) Len() int {
return b.off
Expand Down
7 changes: 7 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1693,3 +1693,10 @@ func Test_validateWithDiscriminator(t *testing.T) {
})
}
}

func TestPathBuffer_WithIndex_ReturnsExpectedString(t *testing.T) {
pb := huma.NewPathBuffer([]byte(""), 0)
pb.Push("prefix")

assert.Equal(t, "prefix[1]", pb.WithIndex(1))
}
Loading