mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
This pull request standardizes the use of the Go 1.18+ `any` type alias
instead of `interface{}` throughout the codebase. The change improves
code readability and aligns with modern Go best practices. The update
touches many files, including core data structures, code generation
templates, logging utilities, and test data, ensuring consistency across
all usages.
**Type alias migration to `any`:**
* Replaced all instances of `interface{}` with `any` in core data
structures such as `garray` and in generated model structs (e.g.,
`TableUser`, `User1`, `User2`) to modernize type usage.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
* Updated function signatures, method parameters, and return types from
`interface{}` to `any` in various parts of the codebase, including code
generation, service logic, and logging utilities (e.g., `mlog`).
[[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74)
[[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
[[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107)
[[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121)
[[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112)
[[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36)
[[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74)
[[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96)
**Generated code and templates:**
* Adjusted generated files and code generation templates to output `any`
instead of `interface{}` for relevant struct fields and function
signatures, ensuring that new code generation aligns with the updated
convention.
[[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
[[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
**Container and utility updates:**
* Refactored the `garray` container implementation and related
constructors/methods to use `[]any` instead of `[]interface{}`, along
with corresponding function signatures.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52)
[[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62)
[[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86)
[[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97)
[[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114)
[[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124)
[[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143)
[[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167)
These changes collectively modernize the codebase and prepare it for
future Go developments by using the idiomatic `any` type.
414 lines
11 KiB
Go
414 lines
11 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gsession
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/container/gmap"
|
|
"github.com/gogf/gf/v2/container/gvar"
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
)
|
|
|
|
// Session struct for storing single session data, which is bound to a single request.
|
|
// The Session struct is the interface with user, but the Storage is the underlying adapter designed interface
|
|
// for functionality implements.
|
|
type Session struct {
|
|
id string // Session id. It retrieves the session if id is custom specified.
|
|
ctx context.Context // Context for current session. Please note that, session lives along with context.
|
|
data *gmap.StrAnyMap // Current Session data, which is retrieved from Storage.
|
|
dirty bool // Used to mark session is modified.
|
|
start bool // Used to mark session is started.
|
|
manager *Manager // Parent session Manager.
|
|
|
|
// idFunc is a callback function used for creating custom session id.
|
|
// This is called if session id is empty ever when session starts.
|
|
idFunc func(ttl time.Duration) (id string)
|
|
}
|
|
|
|
// init does the lazy initialization for session, which retrieves the session if session id is specified,
|
|
// or else it creates a new empty session.
|
|
func (s *Session) init() error {
|
|
if s.start {
|
|
return nil
|
|
}
|
|
var err error
|
|
// Session retrieving.
|
|
if s.id != "" {
|
|
// Retrieve stored session data from storage.
|
|
if s.manager.storage != nil {
|
|
s.data, err = s.manager.storage.GetSession(s.ctx, s.id, s.manager.GetTTL())
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
intlog.Errorf(s.ctx, `session restoring failed for id "%s": %+v`, s.id, err)
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
// Session id creation.
|
|
if s.id == "" {
|
|
if s.idFunc != nil {
|
|
// Use custom session id creating function.
|
|
s.id = s.idFunc(s.manager.ttl)
|
|
} else {
|
|
// Use default session id creating function of storage.
|
|
s.id, err = s.manager.storage.New(s.ctx, s.manager.ttl)
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
intlog.Errorf(s.ctx, "create session id failed: %+v", err)
|
|
return err
|
|
}
|
|
// If session storage does not implements id generating functionality,
|
|
// it then uses default session id creating function.
|
|
if s.id == "" {
|
|
s.id = NewSessionId()
|
|
}
|
|
}
|
|
}
|
|
if s.data == nil {
|
|
s.data = gmap.NewStrAnyMap(true)
|
|
}
|
|
s.start = true
|
|
return nil
|
|
}
|
|
|
|
// Close closes current session and updates its ttl in the session manager.
|
|
// If this session is dirty, it also exports it to storage.
|
|
//
|
|
// NOTE that this function must be called ever after a session request done.
|
|
func (s *Session) Close() error {
|
|
if s.manager.storage == nil {
|
|
return nil
|
|
}
|
|
if s.start && s.id != "" {
|
|
size := s.data.Size()
|
|
if s.dirty {
|
|
err := s.manager.storage.SetSession(s.ctx, s.id, s.data, s.manager.ttl)
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
return err
|
|
}
|
|
} else if size > 0 {
|
|
err := s.manager.storage.UpdateTTL(s.ctx, s.id, s.manager.ttl)
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Set sets key-value pair to this session.
|
|
func (s *Session) Set(key string, value any) (err error) {
|
|
if err = s.init(); err != nil {
|
|
return err
|
|
}
|
|
if err = s.manager.storage.Set(s.ctx, s.id, key, value, s.manager.ttl); err != nil {
|
|
if !gerror.Is(err, ErrorDisabled) {
|
|
return err
|
|
}
|
|
s.data.Set(key, value)
|
|
}
|
|
s.dirty = true
|
|
return nil
|
|
}
|
|
|
|
// SetMap batch sets the session using map.
|
|
func (s *Session) SetMap(data map[string]any) (err error) {
|
|
if err = s.init(); err != nil {
|
|
return err
|
|
}
|
|
if err = s.manager.storage.SetMap(s.ctx, s.id, data, s.manager.ttl); err != nil {
|
|
if !gerror.Is(err, ErrorDisabled) {
|
|
return err
|
|
}
|
|
s.data.Sets(data)
|
|
}
|
|
s.dirty = true
|
|
return nil
|
|
}
|
|
|
|
// Remove removes key along with its value from this session.
|
|
func (s *Session) Remove(keys ...string) (err error) {
|
|
if s.id == "" {
|
|
return nil
|
|
}
|
|
if err = s.init(); err != nil {
|
|
return err
|
|
}
|
|
for _, key := range keys {
|
|
if err = s.manager.storage.Remove(s.ctx, s.id, key); err != nil {
|
|
if !gerror.Is(err, ErrorDisabled) {
|
|
return err
|
|
}
|
|
s.data.Remove(key)
|
|
}
|
|
}
|
|
s.dirty = true
|
|
return nil
|
|
}
|
|
|
|
// RemoveAll deletes all key-value pairs from this session.
|
|
func (s *Session) RemoveAll() (err error) {
|
|
if s.id == "" {
|
|
return nil
|
|
}
|
|
if err = s.init(); err != nil {
|
|
return err
|
|
}
|
|
if err = s.manager.storage.RemoveAll(s.ctx, s.id); err != nil {
|
|
if !gerror.Is(err, ErrorDisabled) {
|
|
return err
|
|
}
|
|
}
|
|
// Remove data from memory.
|
|
if s.data != nil {
|
|
s.data.Clear()
|
|
}
|
|
s.dirty = true
|
|
return nil
|
|
}
|
|
|
|
// Id returns the session id for this session.
|
|
// It creates and returns a new session id if the session id is not passed in initialization.
|
|
func (s *Session) Id() (id string, err error) {
|
|
if err = s.init(); err != nil {
|
|
return "", err
|
|
}
|
|
return s.id, nil
|
|
}
|
|
|
|
// SetId sets custom session before session starts.
|
|
// It returns error if it is called after session starts.
|
|
func (s *Session) SetId(id string) error {
|
|
if s.start {
|
|
return gerror.NewCode(gcode.CodeInvalidOperation, "session already started")
|
|
}
|
|
s.id = id
|
|
return nil
|
|
}
|
|
|
|
// SetIdFunc sets custom session id creating function before session starts.
|
|
// It returns error if it is called after session starts.
|
|
func (s *Session) SetIdFunc(f func(ttl time.Duration) string) error {
|
|
if s.start {
|
|
return gerror.NewCode(gcode.CodeInvalidOperation, "session already started")
|
|
}
|
|
s.idFunc = f
|
|
return nil
|
|
}
|
|
|
|
// Data returns all data as map.
|
|
// Note that it's using value copy internally for concurrent-safe purpose.
|
|
func (s *Session) Data() (sessionData map[string]any, err error) {
|
|
if s.id == "" {
|
|
return map[string]any{}, nil
|
|
}
|
|
if err = s.init(); err != nil {
|
|
return nil, err
|
|
}
|
|
sessionData, err = s.manager.storage.Data(s.ctx, s.id)
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
intlog.Errorf(s.ctx, `%+v`, err)
|
|
}
|
|
if sessionData != nil {
|
|
return sessionData, nil
|
|
}
|
|
return s.data.Map(), nil
|
|
}
|
|
|
|
// Size returns the size of the session.
|
|
func (s *Session) Size() (size int, err error) {
|
|
if s.id == "" {
|
|
return 0, nil
|
|
}
|
|
if err = s.init(); err != nil {
|
|
return 0, err
|
|
}
|
|
size, err = s.manager.storage.GetSize(s.ctx, s.id)
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
intlog.Errorf(s.ctx, `%+v`, err)
|
|
}
|
|
if size > 0 {
|
|
return size, nil
|
|
}
|
|
return s.data.Size(), nil
|
|
}
|
|
|
|
// Contains checks whether key exist in the session.
|
|
func (s *Session) Contains(key string) (ok bool, err error) {
|
|
if s.id == "" {
|
|
return false, nil
|
|
}
|
|
if err = s.init(); err != nil {
|
|
return false, err
|
|
}
|
|
v, err := s.Get(key)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return !v.IsNil(), nil
|
|
}
|
|
|
|
// IsDirty checks whether there's any data changes in the session.
|
|
func (s *Session) IsDirty() bool {
|
|
return s.dirty
|
|
}
|
|
|
|
// Get retrieves session value with given key.
|
|
// It returns `def` if the key does not exist in the session if `def` is given,
|
|
// or else it returns nil.
|
|
func (s *Session) Get(key string, def ...any) (value *gvar.Var, err error) {
|
|
if s.id == "" {
|
|
return nil, nil
|
|
}
|
|
if err = s.init(); err != nil {
|
|
return nil, err
|
|
}
|
|
v, err := s.manager.storage.Get(s.ctx, s.id, key)
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
intlog.Errorf(s.ctx, `%+v`, err)
|
|
return nil, err
|
|
}
|
|
if v != nil {
|
|
return gvar.New(v), nil
|
|
}
|
|
if v = s.data.Get(key); v != nil {
|
|
return gvar.New(v), nil
|
|
}
|
|
if len(def) > 0 {
|
|
return gvar.New(def[0]), nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// MustId performs as function Id, but it panics if any error occurs.
|
|
func (s *Session) MustId() string {
|
|
id, err := s.Id()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// MustGet performs as function Get, but it panics if any error occurs.
|
|
func (s *Session) MustGet(key string, def ...any) *gvar.Var {
|
|
v, err := s.Get(key, def...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// MustSet performs as function Set, but it panics if any error occurs.
|
|
func (s *Session) MustSet(key string, value any) {
|
|
err := s.Set(key, value)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// MustSetMap performs as function SetMap, but it panics if any error occurs.
|
|
func (s *Session) MustSetMap(data map[string]any) {
|
|
err := s.SetMap(data)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// MustContains performs as function Contains, but it panics if any error occurs.
|
|
func (s *Session) MustContains(key string) bool {
|
|
b, err := s.Contains(key)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
// MustData performs as function Data, but it panics if any error occurs.
|
|
func (s *Session) MustData() map[string]any {
|
|
m, err := s.Data()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// MustSize performs as function Size, but it panics if any error occurs.
|
|
func (s *Session) MustSize() int {
|
|
size, err := s.Size()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return size
|
|
}
|
|
|
|
// MustRemove performs as function Remove, but it panics if any error occurs.
|
|
func (s *Session) MustRemove(keys ...string) {
|
|
err := s.Remove(keys...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// RegenerateId regenerates a new session id for current session.
|
|
// It keeps the session data and updates the session id with a new one.
|
|
// This is commonly used to prevent session fixation attacks and increase security.
|
|
//
|
|
// The parameter `deleteOld` specifies whether to delete the old session data:
|
|
// - If true: the old session data will be deleted immediately
|
|
// - If false: the old session data will be kept and expire according to its TTL
|
|
func (s *Session) RegenerateId(deleteOld bool) (newId string, err error) {
|
|
if err = s.init(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Generate new session id
|
|
if s.idFunc != nil {
|
|
newId = s.idFunc(s.manager.ttl)
|
|
} else {
|
|
newId, err = s.manager.storage.New(s.ctx, s.manager.ttl)
|
|
if err != nil && !gerror.Is(err, ErrorDisabled) {
|
|
return "", err
|
|
}
|
|
if newId == "" {
|
|
newId = NewSessionId()
|
|
}
|
|
}
|
|
|
|
// If using storage, need to copy data to new id
|
|
if s.manager.storage != nil {
|
|
if err = s.manager.storage.SetSession(s.ctx, newId, s.data, s.manager.ttl); err != nil {
|
|
if !gerror.Is(err, ErrorDisabled) {
|
|
return "", err
|
|
}
|
|
}
|
|
// Delete old session data if requested
|
|
if deleteOld {
|
|
if err = s.manager.storage.RemoveAll(s.ctx, s.id); err != nil {
|
|
if !gerror.Is(err, ErrorDisabled) {
|
|
return "", err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update session id
|
|
s.id = newId
|
|
s.dirty = true
|
|
return newId, nil
|
|
}
|
|
|
|
// MustRegenerateId performs as function RegenerateId, but it panics if any error occurs.
|
|
func (s *Session) MustRegenerateId(deleteOld bool) string {
|
|
newId, err := s.RegenerateId(deleteOld)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return newId
|
|
}
|