package q5 import ( "strconv" ) const ( XVT_UNDEFINED = 0 XVT_INT = iota XVT_FLOAT = iota XVT_STRING = iota XVT_BYTES = iota XVT_USERDATA = iota ) type XValue struct { _type int8 _val interface{} } func NewXUndefined() *XValue { p := new(XValue) p._type = XVT_UNDEFINED p._val = nil return p } func NewXInt32(val int32) *XValue { p := new(XValue) p.SetInt32(val) return p } func NewXInt64(val int64) *XValue { p := new(XValue) p.SetInt64(val) return p } func NewXString(val string) *XValue { p := new(XValue) p.SetString(val) return p } func (this *XValue) GetType() int8 { if this._type == XVT_UNDEFINED { return XVT_INT } else { return this._type } } func (this *XValue) Reset() { this._type = XVT_UNDEFINED this._val = nil } func (this *XValue) IsUndefined() bool { return this._type == XVT_UNDEFINED } func (this *XValue) IsInt() bool { return this._type == XVT_INT } func (this *XValue) IsFloat() bool { return this._type == XVT_INT } func (this *XValue) IsNumber() bool { return this.IsInt() || this.IsFloat() } func (this *XValue) IsString() bool { return this._type == XVT_STRING } func (this *XValue) IsBytes() bool { return this._type == XVT_BYTES } func (this *XValue) IsUserData() bool { return this._type == XVT_USERDATA } func (this *XValue) SetUInt8(val uint8) *XValue { this.SetInt64(int64(val)) return this } func (this *XValue) SetInt8(val int8) *XValue { this.SetInt64(int64(val)) return this } func (this *XValue) SetUInt16(val uint16) *XValue { this.SetInt64(int64(val)) return this } func (this *XValue) SetInt16(val int16) *XValue { this.SetInt64(int64(val)) return this } func (this *XValue) SetUInt32(val uint32) *XValue { this.SetInt64(int64(val)) return this } func (this *XValue) SetInt32(val int32) *XValue { this.SetInt64(int64(val)) return this } func (this *XValue) SetInt64(val int64) *XValue { val_copy := val this._type = XVT_INT this._val = &val_copy return this } func (this *XValue) SetFloat32(val float32) *XValue { this.SetFloat64(float64(val)) 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) GetUInt8() uint8 { return uint8(this.GetInt64()) } func (this *XValue) GetInt8() int8 { return int8(this.GetInt64()) } func (this *XValue) GetUInt16() uint16 { return uint16(this.GetInt64()) } func (this *XValue) GetInt16() int16 { return int16(this.GetInt64()) } func (this *XValue) GetUInt32() uint32 { return uint32(this.GetInt64()) } 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) GetFloat32() float32 { return float32(this.GetFloat64()) } func (this *XValue) GetFloat64() float64 { switch this._type { case XVT_INT: return float64(*(this._val.(*int64))) case XVT_FLOAT: return *(this._val.(*float64)) case XVT_STRING, XVT_BYTES, XVT_USERDATA: val, err := strconv.ParseFloat(this.GetString(), 64) if err == nil { return val } else { val, err := strconv.ParseInt(this.GetString(), 10, 64) if err == nil { return float64(val) } else { return 0 } } default: return 0 } } func (this *XValue) GetString() string { switch this._type { case XVT_INT: return strconv.FormatInt(this.GetInt64(), 10) case XVT_FLOAT: return strconv.FormatFloat(this.GetFloat64(), 'E', -1, 64) case XVT_STRING: return *(this._val.(*string)) default: return "" } } func (this *XValue) GetUserData() interface{} { if this._type == XVT_USERDATA { return this._val } else { return nil } } func (this *XValue) ToJsonInterface() interface{} { switch this._type { case XVT_INT: int64Val := this.GetInt64() if uint64(int64Val) > (1 << 60) { return this.GetString() } else { return this.GetInt64() } case XVT_FLOAT: return this.GetFloat64() case XVT_STRING: return this.GetString() case XVT_BYTES: return this.GetString() default: panic("XValue.ToJsonInterface type error") return "" } } func (this *XValue) TryFromInterface(val interface{}) bool { if val == nil { this._type = XVT_UNDEFINED this._val = nil return true } switch val.(type) { case int8: this.SetInt8(val.(int8)) case int16: this.SetInt16(val.(int16)) case int32: this.SetInt32(val.(int32)) case int64: this.SetInt64(val.(int64)) case uint8: this.SetUInt8(val.(uint8)) case uint16: this.SetUInt16(val.(uint16)) case uint32: this.SetUInt32(val.(uint32)) case float32: this.SetFloat32(val.(float32)) case float64: this.SetFloat64(val.(float64)) case string: this.SetString(val.(string)) default: return false } return true } func (this *XValue) AsXObject() *XObject { xobj := new(XObject) xobj._type = XOT_SIMPLE xobj._val = this return xobj }