Closed trim21 closed 3 months ago
I would be surprised if it doesn't already support that. It seems reasonable to allow it to any type that can unmarshal from bytes, or has UnmarshalText or UnmarshalBytes. There's https://pkg.go.dev/github.com/anacrolix/torrent/bencode#Unmarshaler, but those other methods should be checked for byte strings.
I would be surprised if it doesn't already support that
I'm pretty sure it doesn't
package main
import (
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/metainfo"
"github.com/samber/lo"
)
type scrapeResponse struct {
Files map[metainfo.Hash]scrapeResponseFile `bencode:"files"`
}
type scrapeResponseFile struct {
Complete int `bencode:"complete"`
Downloaded int `bencode:"downloaded"`
Incomplete int `bencode:"incomplete"`
}
func main() {
var s scrapeResponse
lo.Must0(bencode.Unmarshal([]byte("d5:filesd20:\x05a~F\xfd{c\xd1`\xb8\xd9\x89\xceM\xb9t\x1d\\\x0b\xded9:completedi1e10:downloadedi1eeee"), &s))
}
panic: reflect.Value.Convert: value of type string cannot be converted to type infohash.T [recovered]
panic: reflect.Value.Convert: value of type string cannot be converted to type infohash.T
Thanks. Would you like to have a crack at a unit test/implementation?
I'll try
Thanks for submitting this. The fix was not easy.
I forgot the tests, I've force pushed those, see the referenced commit.
I'm trying to decode scrape response, which looks like this
can we support decoding bencode string to byte array with length check?