q5/xvalue.go
aozhiwei f58f61120a 1
2020-09-07 16:57:34 +08:00

95 lines
1.6 KiB
Go

package q5
import (
"strconv"
)
const (
XVT_INT = 0
XVT_FLOAT = iota
XVT_STRING = iota
XVT_BYTES = iota
XVT_USERDATA = iota
)
type XValue struct
{
_type int8
_val interface{}
}
func (this *XValue) GetType() int8 {
return this._type
}
func (this *XValue) SetInt64(val int64) *XValue {
val_copy := val
this._type = XVT_INT
this._val = &val_copy
return this
}
func (this *XValue) SetFloat64(val float64) *XValue {
val_copy := val
this._type = XVT_FLOAT
this._val = &val_copy
return this
}
func (this *XValue) SetString(val string) *XValue {
val_copy := "" + val
this._type = XVT_STRING
this._val = &val_copy
return this
}
func (this *XValue) SetBytes(val []byte) *XValue {
val_copy := val
this._type = XVT_FLOAT
this._val = &val_copy
return this
}
func (this *XValue) SetUserData(val interface{}) *XValue {
val_copy := val
this._type = XVT_FLOAT
this._val = &val_copy
return this
}
func (this *XValue) GetInt32() int32 {
return int32(this.GetInt64())
}
func (this *XValue) GetInt64() int64 {
switch this._type {
case XVT_INT:
return *(this._val.(*int64))
case XVT_FLOAT:
return int64(*(this._val.(*float64)))
case XVT_STRING, XVT_BYTES, XVT_USERDATA:
val, err := strconv.ParseInt(this.GetString(), 10, 64)
if err == nil {
return val
} else {
val, err := strconv.ParseFloat(this.GetString(), 64)
if err == nil {
return int64(val)
} else {
return 0
}
}
default:
return 0
}
}
func (this *XValue) GetString() string {
switch this._type {
case XVT_STRING:
return *(this._val.(*string))
default:
return ""
}
}