summaryrefslogtreecommitdiff
path: root/conv
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-04-21 10:47:19 -0400
committerGitHub <noreply@github.com>2018-04-21 10:47:19 -0400
commit49d64b9a3b6f5b2f0354fa691ccb9808bda584d9 (patch)
tree5bbe04e1437afd85a2cedbbf33cb7a8870572adf /conv
parent0465ece752b429c611c7628e986bebd984cfe3f6 (diff)
Linting subpackages too... (#302)
* Linting subpackages too... Signed-off-by: Dave Henderson <dhenderson@gmail.com> * Fixing lint issues Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'conv')
-rw-r--r--conv/conv.go64
1 files changed, 36 insertions, 28 deletions
diff --git a/conv/conv.go b/conv/conv.go
index fb12212a..cdc35462 100644
--- a/conv/conv.go
+++ b/conv/conv.go
@@ -120,20 +120,10 @@ func MustAtoi(s string) int {
return i
}
-// ToInt64 - taken from github.com/Masterminds/sprig
+// ToInt64 - convert input to an int64, if convertible. Otherwise, returns 0.
func ToInt64(v interface{}) int64 {
if str, ok := v.(string); ok {
- iv, err := strconv.ParseInt(str, 0, 64)
- if err != nil {
- // maybe it's a float?
- var fv float64
- fv, err = strconv.ParseFloat(str, 64)
- if err != nil {
- return 0
- }
- return ToInt64(fv)
- }
- return iv
+ return strToInt64(str)
}
val := reflect.Indirect(reflect.ValueOf(v))
@@ -150,7 +140,7 @@ func ToInt64(v interface{}) int64 {
case reflect.Float32, reflect.Float64:
return int64(val.Float())
case reflect.Bool:
- if val.Bool() == true {
+ if val.Bool() {
return 1
}
return 0
@@ -182,22 +172,10 @@ func ToInts(in ...interface{}) []int {
return out
}
-// ToFloat64 - taken from github.com/Masterminds/sprig
+// ToFloat64 - convert input to a float64, if convertible. Otherwise, returns 0.
func ToFloat64(v interface{}) float64 {
if str, ok := v.(string); ok {
- // this is inefficient, but it's the only way I can think of to
- // properly convert octal integers to floats
- iv, err := strconv.ParseInt(str, 0, 64)
- if err != nil {
- // ok maybe it's a float?
- var fv float64
- fv, err = strconv.ParseFloat(str, 64)
- if err != nil {
- return 0
- }
- return fv
- }
- return float64(iv)
+ return strToFloat64(str)
}
val := reflect.Indirect(reflect.ValueOf(v))
@@ -211,7 +189,7 @@ func ToFloat64(v interface{}) float64 {
case reflect.Float32, reflect.Float64:
return val.Float()
case reflect.Bool:
- if val.Bool() == true {
+ if val.Bool() {
return 1
}
return 0
@@ -220,6 +198,36 @@ func ToFloat64(v interface{}) float64 {
}
}
+func strToInt64(str string) int64 {
+ iv, err := strconv.ParseInt(str, 0, 64)
+ if err != nil {
+ // maybe it's a float?
+ var fv float64
+ fv, err = strconv.ParseFloat(str, 64)
+ if err != nil {
+ return 0
+ }
+ return ToInt64(fv)
+ }
+ return iv
+}
+
+func strToFloat64(str string) float64 {
+ // this is inefficient, but it's the only way I can think of to
+ // properly convert octal integers to floats
+ iv, err := strconv.ParseInt(str, 0, 64)
+ if err != nil {
+ // ok maybe it's a float?
+ var fv float64
+ fv, err = strconv.ParseFloat(str, 64)
+ if err != nil {
+ return 0
+ }
+ return fv
+ }
+ return float64(iv)
+}
+
// ToFloat64s -
func ToFloat64s(in ...interface{}) []float64 {
out := make([]float64, len(in))