iedc-go/objtoobj.go
2017-08-04 08:20:59 +00:00

226 lines
3.7 KiB
Go

package hotime
import (
"errors"
"strconv"
"encoding/json"
)
//仅限于hotime.Slice
func ObjToMap(obj interface{},e... *Error) Map {
var err error
var v Map
if obj == nil {
v = nil
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type){
case Map:v = obj.(Map)
case map[string]interface {}:v=obj.(map[string]interface{})
default:
v = nil
err = errors.New("没有合适的转换对象!")
}
}
if(len(e)!=0){
e[0].SetError(err)
}
return v
}
//仅限于hotime.Slice
func ObjToSlice(obj interface{},e... *Error) Slice{
var err error
var v Slice
if obj == nil {
v = nil
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case Slice:
v = obj.(Slice)
case []interface {}:v=obj.([]interface{})
default:
v = nil
err = errors.New("没有合适的转换对象!")
}
}
if(len(e)!=0){
e[0].SetError(err)
}
return v
}
func ObjToFloat64(obj interface{},e... *Error) float64 {
var err error
v := float64(0)
if obj == nil {
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case int:
v = float64(obj.(int))
case int64:
v = float64(obj.(int64))
case string:
value, e := strconv.ParseFloat(obj.(string), 64)
if e != nil {
v = float64(0)
err = e
} else {
v = value
}
case float64:
v = obj.(float64)
case float32:
v = float64(obj.(float32))
case uint8:
value, e := strconv.ParseFloat(obj.(string), 64)
if e != nil {
v = float64(0)
err = e
} else {
v = value
}
default:
v = float64(0)
err = errors.New("没有合适的转换对象!")
}
}
if(len(e)!=0){
e[0].SetError(err)
}
return v
}
func ObjToInt64(obj interface{},e... *Error) int64 {
var err error
v := int64(0)
if obj == nil {
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case int:
v = int64(obj.(int))
case int64:
v = obj.(int64)
case string:
value, e := StrToInt(obj.(string))
if e != nil {
v = int64(0)
err = e
} else {
v = int64(value)
}
case uint8:
value, e := StrToInt(obj.(string))
if e != nil {
v = int64(0)
err = e
} else {
v = int64(value)
}
case float64:
v = int64(obj.(float64))
case float32:
v = int64(obj.(float32))
default:
v = int64(0)
err = errors.New("没有合适的转换对象!")
}
}
if(len(e)!=0){
e[0].SetError(err)
}
return v
}
func ObjToInt(obj interface{},e... *Error) int {
var err error
v := 0
if obj == nil {
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case int:
v = obj.(int)
case int64:
v = int(obj.(int64))
case string:
value, e := StrToInt(obj.(string))
if e != nil {
v = 0
err = e
} else {
v = value
}
case uint8:
value, e := StrToInt(obj.(string))
if e != nil {
v = 0
err = e
} else {
v = value
}
case float64:
v = int(obj.(float64))
case float32:
v = int(obj.(float32))
default:
v = 0
err = errors.New("没有合适的转换对象!")
}
}
if(len(e)!=0){
e[0].SetError(err)
}
return v
}
func ObjToStr(obj interface{}) string {
// fmt.Println(reflect.ValueOf(obj).Type().String() )
str := ""
if obj==nil{
return str
}
switch obj.(type) {
case int:
str = strconv.Itoa(obj.(int))
case uint8:
str = obj.(string)
case int64:
str = strconv.FormatInt(obj.(int64), 10)
case []byte:
str = string(obj.([]byte))
case string:
str = obj.(string)
case float64:
str = strconv.FormatFloat(obj.(float64), 'f', 6, 64)
default:
strbte,err:=json.Marshal(obj);
if err==nil{
str=string(strbte)
}
}
return str
}
//字符串转int
func StrToInt(s string) (int, error) {
i, err := strconv.Atoi(s)
return i, err
}