From 4feb1f61f2a4ada9e8cfb3a98ace1ea5b88a901c Mon Sep 17 00:00:00 2001 From: huangzj Date: Tue, 28 Apr 2020 17:51:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(Go-StudyExample):=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E5=A6=82=E4=B8=8B=EF=BC=9A=20map=E4=B8=8Estr?= =?UTF-8?q?uct=E4=B9=8B=E9=97=B4=E8=BD=AC=E6=8D=A2=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=B7=A5=E5=85=B7=E4=BD=BF=E7=94=A8=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=EF=BC=8C=E5=8F=82=E8=80=83=E4=BB=A3=E7=A0=81=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=EF=BC=9Ahttps://github.com/mitchellh/mapstructure=20json?= =?UTF-8?q?=E4=B8=8Estruct=E4=B9=8B=E9=97=B4=E8=BD=AC=E6=8D=A2=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=B7=A5=E5=85=B7=E4=BD=BF=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.go | 22 +++++++ entity/TestEntity.go | 7 +++ entity/UserType.go | 20 ++++++ example/JsonTest.go | 110 +++++++++++++++++++++++++++++++++ example/MapstructureExample.go | 94 ++++++++++++++++++++++++++++ go.mod | 5 ++ readme.md | 5 ++ 7 files changed, 263 insertions(+) create mode 100644 Main.go create mode 100644 entity/TestEntity.go create mode 100644 entity/UserType.go create mode 100644 example/JsonTest.go create mode 100644 example/MapstructureExample.go create mode 100644 go.mod create mode 100644 readme.md diff --git a/Main.go b/Main.go new file mode 100644 index 0000000..8820962 --- /dev/null +++ b/Main.go @@ -0,0 +1,22 @@ +/* + * @Author : huangzj + * @Time : 2020/4/28 17:17 + * @Description: + */ + +package main + +import "Go-StudyExample/example" + +func main() { + //--------------------------测试mapStructure的功能----- + //这边的四种使用方式差别感觉不是很大... + example.MapStructureTestFunc() + example.MapStructureTestFunc1() + example.MapStructureTestFunc2() + example.MapStructureTestFunc3() + + //--------------------------测试json包的转换功能 + example.JsonMarshalTest() + example.JsonUnmarshalTest() +} diff --git a/entity/TestEntity.go b/entity/TestEntity.go new file mode 100644 index 0000000..7012184 --- /dev/null +++ b/entity/TestEntity.go @@ -0,0 +1,7 @@ +package entity + +type Entity struct { + Num int + S string + T map[string]string +} diff --git a/entity/UserType.go b/entity/UserType.go new file mode 100644 index 0000000..91ed1ca --- /dev/null +++ b/entity/UserType.go @@ -0,0 +1,20 @@ +/* + * @Author : huangzj + * @Time : 2020/3/26 18:18 + * @Description: + */ + +package entity + +import "time" + +type UserType struct { + UserTypeId int + UserTypeName string +} + +type User struct { + UserType UserType `jpath:"userType"` + LoginName string `jpath:"loginName"` + T time.Time `jpath:"t"` +} diff --git a/example/JsonTest.go b/example/JsonTest.go new file mode 100644 index 0000000..e0e8907 --- /dev/null +++ b/example/JsonTest.go @@ -0,0 +1,110 @@ +/* + * @Author : huangzj + * @Time : 2020/3/27 15:38 + * @Description: + */ + +package example + +import ( + "encoding/json" + "fmt" + "time" +) + +type Time time.Time + +const ( + timeFormart = "2006-01-02 15:04:05" +) + +//-------------------结构体---------------- + +type Person struct { + Id int64 `json:"id"` + Name string `json:"name"` + Birthday Time `json:"birthday"` +} + +type json1 struct { + Name string `json:"name"` //测试一下别名 + Value string `json:"value,-"` //测试一下,有别名也有 - 能被转换 + Value1 string `json:"-"` //测试一下,只有 - 能被转换 + method1 string `json:"method1"` //测试一下私有属性不能被转换 + Now time.Time `json:"now"` //测试一下时间字段可否被转换 + Now1 Time `json:"now1"` //自定义时间转换的格式 + Age int `json:",omitempty"` //测试一下 omitempty 值为空不能被转换 + AgeString string `json:",omitempty"` //测试一下字符串 omitempty 值为空 不能被转换 + Length int `json:",String"` //测试一下tag里面带有string的自动转换 + Person Person //测试一下多级的结构 +} + +//-----------------------------------------示例方法 + +/* + * 测试序列化功能 + */ +func JsonMarshalTest() { + j := json1{ + Name: "name", + Value: "value", + Value1: "value1", + method1: "method1", + Now: time.Now(), + Now1: Time(time.Now()), + Age: 0, + Length: 24, + Person: Person{ + Id: 123, + Name: "xxxx", + Birthday: Time(time.Now()), + }, + } + + b, err := json.Marshal(j) + if err != nil { + panic(err.Error()) + } + + fmt.Print(string(b)) + +} + +/* + * 测试序列化功能 + */ +func JsonUnmarshalTest() { + src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30 16:09:51"}` + p := new(Person) + err := json.Unmarshal([]byte(src), &p) + + var m map[string]interface{} + _ = json.Unmarshal([]byte(src), &m) + if err != nil { + panic(err.Error()) + } +} + +//-------------------------接口实现 + +//实现该方法,实现对应的时间处理,json应该是没有支持时间处理的 +func (t *Time) UnmarshalJSON(data []byte) (err error) { + now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local) + *t = Time(now) + return +} + +/* + * 实现该方法,确定时间格式的输出 + */ +func (t Time) MarshalJSON() ([]byte, error) { + b := make([]byte, 0, len(timeFormart)+2) + b = append(b, '"') + b = time.Time(t).AppendFormat(b, timeFormart) + b = append(b, '"') + return b, nil +} + +func (t Time) String() string { + return time.Time(t).Format(timeFormart) +} diff --git a/example/MapstructureExample.go b/example/MapstructureExample.go new file mode 100644 index 0000000..19936d5 --- /dev/null +++ b/example/MapstructureExample.go @@ -0,0 +1,94 @@ +package example + +import ( + entity2 "Go-StudyExample/entity" + "encoding/json" + "fmt" + "github.com/goinggo/mapstructure" +) + +//-----------------------json数据--------------------- +var document = `{"loginName":"sptest1","userType":{"userTypeId":1,"userTypeName":"normal_user","t":"2026-01-02 15:04:05"}}` + +var document1 = `{"cobrandId":10010352,"channelId":-1,"locale":"en_US","tncVersion":2,"people":[{"name":"jack","age":{"birth":10,"year":2000,"animals":[{"barks":"yes","tail":"yes"},{"barks":"no","tail":"yes"}]}},{"name":"jill","age":{"birth":11,"year":2001}}]}` + +var document2 = `[{"name":"bill"},{"name":"lisa"}]` + +//-----------------------------结构体----------------------- +type Animal struct { + Barks string `jpath:"barks"` +} + +type People struct { + Age int `jpath:"age.birth"` // jpath is relative to the array + Animals []Animal `jpath:"age.animals"` +} + +type Items struct { + Categories []string `jpath:"categories"` + Peoples []People `jpath:"people"` // Specify the location of the array +} + +//---------------------------------测试方法------------------------ + +func MapStructureTestFunc() { + var te entity2.Entity + m := make(map[string]interface{}) + m["Num"] = 1 + m["S"] = "test" + m["T"] = map[string]string{"1": "1", "2": "2"} + + err := mapstructure.Decode(m, &te) + if err != nil { + panic(err.Error()) + } + + fmt.Print(te.Num, " ", te.S, " ", te.T) +} + +func MapStructureTestFunc1() { + var docMap map[string]interface{} + _ = json.Unmarshal([]byte(document), &docMap) + + var user entity2.User + err := mapstructure.Decode(docMap, &user) + + if err != nil { + panic(err.Error()) + } + fmt.Println(user.T.Format("2006-01-02 15:04:05")) + fmt.Println(user, " ", user.UserType.UserTypeId, " ", user.UserType.UserTypeName) +} + +type NameDoc struct { + Name string `jpath:"name"` +} + +func MapStructureTestFunc2() { + + sliceScript := []byte(document2) + var sliceMap []map[string]interface{} + _ = json.Unmarshal(sliceScript, &sliceMap) + + var myslice []NameDoc + err := mapstructure.DecodeSlicePath(sliceMap, &myslice) + + if err != nil { + panic(err.Error()) + } + + fmt.Println(myslice[0], " ", myslice[1]) +} + +func MapStructureTestFunc3() { + docScript := []byte(document1) + var docMap map[string]interface{} + _ = json.Unmarshal(docScript, &docMap) + + var items Items + err := mapstructure.DecodePath(docMap, &items) + if err != nil { + panic(err.Error()) + } + fmt.Println(items.Peoples[0], items.Peoples[1]) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..45fad5e --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module Go-StudyExample + +go 1.13 + +require github.com/goinggo/mapstructure v0.0.0-20140717182941-194205d9b4a9 diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..ff969fd --- /dev/null +++ b/readme.md @@ -0,0 +1,5 @@ +# 更新日志 + +2020/4/27 :创建项目,添加使用示例如下: +map与struct之间转换的处理工具使用示例,参考代码地址:https://github.com/mitchellh/mapstructure +json与struct之间转换处理工具使用示例 \ No newline at end of file