Introduced `lol.Tracer` for function entry/exit logging across various packages. This improves traceability and debugging of function executions while preserving existing behavior. Removed unused files `doc.go` and `nothing.go` to clean up the repository.
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// Package keys is a composable framework for constructing badger keys from
|
|
// fields of events.
|
|
package keys
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"realy.lol/lol"
|
|
)
|
|
|
|
// Element is an enveloper for a type that can Read and Write its binary form.
|
|
type Element interface {
|
|
// Write the binary form of the field into the given bytes.Buffer.
|
|
Write(buf io.Writer)
|
|
// Read accepts a bytes.Buffer and decodes a field from it.
|
|
Read(buf io.Reader) Element
|
|
// Len gives the length of the bytes output by the type.
|
|
Len() int
|
|
}
|
|
|
|
// Write the contents of each Element to a byte slice.
|
|
func Write(elems ...Element) (b []byte) {
|
|
lol.Tracer("Write", elems)
|
|
defer func() { lol.Tracer("end Write", b) }()
|
|
// get the length of the buffer required
|
|
var length int
|
|
for _, el := range elems {
|
|
length += el.Len()
|
|
}
|
|
buf := bytes.NewBuffer(make([]byte, 0, length))
|
|
// write out the data from each element
|
|
for _, el := range elems {
|
|
el.Write(buf)
|
|
}
|
|
b = buf.Bytes()
|
|
return
|
|
}
|
|
|
|
// Read the contents of a byte slice into the provided list of Element types.
|
|
func Read(b []byte, elems ...Element) {
|
|
lol.Tracer("Read", b, elems)
|
|
defer func() { lol.Tracer("end Read", b) }()
|
|
buf := bytes.NewBuffer(b)
|
|
for _, el := range elems {
|
|
el.Read(buf)
|
|
}
|
|
}
|
|
|
|
// Make is a convenience method to wrap a list of Element into a slice.
|
|
func Make(elems ...Element) []Element { return elems }
|