tape: Correctly decode into a table destination all the time

This commit is contained in:
Sasha Koshka 2025-10-13 10:33:59 -04:00
parent 4575fa229b
commit 5c2b8a0582

View File

@ -242,12 +242,22 @@ func decodeAnyOrError(decoder *Decoder, destination reflect.Value, tag Tag) (n i
}
lengthCast, err := Uint64ToIntSafe(length)
if err != nil { return n, err }
if isTypeAny(destination.Type()) {
// need a skeleton value if we are assigning to any.
value := reflect.MakeMapWithSize(reflect.TypeOf(dummyMap), lengthCast)
destination.Set(value)
destination = value
}
// im fucking so done dude. im so fucking done. this was
// supposed to only run when we need it but i guess it runs all
// the time, because when we get a map destination (a valid,
// allocated one) we break apart on SetMapIndex because of a nil
// map. yeah thats right. a fucking nil map panic. on the map we
// just allocated. but running this unconditionally (whether or
// not we receive an empty any value) actually makes it fucking
// work. go figure().
//
// (the map allocation functionality in skeletonPointer has been
// removed)
value := reflect.MakeMapWithSize(reflect.TypeOf(dummyMap), lengthCast)
destination.Set(value)
destination = value
destination.Clear()
for _ = range lengthCast {
key, nn, err := decoder.ReadUint16()
@ -355,12 +365,8 @@ func encodeAnyMap(encoder *Encoder, value any, tag Tag) (n int, err error) {
}
func canSet(destination reflect.Type, tag Tag) error {
fmt.Println("== CAN SET ====================")
fmt.Println(destination)
fmt.Println(tag)
// anything can be assigned to `any`
if isTypeAny(destination) {
fmt.Println("OK")
return nil
}
switch tag.WithoutCN() {
@ -401,7 +407,6 @@ func canSet(destination reflect.Type, tag Tag) error {
default:
return fmt.Errorf("unknown TN %d", tag.TN())
}
fmt.Println("OK")
return nil
}
@ -506,9 +511,6 @@ func skeletonPointer(decoder *Decoder, tag Tag) (reflect.Value, error) {
typ, err := typeOf(decoder, tag)
if err != nil { return reflect.Value { }, err }
value := reflect.New(typ)
if tag.Is(KTV) {
value.Elem().Set(reflect.MakeMap(typ))
}
return value, nil
}