-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
34 lines (28 loc) · 940 Bytes
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package valley
// InitialPathSize sets the default size of a new Path's internal buffer.
var InitialPathSize = 32
// Path is used to represent the current position in a structure, to output a useful field value to
// identify where a ConstraintViolation occurred.
type Path struct {
buf []byte
}
// NewPath returns a new Path instance.
func NewPath() *Path {
return &Path{
buf: make([]byte, 0, InitialPathSize),
}
}
// Write appends the given string to the end of the internal buffer.
func (r *Path) Write(in string) int {
r.buf = append(r.buf, in...)
return len(in)
}
// TruncateRight cuts n bytes off of the end of the buffer. The backing array for the buffer does
// not shrink, meaning we can re-use that memory if we need to.
func (r *Path) TruncateRight(n int) {
r.buf = r.buf[:len(r.buf)-n]
}
// String renders this path as a string, to be sent to the frontend.
func (r *Path) String() string {
return string(r.buf)
}