This commit is contained in:
aozhiwei 2020-10-26 14:04:33 +08:00
parent 190668be5f
commit 1d9c4287fb
2 changed files with 42 additions and 21 deletions

View File

@ -6,11 +6,15 @@ type MutableXObject struct {
func NewMxoArray() *MutableXObject { func NewMxoArray() *MutableXObject {
p := new(MutableXObject) p := new(MutableXObject)
p.XObject._type = XOT_ARRAY
p.XObject._val = make([]*XObject, 10)
return p return p
} }
func NewMxoObject() *MutableXObject { func NewMxoObject() *MutableXObject {
p := new(MutableXObject) p := new(MutableXObject)
p.XObject._type = XOT_OBJECT
p.XObject._val = make(map[string]*XObject)
return p return p
} }
@ -23,8 +27,8 @@ func (this *MutableXObject) PushXObject(val *XObject) *MutableXObject{
if this.XObject.GetType() != XOT_ARRAY { if this.XObject.GetType() != XOT_ARRAY {
panic("MutableXObject.PushXvalue type error") panic("MutableXObject.PushXvalue type error")
} }
array := this.XObject._val.(*[]*XObject) array := this.XObject._val.([]*XObject)
this.XObject._val = append(*array, val) this.XObject._val = append(array, val)
return this return this
} }
@ -37,8 +41,8 @@ func (this *MutableXObject) SetXObject(key string, val *XObject) *MutableXObject
if this.XObject.GetType() != XOT_OBJECT { if this.XObject.GetType() != XOT_OBJECT {
panic("MutableXObject.PushXvalue type error") panic("MutableXObject.PushXvalue type error")
} }
kvObj := this.XObject._val.(*(map[string]*XObject)) kvObj := this.XObject._val.(map[string]*XObject)
(*kvObj)[key] = val kvObj[key] = val
return this return this
} }

View File

@ -24,22 +24,22 @@ func NewXoFromJsonStr(jsonStr string) *XObject {
} }
} }
func (this *XObject) Size(key interface{}) int { func (this *XObject) Size() int {
if (this._type == XOT_ARRAY) { if (this._type == XOT_ARRAY) {
array := this._val.(*[]*XObject) array := this._val.([]*XObject)
return len(*array) return len(array)
} else { } else {
panic("XObject.Size type error") panic("XObject.Size type error")
return 0 return 0
} }
} }
func (this *XObject) Reset() { func (this *XObject) reset() {
this._type = XOT_SIMPLE this._type = XOT_SIMPLE
this._val = nil this._val = NewXUndefined()
} }
func (this *XObject) GetType() int8{ func (this *XObject) GetType() int8 {
return this._type return this._type
} }
@ -73,8 +73,8 @@ func (this *XObject) At(key string) *XObject {
func (this *XObject) Index(index int32) *XObject { func (this *XObject) Index(index int32) *XObject {
if (this._type == XOT_ARRAY) { if (this._type == XOT_ARRAY) {
array := this._val.(*[]*XObject) array := this._val.([]*XObject)
return (*array)[index] return array[index]
} else { } else {
panic("XObject.At type error") panic("XObject.At type error")
return nil return nil
@ -107,6 +107,7 @@ func (this *XObject) readFromJsonString(data string) bool {
} }
this._type = XOT_OBJECT this._type = XOT_OBJECT
this._val = kvObj this._val = kvObj
return true
} else if jsonType == JSON_ARRAY { } else if jsonType == JSON_ARRAY {
var rawJson []interface{} var rawJson []interface{}
err := json.Unmarshal([]byte(data), &rawJson) err := json.Unmarshal([]byte(data), &rawJson)
@ -120,6 +121,7 @@ func (this *XObject) readFromJsonString(data string) bool {
xobj := new(XObject).fromInterface(val) xobj := new(XObject).fromInterface(val)
arrObj[index] = xobj arrObj[index] = xobj
} }
return true
} }
return false return false
} }
@ -135,9 +137,9 @@ func (this *XObject) ToJsonStr() string {
func (this *XObject) ToInterface() interface{} { func (this *XObject) ToInterface() interface{} {
if this._type == XOT_ARRAY { if this._type == XOT_ARRAY {
array := this._val.(*[]*XObject) array := this._val.([]*XObject)
arrObj := make([]interface{}, len(*array)) arrObj := make([]interface{}, len(array))
for index, val := range *array { for index, val := range array {
arrObj[index] = val.ToInterface() arrObj[index] = val.ToInterface()
} }
return arrObj return arrObj
@ -152,12 +154,27 @@ func (this *XObject) ToInterface() interface{} {
return this.AsXValue().ToJsonInterface() return this.AsXValue().ToJsonInterface()
} }
func (this *XObject) fromInterface(val interface{}) *XObject { func (this *XObject) fromInterface(iVal interface{}) *XObject {
if this._type != XOT_SIMPLE { if array, ok := iVal.([]interface{}); ok {
panic("XObject.fromInterface type error1") arrObj := make([]*XObject, len(array))
} for index, val := range array {
if !this.AsXValue().TryFromInterface(val) { arrObj[index] = new(XObject).fromInterface(val)
panic("XObject.fromInterface type error2") }
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")
}
} }
return this return this
} }