From eaf5c8f1efbafb299e7c30ba204f674faf544151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 1 Jun 2023 18:47:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0object=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- object.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/object.go b/object.go index 4e8c7f5..eafaf3c 100644 --- a/object.go +++ b/object.go @@ -7,9 +7,47 @@ // Date : 2023-06-01 18:33 package wrapper -func ObjectData(data map[string]interface{}) { +import ( + "encoding/json" + "reflect" +) +// ObjectData 对象类型, 支持 nil / Map / Struct +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:36 2023/6/1 +func ObjectData(data interface{}) *ObjectType { + ot := &ObjectType{ + source: data, + data: map[interface{}]interface{}{}, + byteData: []byte{}, + isValid: true, + } + if nil == ot { + return ot + } + reflectType := reflect.TypeOf(data) + switch reflectType.Kind() { + case reflect.Map: + fallthrough + case reflect.Struct: + ot.byteData, _ = json.Marshal(ot.source) + default: + // 数据类型不是 nil / map / struct 质疑 + ot.isValid = false + } + return ot } +// ObjectType ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:38 2023/6/1 type ObjectType struct { + source interface{} + data map[interface{}]interface{} + byteData []byte + isValid bool }