Add more robust value comparison
This commit is contained in:
33
upnp/utils_binary.go
Normal file
33
upnp/utils_binary.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package upnp
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// toBinary tries to convert v into a []byte.
|
||||
// - if v is []byte, returns it directly
|
||||
// - if v is string, attempts base64 decode, then hex decode
|
||||
func toBinary(v interface{}) ([]byte, error) {
|
||||
switch val := v.(type) {
|
||||
case []byte:
|
||||
return val, nil
|
||||
|
||||
case string:
|
||||
// Try base64 first
|
||||
if data, err := base64.StdEncoding.DecodeString(val); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Try hex next
|
||||
if data, err := hex.DecodeString(val); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("cannot parse string as base64 or hex: %q", val)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("cannot convert type %T to binary", v)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user