db/pkg/headers/helper.go

36 lines
601 B
Go
Raw Normal View History

2022-04-16 20:10:49 +00:00
package headers
// ByteHelper provides helping functions to less-fun-types
type SingleByteType interface {
bool | ~uint8
}
type BoolType interface {
bool
}
func ByteToBool(Input byte) bool {
if byte(1) == Input {
return true
}
return false
}
// ToSingleByte converts single-byte types, such as int8 and bool to single bytes.
func ToSingleByte[T SingleByteType](input T) byte {
switch input := any(input).(type) {
case uint8:
return byte(input)
case int8:
return byte(input)
case bool:
if input {
return byte(1)
} else {
return byte(0)
}
default:
return byte(-0)
}
}