Skip to content

Commit

Permalink
Define 0 float64 value as a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
madhuravi committed Jan 19, 2017
1 parent 59f427a commit 3651fc0
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions config/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ const (
Slice
// Dictionary contains words and their definitions
Dictionary
// Zero constants
_float64Zero = float64(0)
)

var _typeTimeDuration = reflect.TypeOf(time.Duration(0))
var (
_typeTimeDuration = reflect.TypeOf(time.Duration(0))
)

// GetType returns GO type of the provided object
func GetType(value interface{}) ValueType {
Expand Down Expand Up @@ -201,9 +205,8 @@ func (cv Value) TryAsBool() (bool, bool) {

// TryAsFloat attempts to return the configuration value as a float
func (cv Value) TryAsFloat() (float64, bool) {
f := float64(0)
v := cv.Value()
if val, err := convertValue(v, reflect.TypeOf(f)); v != nil && err == nil {
if val, err := convertValue(v, reflect.TypeOf(_float64Zero)); v != nil && err == nil {
return val.(float64), true
}
switch val := v.(type) {
Expand All @@ -216,7 +219,7 @@ func (cv Value) TryAsFloat() (float64, bool) {
case float32:
return float64(val), true
default:
return f, false
return _float64Zero, false
}
}

Expand Down

0 comments on commit 3651fc0

Please sign in to comment.