1
This commit is contained in:
parent
efae915742
commit
dacdd2a5db
28
httpcli.go
28
httpcli.go
@ -2,9 +2,10 @@ package q5
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
net_url "net/url"
|
||||
"io/ioutil"
|
||||
"errors"
|
||||
"strings"
|
||||
//"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -12,32 +13,39 @@ const (
|
||||
HTTP_OPT_SET_PROXY = iota
|
||||
)
|
||||
|
||||
func internalSetOpt(client *http.Client, request *http.Request, opt int32, key *XValue, val *XValue) {
|
||||
func internalSetOpt(client *http.Client, request *http.Request, opt int32,
|
||||
key string, val string) {
|
||||
switch opt {
|
||||
case HTTP_OPT_ADD_HEADER:
|
||||
request.Header.Add(key.GetString(), val.GetString())
|
||||
request.Header.Add(key, val)
|
||||
default:
|
||||
panic("error http opt")
|
||||
}
|
||||
}
|
||||
|
||||
func HttpGet(url string, params *XObject) (string, error) {
|
||||
func HttpGet(url string, params map[string]string) (string, error) {
|
||||
return HttpGetEx(url, params, nil)
|
||||
}
|
||||
|
||||
func HttpGetEx(url string, params *XObject,
|
||||
initFunc func(setOpt func(int32, *XValue, *XValue))) (string, error) {
|
||||
func HttpGetEx(url string, params map[string]string,
|
||||
initFunc func(setOpt func(int32, string, string))) (string, error) {
|
||||
if !StrContains(url, "?") {
|
||||
url = url + "?"
|
||||
}
|
||||
url = url + params.ToUrlEncodedStr()
|
||||
{
|
||||
u := net_url.Values{}
|
||||
for key, val := range params {
|
||||
u.Set(key, val)
|
||||
}
|
||||
url = url + u.Encode()
|
||||
}
|
||||
client := &http.Client{}
|
||||
request, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
panic("http.NewRequest error")
|
||||
}
|
||||
if initFunc != nil {
|
||||
initFunc(func (opt int32, key *XValue, val *XValue) {
|
||||
initFunc(func (opt int32, key string, val string) {
|
||||
internalSetOpt(client, request, opt, key, val)
|
||||
})
|
||||
}
|
||||
@ -56,7 +64,8 @@ func HttpGetEx(url string, params *XObject,
|
||||
}
|
||||
}
|
||||
|
||||
func HttpGetAsJson(url string, params *XObject) (*XObject, string, error) {
|
||||
/*
|
||||
func HttpGetAsJson(url string, params map[string]string) (map, string, error) {
|
||||
respStr, err := HttpGet(url, params)
|
||||
if err != nil {
|
||||
return nil, respStr, err
|
||||
@ -82,3 +91,4 @@ func HttpPostContent(url string, contentType string, body string, response *stri
|
||||
return err
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
53
init.go
53
init.go
@ -1,53 +0,0 @@
|
||||
package q5
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"bufio"
|
||||
)
|
||||
|
||||
var optDebug = ""
|
||||
var _optDebug = false
|
||||
var _optTimeZone int64 = 8
|
||||
var q5JsonConfData = ""
|
||||
var q5JsonConf *XObject
|
||||
var initLines []string
|
||||
|
||||
func init() {
|
||||
initLines = make([]string, 0)
|
||||
if optDebug != "" {
|
||||
_optDebug = true
|
||||
}
|
||||
initLines = append(initLines, fmt.Sprintf("optDebug %s _optDebug:%t", optDebug, _optDebug))
|
||||
confName := "q5.json"
|
||||
if Debug() {
|
||||
confName = "q5.debug.json"
|
||||
}
|
||||
if f, err := os.Open(confName); err == nil {
|
||||
defer f.Close()
|
||||
q5JsonConfData, _ = bufio.NewReader(f).ReadString(0)
|
||||
q5JsonConf = NewXoFromJsonStr(q5JsonConfData)
|
||||
if q5JsonConf.HasKey("timeZone") {
|
||||
_optTimeZone = q5JsonConf.At("timeZone").AsXValue().GetInt64()
|
||||
if _optTimeZone < 1 || _optTimeZone > 24 {
|
||||
panic(confName +
|
||||
" timeZone error val:" +
|
||||
NewXInt64(_optTimeZone).GetString())
|
||||
}
|
||||
}
|
||||
initLines = append(initLines, fmt.Sprintf("%s data:%s", confName, q5JsonConfData))
|
||||
initLines = append(initLines, fmt.Sprintf("%s json:%s", confName, q5JsonConf.ToJsonStr()))
|
||||
} else {
|
||||
initLines = append(initLines, fmt.Sprintf("%s not found", confName))
|
||||
}
|
||||
initLines = append(initLines, fmt.Sprintf("_optTimeZone:%d", _optTimeZone))
|
||||
/*if Debug() {
|
||||
for _, val := range initLines {
|
||||
fmt.Println(val)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
func GetInitLogs() []string {
|
||||
return initLines
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package q5
|
||||
|
||||
type MutableXObject struct {
|
||||
XObject
|
||||
}
|
||||
|
||||
func NewMxoArray() *MutableXObject {
|
||||
p := new(MutableXObject)
|
||||
p.XObject._type = XOT_ARRAY
|
||||
p.XObject._val = make([]*XObject, 0)
|
||||
return p
|
||||
}
|
||||
|
||||
func NewMxoObject() *MutableXObject {
|
||||
p := new(MutableXObject)
|
||||
p.XObject._type = XOT_OBJECT
|
||||
p.XObject._val = make(map[string]*XObject)
|
||||
return p
|
||||
}
|
||||
|
||||
func (this *MutableXObject) PushXValue(val *XValue) *MutableXObject{
|
||||
this.PushXObject(val.AsXObject())
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *MutableXObject) PushXObject(val *XObject) *MutableXObject{
|
||||
if this.XObject.GetType() != XOT_ARRAY {
|
||||
panic("MutableXObject.PushXvalue type error")
|
||||
}
|
||||
array := this.XObject._val.([]*XObject)
|
||||
this.XObject._val = append(array, val)
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *MutableXObject) SetXValue(key string, val *XValue) *MutableXObject{
|
||||
this.SetXObject(key, val.AsXObject())
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *MutableXObject) SetXObject(key string, val *XObject) *MutableXObject{
|
||||
if this.XObject.GetType() != XOT_OBJECT {
|
||||
panic("MutableXObject.PushXvalue type error")
|
||||
}
|
||||
kvObj := this.XObject._val.(map[string]*XObject)
|
||||
kvObj[key] = val
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *MutableXObject) AsXObject() *XObject{
|
||||
return &this.XObject
|
||||
}
|
30
sysutils.go
30
sysutils.go
@ -6,23 +6,11 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
"io/ioutil"
|
||||
//"io/ioutil"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func Debug() bool {
|
||||
return _optDebug
|
||||
}
|
||||
|
||||
func GetTimeZone() int64 {
|
||||
return _optTimeZone
|
||||
}
|
||||
|
||||
func GetDaySeconds(seconds int64) int64 {
|
||||
return GetDaySecondsEx(seconds, GetTimeZone())
|
||||
}
|
||||
|
||||
func GetDaySecondsEx(seconds int64, timeZone int64) int64 {
|
||||
func GetDaySeconds(seconds int64, timeZone int64) int64 {
|
||||
return ((seconds + timeZone * 3600)/3600/24 + 1) * 3600 * 24 - 3600 * timeZone;
|
||||
}
|
||||
|
||||
@ -45,6 +33,7 @@ func MkInt64(lo32 int32, hi32 int32) int64 {
|
||||
return int64(lo32) + (int64(hi32) << 32)
|
||||
}
|
||||
|
||||
/*
|
||||
func Request(r *http.Request, name string) *XValue {
|
||||
if r.Form == nil {
|
||||
r.ParseForm()
|
||||
@ -56,6 +45,7 @@ func Request(r *http.Request, name string) *XValue {
|
||||
}
|
||||
return NewXString("")
|
||||
}
|
||||
*/
|
||||
|
||||
func Response(w* http.ResponseWriter, data string) {
|
||||
(*w).Write([]byte(data))
|
||||
@ -66,16 +56,17 @@ func ResponseOk(w* http.ResponseWriter) {
|
||||
}
|
||||
|
||||
func ResponseErr(w* http.ResponseWriter, errCode int32, errMsg string) {
|
||||
respObj := NewMxoObject()
|
||||
/*respObj := NewMxoObject()
|
||||
respObj.SetXValue("errcode", NewXInt32(errCode))
|
||||
respObj.SetXValue("errmsg", NewXString(errMsg))
|
||||
Response(w, respObj.ToJsonStr())
|
||||
Response(w, respObj.ToJsonStr())*/
|
||||
}
|
||||
|
||||
func ResponseInt32Ok(w* http.ResponseWriter, key string, val int32) {
|
||||
(*w).Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "%s":%d}`, key, val)))
|
||||
}
|
||||
|
||||
/*
|
||||
func GetPostBody(r *http.Request) *XValue {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@ -84,6 +75,7 @@ func GetPostBody(r *http.Request) *XValue {
|
||||
v := NewXString(string(body))
|
||||
return v
|
||||
}
|
||||
*/
|
||||
|
||||
func GetRequestRemoteAddr(r *http.Request) string {
|
||||
remoteAddr := r.RemoteAddr
|
||||
@ -149,9 +141,3 @@ func FormatUnixDateEx(sec int64) string {
|
||||
strTime := time.Unix(sec, 0).Format("20060102")
|
||||
return strTime
|
||||
}
|
||||
|
||||
func ParseStruct(obj interface{}, kv map[string]interface{}) {
|
||||
/*for k, v := range kv {
|
||||
|
||||
}*/
|
||||
}
|
||||
|
231
xobject.go
231
xobject.go
@ -1,231 +0,0 @@
|
||||
package q5
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"net/url"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
const (
|
||||
XOT_SIMPLE = 0
|
||||
XOT_ARRAY = iota
|
||||
XOT_OBJECT = iota
|
||||
)
|
||||
|
||||
type XObject struct {
|
||||
_type int8
|
||||
_val interface{}
|
||||
}
|
||||
|
||||
func NewXoFromJsonStr(jsonStr string) *XObject {
|
||||
p := new(XObject)
|
||||
if p.readFromJsonString(jsonStr) {
|
||||
return p
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func NewXoString(val string) *XObject {
|
||||
p := new(XObject)
|
||||
p._type = XOT_SIMPLE
|
||||
p._val = NewXString(val)
|
||||
return p
|
||||
}
|
||||
|
||||
func (this *XObject) Size() int {
|
||||
if (this._type == XOT_ARRAY) {
|
||||
array := this._val.([]*XObject)
|
||||
return len(array)
|
||||
} else if (this._type == XOT_OBJECT) {
|
||||
object := this._val.(map[string]*XObject)
|
||||
return len(object)
|
||||
} else {
|
||||
panic("XObject.Size type error")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (this *XObject) reset() {
|
||||
this._type = XOT_SIMPLE
|
||||
this._val = NewXUndefined()
|
||||
}
|
||||
|
||||
func (this *XObject) GetType() int8 {
|
||||
return this._type
|
||||
}
|
||||
|
||||
func (this *XObject) IsSimple() bool {
|
||||
return this.GetType() == XOT_SIMPLE
|
||||
}
|
||||
|
||||
func (this *XObject) IsArray() bool {
|
||||
return this.GetType() == XOT_ARRAY
|
||||
}
|
||||
|
||||
func (this *XObject) IsObject() bool {
|
||||
return this.GetType() == XOT_OBJECT
|
||||
}
|
||||
|
||||
func (this *XObject) AsXValue() *XValue {
|
||||
if this._type == XOT_SIMPLE {
|
||||
if this._val == nil {
|
||||
this._val = NewXUndefined()
|
||||
}
|
||||
return this._val.(*XValue)
|
||||
|
||||
} else {
|
||||
panic("XObject.AsXValue type error")
|
||||
return NewXUndefined()
|
||||
}
|
||||
}
|
||||
|
||||
func (this *XObject) At(key string) *XObject {
|
||||
if this._type == XOT_OBJECT {
|
||||
object := this._val.(map[string]*XObject)
|
||||
val, ok := object[key]
|
||||
if ok {
|
||||
return val
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
panic("XObject.At type error")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *XObject) Index(index int32) *XObject {
|
||||
if (this._type == XOT_ARRAY) {
|
||||
array := this._val.([]*XObject)
|
||||
return array[index]
|
||||
} else {
|
||||
panic("XObject.At type error")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (this *XObject) HasKey(key string) bool {
|
||||
if (this._type == XOT_OBJECT) {
|
||||
object := this._val.(map[string]*XObject)
|
||||
_, ok := object[key]
|
||||
return ok
|
||||
} else {
|
||||
panic("XObject.HasKey type error")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (this *XObject) GetSimpleStr(key string, defVal string) string {
|
||||
if this.IsObject() {
|
||||
val := this.At(key)
|
||||
if val != nil {
|
||||
if val.IsSimple() {
|
||||
return val.AsXValue().GetString()
|
||||
} else {
|
||||
return defVal
|
||||
}
|
||||
}
|
||||
}
|
||||
return defVal
|
||||
}
|
||||
|
||||
func (this *XObject) readFromJsonString(data string) bool {
|
||||
jsonType := JsonStrType(data)
|
||||
if jsonType == JSON_OBJECT {
|
||||
var rawJson map[string]interface{}
|
||||
err := json.Unmarshal([]byte(data), &rawJson)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
kvObj := make(map[string]*XObject)
|
||||
for key, val := range rawJson {
|
||||
xobj := new(XObject).fromInterface(val)
|
||||
kvObj[key] = xobj
|
||||
}
|
||||
this._type = XOT_OBJECT
|
||||
this._val = kvObj
|
||||
return true
|
||||
} else if jsonType == JSON_ARRAY {
|
||||
var rawJson []interface{}
|
||||
err := json.Unmarshal([]byte(data), &rawJson)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
arrObj := make([]*XObject, len(rawJson))
|
||||
this._type = XOT_ARRAY
|
||||
this._val = arrObj
|
||||
for index, val := range rawJson {
|
||||
xobj := new(XObject).fromInterface(val)
|
||||
arrObj[index] = xobj
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *XObject) ToJsonStr() string {
|
||||
if this._type == XOT_ARRAY || this._type == XOT_OBJECT {
|
||||
jsonObj, _ := json.Marshal(this.ToInterface())
|
||||
return string(jsonObj)
|
||||
} else {
|
||||
return "{}"
|
||||
}
|
||||
}
|
||||
|
||||
func (this *XObject) ToUrlEncodedStr() string {
|
||||
if this.IsObject() {
|
||||
u := url.Values{}
|
||||
kvObj := this._val.(map[string]*XObject)
|
||||
for key, val := range kvObj {
|
||||
u.Set(key, val.AsXValue().GetString())
|
||||
}
|
||||
return u.Encode()
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (this *XObject) ToInterface() interface{} {
|
||||
if this._type == XOT_ARRAY {
|
||||
array := this._val.([]*XObject)
|
||||
arrObj := make([]interface{}, len(array))
|
||||
for index, val := range array {
|
||||
arrObj[index] = val.ToInterface()
|
||||
}
|
||||
return arrObj
|
||||
} else if this._type == XOT_OBJECT {
|
||||
kvObj := this._val.(map[string]*XObject)
|
||||
object := make(map[string]interface{})
|
||||
for key, val := range kvObj {
|
||||
object[key] = val.ToInterface()
|
||||
}
|
||||
return object
|
||||
}
|
||||
return this.AsXValue().ToJsonInterface()
|
||||
}
|
||||
|
||||
func (this *XObject) fromInterface(iVal interface{}) *XObject {
|
||||
if array, ok := iVal.([]interface{}); ok {
|
||||
arrObj := make([]*XObject, len(array))
|
||||
for index, val := range array {
|
||||
arrObj[index] = new(XObject).fromInterface(val)
|
||||
}
|
||||
this._type = XOT_ARRAY
|
||||
this._val = arrObj
|
||||
} else if object, ok := iVal.(map[string]interface{}); ok {
|
||||
kvObj := make(map[string]*XObject)
|
||||
for key, val := range object {
|
||||
kvObj[key] = new(XObject).fromInterface(val)
|
||||
}
|
||||
this._type = XOT_OBJECT
|
||||
this._val = kvObj
|
||||
} else {
|
||||
this._type = XOT_SIMPLE
|
||||
this._val = NewXUndefined()
|
||||
if !this.AsXValue().TryFromInterface(iVal) {
|
||||
panic("XObject.fromInterface type error2 type" + reflect.TypeOf(iVal).String())
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
311
xvalue.go
311
xvalue.go
@ -1,311 +0,0 @@
|
||||
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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user