session

package
v0.0.0-...-86906c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

func TestManager

func TestManager(t *testing.T, newRepo func() ReadWriter)

Types

type Data

type Data map[string]any

Data represents a sessions data as key/value pairs.

type JSONMemoryRepo

type JSONMemoryRepo struct {
	// contains filtered or unexported fields
}

JSONMemoryRepo implements an in-memory repo for use with a session manager. The intended use of this repo is developer tests.

We want to test against JSON here to make sure we handle numbers correctly, which is why we map to a byte slice

func NewJSONMemoryRepo

func NewJSONMemoryRepo(useNumber bool) *JSONMemoryRepo

NewJSONMemoryRepo returns a new in-memory session repo, intended for use in developer tests.

func (*JSONMemoryRepo) DestroySession

func (r *JSONMemoryRepo) DestroySession(ctx context.Context, id string) error

Destroy deletes a session by the given id from memory.

func (*JSONMemoryRepo) FindSessionDataByID

func (r *JSONMemoryRepo) FindSessionDataByID(ctx context.Context, id string) (Data, error)

FindByID attempts to find session data using the given id.

func (*JSONMemoryRepo) SaveSession

func (r *JSONMemoryRepo) SaveSession(ctx context.Context, s Session) error

Save persists the given session in-memory.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager loads, creates, and commits session data via contexts.

func NewManager

func NewManager(repo ReadWriter) *Manager

NewManager creates a new session manager that will use the provided repository to interact with session data.

func (*Manager) Commit

func (m *Manager) Commit(ctx context.Context) (string, error)

Commit uses the given context to save and/or destroy session data. Until Commit is called all session data only exists on a context.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, key string)

Delete will delete the session value at the given key. Deleting data will also set the session's status to modified.

func (*Manager) Destroy

func (m *Manager) Destroy(ctx context.Context)

Destroy sets the status of the session on the given context to destroyed. Any persisted session data is not actually destroyed until any changes have been committed.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, key string) any

Get will fetch the session value for the given key. If the key does not exist then the zero value is returned. Fetching data will set the session's status to accessed.

func (*Manager) GetBool

func (m *Manager) GetBool(ctx context.Context, key string) bool

GetBool fetches a bool from the session's data. If the given key does not exist then the zero value is returned.

func (*Manager) GetFloat32

func (m *Manager) GetFloat32(ctx context.Context, key string) float32

GetFloat32 fetches a float32 from the session's data. If the given key does not exist then the zero value is returned.

func (*Manager) GetFloat64

func (m *Manager) GetFloat64(ctx context.Context, key string) float64

GetFloat64 fetches a float64 from the session's data. If the given key does not exist then the zero value is returned.

func (*Manager) GetInt

func (m *Manager) GetInt(ctx context.Context, key string) int

GetInt fetches a int from the session's data. If the given key does not exist then the zero value is returned.

func (*Manager) GetString

func (m *Manager) GetString(ctx context.Context, key string) string

GetString fetches a string from the session's data. If the given key does not exist then the zero value is returned.

func (*Manager) GetStrings

func (m *Manager) GetStrings(ctx context.Context, key string) []string

GetString fetches a string slice from the session's data. If the given key does not exist then the zero value is returned.

func (*Manager) GetTime

func (m *Manager) GetTime(ctx context.Context, key string) time.Time

GetTime fetches a time.Time from the session's data. If the given key does not exist then the zero value is returned.

func (*Manager) Has

func (m *Manager) Has(ctx context.Context, key string) bool

Has checks to see whether the given key exists in the session's data.

func (*Manager) Load

func (m *Manager) Load(ctx context.Context, id string) (context.Context, error)

Load attempts to load a session using the given id into the given context.

If a session with the provided id is not found then a new session is created with a new id.

After a session has been loaded or created it is set on the returned context.

func (*Manager) PopBool

func (m *Manager) PopBool(ctx context.Context, key string) bool

PopBool fetches a bool from the session's data. After fetching the value it is then deleted from the session. If the given key does not exist then the zero value is returned.

func (*Manager) PopFloat32

func (m *Manager) PopFloat32(ctx context.Context, key string) float32

PopFloat32 fetches a float32 from the session's data. After fetching the value it is then deleted from the session. If the given key does not exist then the zero value is returned.

func (*Manager) PopFloat64

func (m *Manager) PopFloat64(ctx context.Context, key string) float64

PopFloat64 fetches a float64 from the session's data. After fetching the value it is then deleted from the session. If the given key does not exist then the zero value is returned.

func (*Manager) PopInt

func (m *Manager) PopInt(ctx context.Context, key string) int

PopInt fetches a int from the session's data. After fetching the value it is then deleted from the session. If the given key does not exist then the zero value is returned.

func (*Manager) PopString

func (m *Manager) PopString(ctx context.Context, key string) string

PopString fetches a string from the session's data. After fetching the value it is then deleted from the session. If the given key does not exist then the zero value is returned.

func (*Manager) PopStrings

func (m *Manager) PopStrings(ctx context.Context, key string) []string

PopString fetches a string slice from the session's data. After fetching the value it is then deleted from the session. If the given key does not exist then the zero value is returned.

func (*Manager) PopTime

func (m *Manager) PopTime(ctx context.Context, key string) time.Time

PopTime fetches a time.Time from the session's data. After fetching the value it is then deleted from the session. If the given key does not exist then the zero value is returned.

func (*Manager) Renew

func (m *Manager) Renew(ctx context.Context) error

Renew will update the id of the session on the given context. It will also track the original session id so it can be destroyed when session data is committed. Renewing a session's id will also set the session's status to modified.

func (*Manager) Set

func (m *Manager) Set(ctx context.Context, key string, value any)

Set will set a key/value pair of session data in the session on the given context. Setting a value will also change the session's state to modified.

func (*Manager) Status

func (m *Manager) Status(ctx context.Context) Status

Status returns the status of the session on the given context.

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

ReadWriter is the combination of the Reader and Writer interfaces.

type Reader

type Reader interface {
	FindSessionDataByID(ctx context.Context, id string) (Data, error)
}

Reader defines the interface for reading session data.

type Session

type Session struct {
	ID     string
	Data   Data
	Status Status
	// contains filtered or unexported fields
}

type Status

type Status int
const (
	Unchanged Status = iota
	Accessed
	Modified
	Destroyed
)

type Writer

type Writer interface {
	SaveSession(ctx context.Context, s Session) error
	DestroySession(ctx context.Context, id string) error
}

Reader defines the interface for writing session data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL