Use string.EqualFold where necessary

This commit is contained in:
Adrian Gallagher
2019-06-04 17:27:00 +10:00
parent e965e54e09
commit 3010b62ac1
7 changed files with 157 additions and 157 deletions

View File

@@ -145,7 +145,7 @@ func IsValidCryptoAddress(address, crypto string) (bool, error) {
// YesOrNo returns a boolean variable to check if input is "y" or "yes"
func YesOrNo(input string) bool {
if strings.ToLower(input) == "y" || strings.ToLower(input) == "yes" {
if strings.EqualFold(input, "y") || strings.EqualFold(input, "yes") {
return true
}
return false

View File

@@ -1,55 +1,55 @@
package convert
import (
"fmt"
"strconv"
"time"
)
// FloatFromString format
func FloatFromString(raw interface{}) (float64, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
flt, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, fmt.Errorf("could not convert value: %s Error: %s", str, err)
}
return flt, nil
}
// IntFromString format
func IntFromString(raw interface{}) (int, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
n, err := strconv.Atoi(str)
if err != nil {
return 0, fmt.Errorf("unable to parse as int: %T", raw)
}
return n, nil
}
// Int64FromString format
func Int64FromString(raw interface{}) (int64, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
n, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0, fmt.Errorf("unable to parse as int64: %T", raw)
}
return n, nil
}
// TimeFromUnixTimestampFloat format
func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
ts, ok := raw.(float64)
if !ok {
return time.Time{}, fmt.Errorf("unable to parse, value not float64: %T", raw)
}
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
}
package convert
import (
"fmt"
"strconv"
"time"
)
// FloatFromString format
func FloatFromString(raw interface{}) (float64, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
flt, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, fmt.Errorf("could not convert value: %s Error: %s", str, err)
}
return flt, nil
}
// IntFromString format
func IntFromString(raw interface{}) (int, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
n, err := strconv.Atoi(str)
if err != nil {
return 0, fmt.Errorf("unable to parse as int: %T", raw)
}
return n, nil
}
// Int64FromString format
func Int64FromString(raw interface{}) (int64, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
n, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0, fmt.Errorf("unable to parse as int64: %T", raw)
}
return n, nil
}
// TimeFromUnixTimestampFloat format
func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
ts, ok := raw.(float64)
if !ok {
return time.Time{}, fmt.Errorf("unable to parse, value not float64: %T", raw)
}
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
}

View File

@@ -1,96 +1,96 @@
package convert
import (
"testing"
"time"
)
func TestFloatFromString(t *testing.T) {
t.Parallel()
testString := "1.41421356237"
expectedOutput := float64(1.41421356237)
actualOutput, err := FloatFromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Test failed. Common FloatFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = FloatFromString(testByte)
if err == nil {
t.Error("Test failed. Common FloatFromString. Converted non-string.")
}
testString = " something unconvertible "
_, err = FloatFromString(testString)
if err == nil {
t.Error("Test failed. Common FloatFromString. Converted invalid syntax.")
}
}
func TestIntFromString(t *testing.T) {
t.Parallel()
testString := "1337"
expectedOutput := 1337
actualOutput, err := IntFromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Test failed. Common IntFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = IntFromString(testByte)
if err == nil {
t.Error("Test failed. Common IntFromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = IntFromString(testString)
if err == nil {
t.Error("Test failed. Common IntFromString. Converted invalid syntax.")
}
}
func TestInt64FromString(t *testing.T) {
t.Parallel()
testString := "4398046511104"
expectedOutput := int64(1 << 42)
actualOutput, err := Int64FromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Test failed. Common Int64FromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = Int64FromString(testByte)
if err == nil {
t.Error("Test failed. Common Int64FromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = Int64FromString(testString)
if err == nil {
t.Error("Test failed. Common Int64FromString. Converted invalid syntax.")
}
}
func TestTimeFromUnixTimestampFloat(t *testing.T) {
t.Parallel()
testTimestamp := float64(1414456320000)
expectedOutput := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
actualOutput, err := TimeFromUnixTimestampFloat(testTimestamp)
if actualOutput.UTC().String() != expectedOutput.UTC().String() || err != nil {
t.Errorf("Test failed. Common TimeFromUnixTimestampFloat. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
testString := "Time"
_, err = TimeFromUnixTimestampFloat(testString)
if err == nil {
t.Error("Test failed. Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
}
}
package convert
import (
"testing"
"time"
)
func TestFloatFromString(t *testing.T) {
t.Parallel()
testString := "1.41421356237"
expectedOutput := float64(1.41421356237)
actualOutput, err := FloatFromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Test failed. Common FloatFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = FloatFromString(testByte)
if err == nil {
t.Error("Test failed. Common FloatFromString. Converted non-string.")
}
testString = " something unconvertible "
_, err = FloatFromString(testString)
if err == nil {
t.Error("Test failed. Common FloatFromString. Converted invalid syntax.")
}
}
func TestIntFromString(t *testing.T) {
t.Parallel()
testString := "1337"
expectedOutput := 1337
actualOutput, err := IntFromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Test failed. Common IntFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = IntFromString(testByte)
if err == nil {
t.Error("Test failed. Common IntFromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = IntFromString(testString)
if err == nil {
t.Error("Test failed. Common IntFromString. Converted invalid syntax.")
}
}
func TestInt64FromString(t *testing.T) {
t.Parallel()
testString := "4398046511104"
expectedOutput := int64(1 << 42)
actualOutput, err := Int64FromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Test failed. Common Int64FromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = Int64FromString(testByte)
if err == nil {
t.Error("Test failed. Common Int64FromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = Int64FromString(testString)
if err == nil {
t.Error("Test failed. Common Int64FromString. Converted invalid syntax.")
}
}
func TestTimeFromUnixTimestampFloat(t *testing.T) {
t.Parallel()
testTimestamp := float64(1414456320000)
expectedOutput := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
actualOutput, err := TimeFromUnixTimestampFloat(testTimestamp)
if actualOutput.UTC().String() != expectedOutput.UTC().String() || err != nil {
t.Errorf("Test failed. Common TimeFromUnixTimestampFloat. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
testString := "Time"
_, err = TimeFromUnixTimestampFloat(testString)
if err == nil {
t.Error("Test failed. Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
}
}