Open abourget opened 6 years ago
The custom Size() method is on an instance of the type, so you'd just need to read the instance value to see what size it is and return that.
Unpack Size() only seems to matter for slices, Custom unpack happens here: https://github.com/lunixbochs/struc/blob/master/fields.go#L150
e.g. https://github.com/lunixbochs/struc/blob/master/custom_float16.go#L73 could read:
func (f *Float16) Size(opt *Options) int {
if *f < 128 {
return 1
} else {
return 2
}
}
but when Size
is called, you don't have the data.. so you can't return a variable length.. like a varint would do. Does that make sense ?
Size is not called on Unpack. You have the data on Pack. I know exactly how varint works.
Seems to work fine for me (note, you shouldn't use Pack to get the size, I just didn't bother looking up the thresholds to hardcode it):
package struc
import (
"encoding/binary"
"io"
"strconv"
)
type ByteWrapper struct {
io.Reader
}
func (w *ByteWrapper) ReadByte() (byte, error) {
var b [1]byte
_, err := w.Read(b[:])
return b[0], err
}
type Varint uint64
func (v *Varint) Pack(p []byte, opt *Options) (int, error) {
return binary.PutUvarint(p, uint64(*v)), nil
}
func (v *Varint) Unpack(r io.Reader, length int, opt *Options) error {
n, err := binary.ReadUvarint(&ByteWrapper{r})
*v = Varint(n)
return err
}
func (v *Varint) Size(opt *Options) int {
var buf [8]byte
return binary.PutUvarint(buf[:], uint64(*v))
}
func (v *Varint) String() string {
return strconv.FormatUint(uint64(*v), 10)
}
Ohh I see.. I'll see if I can take a stab at it. Thanks a lot !
@abourget did you by any chance figure out how to integrate varints?
Approximately the code I posted should already work in your own codebase without modifying struc.
I was trying to integrate a
varint32
type (like https://github.com/Smerity/govarint/blob/master/govarint.go#L188:6 ..) by adding a type totypes.go
but I see a Custom declaration needs aSize()
which returns a constant..Do you think it would be easy to modify the lib to support a varint32 type? The size of the read is never defined beforehand.. it could be one byte, two, three, etc..
thanks!