diff --git a/object.go b/object.go index a85b67e..8bebd85 100644 --- a/object.go +++ b/object.go @@ -9,6 +9,8 @@ package wrapper import ( "encoding/json" + "errors" + "git.zhangdeman.cn/zhangdeman/util" "reflect" ) @@ -19,10 +21,11 @@ import ( // Date : 18:36 2023/6/1 func ObjectData(data interface{}) *ObjectType { ot := &ObjectType{ - source: data, - data: map[interface{}]interface{}{}, - byteData: []byte{}, - isValid: true, + source: data, + data: map[interface{}]interface{}{}, + byteData: []byte{}, + isValid: true, + invalidErr: errors.New("data is invalid"), } if nil == ot { return ot @@ -46,10 +49,11 @@ func ObjectData(data interface{}) *ObjectType { // // Date : 18:38 2023/6/1 type ObjectType struct { - source interface{} - data map[interface{}]interface{} - byteData []byte - isValid bool + source interface{} + data map[interface{}]interface{} + byteData []byte + isValid bool + invalidErr error } // IsValid 是否有效对象数据 @@ -75,13 +79,39 @@ func (ot *ObjectType) IsNil() bool { // Author : go_developer@163.com<白茶清欢> // // Date : 18:51 2023/6/1 -func (ot *ObjectType) ToString() string { +func (ot *ObjectType) ToString() StringResult { if ot.IsNil() { - return "nil" + return StringResult{ + Value: "nil", + Err: nil, + } } if !ot.IsValid() { // 非法对象数据 - return "" + return StringResult{ + Value: "", + Err: errors.New("data is invalid"), + } + } + return StringResult{ + Value: string(ot.byteData), + Err: nil, } - return string(ot.byteData) +} + +// ToMapStringAny ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:17 2023/6/2 +func (ot *ObjectType) ToMapStringAny() ObjectResult { + res := ObjectResult{ + Value: map[string]interface{}{}, + Err: nil, + } + if ot.IsNil() { + return res + } + res.Err = util.JSON.UnmarshalWithNumber(ot.byteData, &res.Value) + return res }