From cf34d7bd56dfe878d72bfd89f5f236121da8c5db Mon Sep 17 00:00:00 2001 From: John Date: Fri, 25 Oct 2019 13:49:03 +0800 Subject: [PATCH] fix issue in gconv.Interfaces; improve storage interface for gsession --- .example/database/gdb/mysql/gdb_insert.go | 6 ++---- net/ghttp/ghttp_unit_router_domain_basic_test.go | 2 +- os/gsession/gsession_session.go | 12 ++++++------ os/gsession/gsession_storage.go | 12 ++++++------ os/gsession/gsession_storage_file.go | 12 ++++++------ util/gconv/gconv_slice.go | 4 ++-- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/.example/database/gdb/mysql/gdb_insert.go b/.example/database/gdb/mysql/gdb_insert.go index 3eb2f663e..f53e94503 100644 --- a/.example/database/gdb/mysql/gdb_insert.go +++ b/.example/database/gdb/mysql/gdb_insert.go @@ -12,11 +12,9 @@ func main() { // 开启调试模式,以便于记录所有执行的SQL db.SetDebug(true) + fmt.Println(time.Now()) r, e := db.Table("user").Data(g.Map{ - "passport": "1", - "password": "1", - "nickname": "1", - "create_time": time.Now(), + "create_time": time.Now().Local(), }).Insert() if e != nil { panic(e) diff --git a/net/ghttp/ghttp_unit_router_domain_basic_test.go b/net/ghttp/ghttp_unit_router_domain_basic_test.go index df2751c82..61091f4c1 100644 --- a/net/ghttp/ghttp_unit_router_domain_basic_test.go +++ b/net/ghttp/ghttp_unit_router_domain_basic_test.go @@ -350,7 +350,7 @@ func Test_Router_DomainGroup(t *testing.T) { defer s.Shutdown() gtest.Case(t, func() { client1 := ghttp.NewClient() - client1.SetPrefix(fmt.Sprintf("http://local:%d", p)) + client1.SetPrefix(fmt.Sprintf("http://localhost:%d", p)) client2 := ghttp.NewClient() client2.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) diff --git a/os/gsession/gsession_session.go b/os/gsession/gsession_session.go index ac7f086cc..d621a7f93 100644 --- a/os/gsession/gsession_session.go +++ b/os/gsession/gsession_session.go @@ -54,7 +54,7 @@ func (s *Session) init() { // Set sets key-value pair to this session. func (s *Session) Set(key string, value interface{}) error { s.init() - if err := s.manager.storage.Set(key, value); err != nil { + if err := s.manager.storage.Set(s.id, key, value); err != nil { if err == ErrorDisabled { s.data.Set(key, value) } else { @@ -68,7 +68,7 @@ func (s *Session) Set(key string, value interface{}) error { // Sets batch sets the session using map. func (s *Session) Sets(data map[string]interface{}) error { s.init() - if err := s.manager.storage.SetMap(data); err != nil { + if err := s.manager.storage.SetMap(s.id, data); err != nil { if err == ErrorDisabled { s.data.Sets(data) } else { @@ -85,7 +85,7 @@ func (s *Session) Remove(key string) error { return nil } s.init() - if err := s.manager.storage.Remove(key); err != nil { + if err := s.manager.storage.Remove(s.id, key); err != nil { if err == ErrorDisabled { s.data.Remove(key) } else { @@ -107,7 +107,7 @@ func (s *Session) RemoveAll() error { return nil } s.init() - if err := s.manager.storage.RemoveAll(); err != nil { + if err := s.manager.storage.RemoveAll(s.id); err != nil { if err == ErrorDisabled { s.data.Clear() } else { @@ -130,7 +130,7 @@ func (s *Session) Id() string { func (s *Session) Map() map[string]interface{} { if len(s.id) > 0 { s.init() - if data := s.manager.storage.GetMap(); data != nil { + if data := s.manager.storage.GetMap(s.id); data != nil { return data } return s.data.Map() @@ -195,7 +195,7 @@ func (s *Session) Get(key string, def ...interface{}) interface{} { return nil } s.init() - if v := s.manager.storage.Get(key); v != nil { + if v := s.manager.storage.Get(s.id, key); v != nil { return v } if v := s.data.Get(key); v != nil { diff --git a/os/gsession/gsession_storage.go b/os/gsession/gsession_storage.go index 6101a0ba1..11a538cd7 100644 --- a/os/gsession/gsession_storage.go +++ b/os/gsession/gsession_storage.go @@ -11,21 +11,21 @@ import "time" type Storage interface { // Get retrieves session value with given key. // It returns nil if the key does not exist in the session. - Get(key string) interface{} + Get(id string, key string) interface{} // GetMap retrieves all key-value pairs as map from storage. - GetMap() map[string]interface{} + GetMap(id string) map[string]interface{} // GetSize retrieves the size of key-value pairs from storage. GetSize(id string) int // Set sets key-value session pair to the storage. - Set(key string, value interface{}) error + Set(id string, key string, value interface{}) error // SetMap batch sets key-value session pairs with map to the storage. - SetMap(data map[string]interface{}) error + SetMap(id string, data map[string]interface{}) error // Remove deletes key with its value from storage. - Remove(key string) error + Remove(id string, key string) error // RemoveAll deletes all key-value pairs from storage. - RemoveAll() error + RemoveAll(id string) error // GetSession returns the session data map for given session id. // The parameter specifies the TTL for this session. diff --git a/os/gsession/gsession_storage_file.go b/os/gsession/gsession_storage_file.go index 210e1b78a..0f4b810e0 100644 --- a/os/gsession/gsession_storage_file.go +++ b/os/gsession/gsession_storage_file.go @@ -103,12 +103,12 @@ func (s *StorageFile) sessionFilePath(id string) string { // Get retrieves session value with given key. // It returns nil if the key does not exist in the session. -func (s *StorageFile) Get(key string) interface{} { +func (s *StorageFile) Get(id string, key string) interface{} { return nil } // GetMap retrieves all key-value pairs as map from storage. -func (s *StorageFile) GetMap() map[string]interface{} { +func (s *StorageFile) GetMap(id string) map[string]interface{} { return nil } @@ -118,22 +118,22 @@ func (s *StorageFile) GetSize(id string) int { } // Set sets key-value session pair to the storage. -func (s *StorageFile) Set(key string, value interface{}) error { +func (s *StorageFile) Set(id string, key string, value interface{}) error { return ErrorDisabled } // SetMap batch sets key-value session pairs with map to the storage. -func (s *StorageFile) SetMap(data map[string]interface{}) error { +func (s *StorageFile) SetMap(id string, data map[string]interface{}) error { return ErrorDisabled } // Remove deletes key with its value from storage. -func (s *StorageFile) Remove(key string) error { +func (s *StorageFile) Remove(id string, key string) error { return ErrorDisabled } // RemoveAll deletes all key-value pairs from storage. -func (s *StorageFile) RemoveAll() error { +func (s *StorageFile) RemoveAll(id string) error { return ErrorDisabled } diff --git a/util/gconv/gconv_slice.go b/util/gconv/gconv_slice.go index 4157e90b9..125065fb5 100644 --- a/util/gconv/gconv_slice.go +++ b/util/gconv/gconv_slice.go @@ -526,13 +526,13 @@ func Interfaces(i interface{}) []interface{} { } case reflect.Struct: rt := rv.Type() - array = make([]interface{}, rv.NumField()) + array = make([]interface{}, 0) for i := 0; i < rv.NumField(); i++ { // Only public attributes. if !utilstr.IsLetterUpper(rt.Field(i).Name[0]) { continue } - array[i] = rv.Field(i).Interface() + array = append(array, rv.Field(i).Interface()) } default: return []interface{}{i}