Browse Source

move conversions over to primative

pull/16/head
Brett Langdon 11 years ago
parent
commit
155eed44c6
2 changed files with 81 additions and 93 deletions
  1. +0
    -89
      conversions.go
  2. +81
    -4
      primative.go

+ 0
- 89
conversions.go View File

@ -1,89 +0,0 @@
package forge
import (
"errors"
"fmt"
"math"
"strconv"
)
func asBoolean(value interface{}) (bool, error) {
switch val := value.(type) {
case bool:
return val, nil
case float64:
return val != 0, nil
case int64:
return val != 0, nil
case nil:
return false, nil
case string:
return val != "", nil
}
msg := fmt.Sprintf("Could not convert value %s to type BOOLEAN", value)
return false, errors.New(msg)
}
func asFloat(value interface{}) (float64, error) {
switch val := value.(type) {
case bool:
v := float64(0)
if val {
v = float64(1)
}
return v, nil
case float64:
return val, nil
case int64:
return float64(val), nil
case string:
return strconv.ParseFloat(val, 64)
}
msg := fmt.Sprintf("Could not convert value %s to type FLOAT", value)
return 0, errors.New(msg)
}
func asInteger(value interface{}) (int64, error) {
switch val := value.(type) {
case bool:
v := int64(0)
if val {
v = int64(1)
}
return v, nil
case float64:
return int64(math.Trunc(val)), nil
case int64:
return val, nil
case string:
return strconv.ParseInt(val, 10, 64)
}
msg := fmt.Sprintf("Could not convert value %s to type INTEGER", value)
return 0, errors.New(msg)
}
func asString(value interface{}) (string, error) {
switch val := value.(type) {
case bool:
s := "False"
if val {
s = "True"
}
return s, nil
case float64:
return strconv.FormatFloat(val, 10, -1, 64), nil
case int64:
return strconv.FormatInt(val, 10), nil
case nil:
return "Null", nil
case string:
return val, nil
}
msg := fmt.Sprintf("Could not convert value %s to type STRING", value)
return "", errors.New(msg)
}

+ 81
- 4
primative.go View File

@ -3,6 +3,8 @@ package forge
import (
"errors"
"fmt"
"math"
"strconv"
)
type Primative struct {
@ -68,19 +70,94 @@ func (primative *Primative) UpdateValue(value interface{}) error {
}
func (primative *Primative) AsBoolean() (bool, error) {
return asBoolean(primative.value)
switch val := primative.value.(type) {
case bool:
return val, nil
case float64:
return val != 0, nil
case int64:
return val != 0, nil
case nil:
return false, nil
case string:
return val != "", nil
}
msg := fmt.Sprintf("Could not convert value %s to type BOOLEAN", primative.value)
return false, errors.New(msg)
}
func (primative *Primative) AsFloat() (float64, error) {
return asFloat(primative.value)
switch val := primative.value.(type) {
case bool:
floatVal := float64(0)
if val {
floatVal = float64(1)
}
return floatVal, nil
case float64:
return val, nil
case int64:
return float64(val), nil
case string:
return strconv.ParseFloat(val, 64)
}
msg := fmt.Sprintf("Could not convert value %s to type FLOAT", primative.value)
return 0, errors.New(msg)
}
func (primative *Primative) AsInteger() (int64, error) {
return asInteger(primative.value)
switch val := primative.value.(type) {
case bool:
intVal := int64(0)
if val {
intVal = int64(1)
}
return intVal, nil
case float64:
return int64(math.Trunc(val)), nil
case int64:
return val, nil
case string:
return strconv.ParseInt(val, 10, 64)
}
msg := fmt.Sprintf("Could not convert value %s to type INTEGER", primative.value)
return 0, errors.New(msg)
}
func (primative *Primative) AsNull() (interface{}, error) {
switch val := primative.value.(type) {
case nil:
return val, nil
}
msg := fmt.Sprintf("Could not convert value %s to nil", primative.value)
return 0, errors.New(msg)
}
func (primative *Primative) AsString() (string, error) {
return asString(primative.value)
switch val := primative.value.(type) {
case bool:
strVal := "False"
if val {
strVal = "True"
}
return strVal, nil
case float64:
return strconv.FormatFloat(val, 10, -1, 64), nil
case int64:
return strconv.FormatInt(val, 10), nil
case nil:
return "Null", nil
case string:
return val, nil
}
msg := fmt.Sprintf("Could not convert value %s to type STRING", primative.value)
return "", errors.New(msg)
}
func (primative *Primative) String() string {


Loading…
Cancel
Save