I wrote a TLV handling package:
go.xrfang.cn/tlv, and encountered problem with type checking. Problematic code below:
1. The "Set" function in side tlv package:
package tlv
type (
ValueType interface {
string | []byte | ~float32 | ~float64 |
~int8 | ~int16 | ~int32 | ~int64 |
~uint8 | ~uint16 | ~uint32 | ~uint64
}
Prop map[byte][]byte
)
func Set[T ValueType](p Prop, tag byte, val T) Prop {
... ...
switch v := any(val).(type) {
case string:
... ...
default:
fmt.Printf("tlv.Set: '%T' not supported!\n", val)
}
...
}
2. event.go, which uses tlv:
package event
import (
"
go.xrfang.cn/tlv"
)
type (
Class byte
Event struct {
tlv.Prop
}
)
const (
TagClass = 3
)
func (e *Event) WithClass(cls Class) *Event {
(*e).Prop = tlv.Set((*e).Prop, TagClass, cls)
return e
}
The problem is, while the Class type is based on "byte", it should be accepted by tlv.Set(), which is the case (there is no compiler error). But when the code is running, it printed error message: tlv.Set: 'event.Class' not supported!