diff --git a/.example/net/ghttp/server/session/session.go b/.example/net/ghttp/server/session/basic/session.go similarity index 73% rename from .example/net/ghttp/server/session/session.go rename to .example/net/ghttp/server/session/basic/session.go index 91cc7ade1..c7a21a684 100644 --- a/.example/net/ghttp/server/session/session.go +++ b/.example/net/ghttp/server/session/basic/session.go @@ -9,14 +9,14 @@ import ( func main() { s := g.Server() s.Group("/", func(group *ghttp.RouterGroup) { - g.GET("/set", func(r *ghttp.Request) { + group.GET("/set", func(r *ghttp.Request) { r.Session.Set("time", gtime.Timestamp()) r.Response.Write("ok") }) - g.GET("/get", func(r *ghttp.Request) { + group.GET("/get", func(r *ghttp.Request) { r.Response.WriteJson(r.Session.Map()) }) - g.GET("/clear", func(r *ghttp.Request) { + group.GET("/clear", func(r *ghttp.Request) { r.Session.Clear() }) }) diff --git a/.example/net/ghttp/server/session/config.toml b/.example/net/ghttp/server/session/redis/config.toml similarity index 100% rename from .example/net/ghttp/server/session/config.toml rename to .example/net/ghttp/server/session/redis/config.toml diff --git a/.example/net/ghttp/server/session/redis/redis.go b/.example/net/ghttp/server/session/redis/redis.go new file mode 100644 index 000000000..5544c3318 --- /dev/null +++ b/.example/net/ghttp/server/session/redis/redis.go @@ -0,0 +1,29 @@ +package main + +import ( + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/os/gsession" + "github.com/gogf/gf/os/gtime" + "time" +) + +func main() { + s := g.Server() + s.SetSessionMaxAge(2 * time.Minute) + s.SetSessionStorage(gsession.NewStorageRedis(g.Redis())) + s.Group("/", func(group *ghttp.RouterGroup) { + group.GET("/set", func(r *ghttp.Request) { + r.Session.Set("time", gtime.Timestamp()) + r.Response.Write("ok") + }) + group.GET("/get", func(r *ghttp.Request) { + r.Response.WriteJson(r.Session.Map()) + }) + group.GET("/clear", func(r *ghttp.Request) { + r.Session.Clear() + }) + }) + s.SetPort(8199) + s.Run() +} diff --git a/.example/other/test.go b/.example/other/test.go index d8ade6f94..3d3903a4b 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -2,14 +2,35 @@ package main import ( "fmt" - "github.com/gogf/gf/os/gtime" - "github.com/gogf/gf/util/gconv" + "github.com/gogf/gf/encoding/gjson" ) func main() { - t1 := gconv.Convert(1989, "Time") - t2 := gconv.Time("2033-01-11 04:00:00 +0800 CST") - fmt.Println(gtime.Timestamp()) - fmt.Println(t1) - fmt.Println(t2) + + type Item struct { + Title string `json:"title"` + Key string `json:"key"` + } + + type M struct { + Id string `json:"id"` + Me map[string]interface{} `json:"me"` + Txt string `json:"txt"` + Items []*Item `json:"items"` + } + + txt := `{ + "id":"88888", + "me":{"name":"mikey","day":"20009"}, + "txt":"hello", + "items":null + }` + + json, _ := gjson.LoadContent(txt) + fmt.Println(json) + m := new(M) + e := json.ToStructDeep(m) + fmt.Println(e) + fmt.Println(m) + } diff --git a/encoding/gjson/gjson_z_unit_basic_test.go b/encoding/gjson/gjson_z_unit_basic_test.go index aa767298c..014be9fd1 100644 --- a/encoding/gjson/gjson_z_unit_basic_test.go +++ b/encoding/gjson/gjson_z_unit_basic_test.go @@ -462,3 +462,39 @@ func Test_IsNil(t *testing.T) { gtest.Assert(j.IsNil(), true) }) } + +func Test_ToStructDeep(t *testing.T) { + gtest.Case(t, func() { + type Item struct { + Title string `json:"title"` + Key string `json:"key"` + } + + type M struct { + Id string `json:"id"` + Me map[string]interface{} `json:"me"` + Txt string `json:"txt"` + Items []*Item `json:"items"` + } + + txt := `{ + "id":"88888", + "me":{"name":"mikey","day":"20009"}, + "txt":"hello", + "items":null + }` + + j, err := gjson.LoadContent(txt) + gtest.Assert(err, nil) + gtest.Assert(j.GetString("me.name"), "mikey") + gtest.Assert(j.GetString("items"), "") + gtest.Assert(j.GetBool("items"), false) + gtest.Assert(j.GetArray("items"), nil) + m := new(M) + err = j.ToStructDeep(m) + gtest.Assert(err, nil) + gtest.AssertNE(m.Me, nil) + gtest.Assert(m.Me["day"], "20009") + gtest.Assert(m.Items, nil) + }) +} diff --git a/util/gconv/gconv_struct.go b/util/gconv/gconv_struct.go index c2ffb1cfe..49f90e4cf 100644 --- a/util/gconv/gconv_struct.go +++ b/util/gconv/gconv_struct.go @@ -223,7 +223,11 @@ func bindVarToStructAttr(elem reflect.Value, name string, value interface{}) (er err = bindVarToReflectValue(structFieldValue, value) } }() - structFieldValue.Set(reflect.ValueOf(Convert(value, structFieldValue.Type().String()))) + if value == nil { + structFieldValue.Set(reflect.Zero(structFieldValue.Type())) + } else { + structFieldValue.Set(reflect.ValueOf(Convert(value, structFieldValue.Type().String()))) + } return nil } @@ -242,7 +246,11 @@ func bindVarToStructByIndex(elem reflect.Value, index int, value interface{}) (e err = bindVarToReflectValue(structFieldValue, value) } }() - structFieldValue.Set(reflect.ValueOf(Convert(value, structFieldValue.Type().String()))) + if value == nil { + structFieldValue.Set(reflect.Zero(structFieldValue.Type())) + } else { + structFieldValue.Set(reflect.ValueOf(Convert(value, structFieldValue.Type().String()))) + } return nil } diff --git a/util/gconv/gconv_z_unit_struct_test.go b/util/gconv/gconv_z_unit_struct_test.go index a4c8128f1..9ba5fdf9f 100644 --- a/util/gconv/gconv_z_unit_struct_test.go +++ b/util/gconv/gconv_z_unit_struct_test.go @@ -569,6 +569,36 @@ func Test_Struct_Interface(t *testing.T) { }) } +func Test_Struct_NilAttribute(t *testing.T) { + gtest.Case(t, func() { + type Item struct { + Title string `json:"title"` + Key string `json:"key"` + } + + type M struct { + Id string `json:"id"` + Me map[string]interface{} `json:"me"` + Txt string `json:"txt"` + Items []*Item `json:"items"` + } + m := new(M) + err := gconv.Struct(g.Map{ + "id": "88888", + "me": g.Map{ + "name": "mikey", + "day": "20009", + }, + "txt": "hello", + "items": nil, + }, m) + gtest.Assert(err, nil) + gtest.AssertNE(m.Me, nil) + gtest.Assert(m.Me["day"], "20009") + gtest.Assert(m.Items, nil) + }) +} + func Test_Struct_Complex(t *testing.T) { gtest.Case(t, func() { type ApplyReportDetail struct {