mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add tests for comman GetAnyAs functions
This commit is contained in:
@@ -27,12 +27,55 @@ func GetAnyAsInt(value any) (int, bool) {
|
||||
return int(byteValue), true
|
||||
}
|
||||
|
||||
floatValue, ok := value.(float64)
|
||||
float32Value, ok := value.(float32)
|
||||
if ok {
|
||||
if floatValue != math.Floor(floatValue) {
|
||||
if float64(float32Value) != math.Floor(float64(float32Value)) {
|
||||
return 0, false
|
||||
}
|
||||
return int(floatValue), true
|
||||
return int(float32Value), true
|
||||
}
|
||||
|
||||
float64Value, ok := value.(float64)
|
||||
if ok {
|
||||
if float64Value != math.Floor(float64Value) {
|
||||
return 0, false
|
||||
}
|
||||
return int(float64Value), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func GetAnyAsByte(value any) (byte, bool) {
|
||||
|
||||
byteValue, ok := value.(byte)
|
||||
if ok {
|
||||
return byte(byteValue), true
|
||||
}
|
||||
|
||||
intValue, ok := value.(int)
|
||||
if ok {
|
||||
return byte(intValue), true
|
||||
}
|
||||
|
||||
uintValue, ok := value.(uint)
|
||||
if ok {
|
||||
return byte(uintValue), true
|
||||
}
|
||||
|
||||
float32Value, ok := value.(float32)
|
||||
if ok {
|
||||
if float64(float32Value) != math.Floor(float64(float32Value)) {
|
||||
return 0, false
|
||||
}
|
||||
return byte(float32Value), true
|
||||
}
|
||||
|
||||
float64Value, ok := value.(float64)
|
||||
if ok {
|
||||
if float64Value != math.Floor(float64Value) {
|
||||
return 0, false
|
||||
}
|
||||
return byte(float64Value), true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
@@ -46,39 +89,11 @@ func GetAnyAsByteSlice(value any) ([]byte, bool) {
|
||||
result := make([]byte, v.Len())
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
elem := v.Index(i).Interface()
|
||||
byteValue, ok := elem.(byte)
|
||||
if ok {
|
||||
result[i] = byteValue
|
||||
continue
|
||||
elemValue, ok := GetAnyAsByte(elem)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
uintValue, ok := elem.(uint)
|
||||
if ok {
|
||||
if uintValue > 255 {
|
||||
return nil, false
|
||||
}
|
||||
result[i] = byte(uintValue)
|
||||
continue
|
||||
}
|
||||
intValue, ok := elem.(int)
|
||||
if ok {
|
||||
if intValue < 0 || intValue > 255 {
|
||||
return nil, false
|
||||
}
|
||||
result[i] = byte(intValue)
|
||||
continue
|
||||
}
|
||||
floatValue, ok := elem.(float64)
|
||||
if ok {
|
||||
if floatValue != math.Floor(floatValue) {
|
||||
return nil, false
|
||||
}
|
||||
if floatValue < 0 || floatValue > 255 {
|
||||
return nil, false
|
||||
}
|
||||
result[i] = byte(floatValue)
|
||||
continue
|
||||
}
|
||||
return nil, false
|
||||
result[i] = elemValue
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
@@ -92,30 +107,11 @@ func GetAnyAsIntSlice(value any) ([]int, bool) {
|
||||
result := make([]int, v.Len())
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
elem := v.Index(i).Interface()
|
||||
byteValue, ok := elem.(byte)
|
||||
if ok {
|
||||
result[i] = int(byteValue)
|
||||
continue
|
||||
elemInt, ok := GetAnyAsInt(elem)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
uintValue, ok := elem.(uint)
|
||||
if ok {
|
||||
result[i] = int(uintValue)
|
||||
continue
|
||||
}
|
||||
intValue, ok := elem.(int)
|
||||
if ok {
|
||||
result[i] = int(intValue)
|
||||
continue
|
||||
}
|
||||
floatValue, ok := elem.(float64)
|
||||
if ok {
|
||||
if floatValue != math.Floor(floatValue) {
|
||||
return nil, false
|
||||
}
|
||||
result[i] = int(floatValue)
|
||||
continue
|
||||
}
|
||||
return nil, false
|
||||
result[i] = elemInt
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user