Compare commits

...

3 Commits

2 changed files with 22 additions and 11 deletions

View File

@ -24,7 +24,7 @@ const preamble = `
const static = ` const static = `
// Table is a KTV table with an undefined schema. // Table is a KTV table with an undefined schema.
type Table map[uint16] any type Table = map[uint16] any
// Message is any message that can be sent along this protocol. // Message is any message that can be sent along this protocol.
type Message interface { type Message interface {

View File

@ -242,12 +242,22 @@ func decodeAnyOrError(decoder *Decoder, destination reflect.Value, tag Tag) (n i
} }
lengthCast, err := Uint64ToIntSafe(length) lengthCast, err := Uint64ToIntSafe(length)
if err != nil { return n, err } if err != nil { return n, err }
if isTypeAny(destination.Type()) {
// need a skeleton value if we are assigning to any. // im fucking so done dude. im so fucking done. this was
value := reflect.MakeMapWithSize(reflect.TypeOf(dummyMap), lengthCast) // supposed to only run when we need it but i guess it runs all
destination.Set(value) // the time, because when we get a map destination (a valid,
destination = value // 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() destination.Clear()
for _ = range lengthCast { for _ = range lengthCast {
key, nn, err := decoder.ReadUint16() key, nn, err := decoder.ReadUint16()
@ -387,7 +397,11 @@ func canSet(destination reflect.Type, tag Tag) error {
return errCantAssignf("cannot assign array to %v", destination) return errCantAssignf("cannot assign array to %v", destination)
} }
case KTV: case KTV:
if destination != reflect.TypeOf(dummyMap) { cantAssign :=
destination.Kind() != reflect.Map ||
destination.Key().Kind() != reflect.Uint16 ||
!isTypeAny(destination.Elem())
if cantAssign {
return errCantAssignf("cannot assign table to %v", destination) return errCantAssignf("cannot assign table to %v", destination)
} }
default: default:
@ -497,9 +511,6 @@ func skeletonPointer(decoder *Decoder, tag Tag) (reflect.Value, error) {
typ, err := typeOf(decoder, tag) typ, err := typeOf(decoder, tag)
if err != nil { return reflect.Value { }, err } if err != nil { return reflect.Value { }, err }
value := reflect.New(typ) value := reflect.New(typ)
if tag.Is(KTV) {
value.Elem().Set(reflect.MakeMap(typ))
}
return value, nil return value, nil
} }