Skip to content

Commit

Permalink
Adding conv.ToInt64, conv.ToFloat64, and others...
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Oct 26, 2017
1 parent d82feb1 commit a54ea8a
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
98 changes: 98 additions & 0 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conv
import (
"fmt"
"log"
"math"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -108,3 +109,100 @@ func MustAtoi(s string) int {
i, _ := strconv.Atoi(s)
return i
}

// ToInt64 - taken from github.com/Masterminds/sprig
func ToInt64(v interface{}) int64 {
if str, ok := v.(string); ok {
iv, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0
}
return iv
}

val := reflect.Indirect(reflect.ValueOf(v))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return val.Int()
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return int64(val.Uint())
case reflect.Uint, reflect.Uint64:
tv := val.Uint()
if tv <= math.MaxInt64 {
return int64(tv)
}
// TODO: What is the sensible thing to do here?
return math.MaxInt64
case reflect.Float32, reflect.Float64:
return int64(val.Float())
case reflect.Bool:
if val.Bool() == true {
return 1
}
return 0
default:
return 0
}
}

// ToInt -
func ToInt(in interface{}) int {
return int(ToInt64(in))
}

// ToInt64s -
func ToInt64s(in ...interface{}) []int64 {
out := make([]int64, len(in))
for i, v := range in {
out[i] = ToInt64(v)
}
return out
}

// ToInts -
func ToInts(in ...interface{}) []int {
out := make([]int, len(in))
for i, v := range in {
out[i] = ToInt(v)
}
return out
}

// ToFloat64 - taken from github.com/Masterminds/sprig
func ToFloat64(v interface{}) float64 {
if str, ok := v.(string); ok {
iv, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return iv
}

val := reflect.Indirect(reflect.ValueOf(v))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return float64(val.Int())
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return float64(val.Uint())
case reflect.Uint, reflect.Uint64:
return float64(val.Uint())
case reflect.Float32, reflect.Float64:
return val.Float()
case reflect.Bool:
if val.Bool() == true {
return 1
}
return 0
default:
return 0
}
}

// ToFloat64s -
func ToFloat64s(in ...interface{}) []float64 {
out := make([]float64, len(in))
for i, v := range in {
out[i] = ToFloat64(v)
}
return out
}
36 changes: 35 additions & 1 deletion docs/content/functions/conv.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,38 @@ The number is less than 5
```console
$ NUMBER=21 gomplate < input.tmpl
The number is greater than 5
```
```

## `conv.ToInt64`

Converts the input to an `int64`.

```console
$ gomplate -i '{{conv.ToInt64 "9223372036854775807"}}'
9223372036854775807
```

## `conv.ToInt`

Converts the input to an `int`. This is similar to `conv.Atoi`, but handles booleans and numbers as well as strings.

```console
$ gomplate -i '{{conv.ToInt (gt 1 2)}}'
0
```

## `conv.ToInt64s`

Converts the inputs to an array of `int64`s

## `conv.ToInts`

Converts the inputs to an array of `int`s

## `conv.ToFloat64`

Converts the input to a `float64`

## `conv.ToFloat64s`

Converts the inputs to an array of `float64`s
24 changes: 24 additions & 0 deletions funcs/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,27 @@ func (f *ConvFuncs) Atoi(s string) int {
func (f *ConvFuncs) URL(s string) (*url.URL, error) {
return url.Parse(s)
}

func (f *ConvFuncs) ToInt64(in interface{}) int64 {
return conv.ToInt64(in)
}

func (f *ConvFuncs) ToInt(in interface{}) int {
return conv.ToInt(in)
}

func (f *ConvFuncs) ToInt64s(in ...interface{}) []int64 {
return conv.ToInt64s(in)
}

func (f *ConvFuncs) ToInts(in ...interface{}) []int {
return conv.ToInts(in)
}

func (f *ConvFuncs) ToFloat64(in interface{}) float64 {
return conv.ToFloat64(in)
}

func (f *ConvFuncs) ToFloat64s(in ...interface{}) []float64 {
return conv.ToFloat64s(in)
}

0 comments on commit a54ea8a

Please sign in to comment.