use distinct types for player and session ids

This commit is contained in:
2024-01-31 22:40:12 -06:00
parent d61c70da86
commit 73b5ac7960
8 changed files with 42 additions and 32 deletions

View File

@@ -85,7 +85,7 @@ func Login(ctx context.Context, db RowQuerier, user, pass string) (ID, error) {
slog.ErrorContext(ctx, "login failed", "user", user)
return ID{}, fmt.Errorf("invalid credentials")
}
return ID(id), nil
return ID{id}, nil
}
const initUsers = `CREATE TABLE shotgun_users (

View File

@@ -31,7 +31,7 @@ func TestLogin(t *testing.T) {
if err == nil {
t.Errorf("logging in nonexistent user didn't err")
}
if id != uuid.Nil {
if id.UUID != uuid.Nil {
t.Errorf("got nonzero user %v before registering", id)
}
@@ -43,7 +43,7 @@ func TestLogin(t *testing.T) {
if err != nil {
t.Errorf("couldn't login after registering: %v", err)
}
if id == uuid.Nil {
if id.UUID == uuid.Nil {
t.Errorf("got zero player id after registering")
}
@@ -51,7 +51,7 @@ func TestLogin(t *testing.T) {
if err == nil {
t.Errorf("logged in with wrong password")
}
if wrong != uuid.Nil {
if wrong.UUID != uuid.Nil {
t.Errorf("got nonzero user %v with wrong password (real is %v)", wrong, id)
}
}

View File

@@ -4,4 +4,6 @@ package player
import "github.com/google/uuid"
// ID is a unique ID for a player.
type ID = uuid.UUID
type ID struct {
uuid.UUID
}

View File

@@ -11,7 +11,9 @@ import (
)
// Session is a session ID.
type Session = uuid.UUID
type Session struct {
uuid.UUID
}
// InitSessions initializes an SQLite table relating player IDs to sessions.
func InitSessions(ctx context.Context, db Execer) error {
@@ -32,7 +34,7 @@ func StartSession(ctx context.Context, db Execer, p ID, now time.Time) (Session,
return Session{}, fmt.Errorf("couldn't start session: %w", err)
}
slog.InfoContext(ctx, "session started", "player", p, "session", id, "now", now)
return Session(id), nil
return Session{id}, nil
}
// FromSession gets the player ID associated with a session and updates the

View File

@@ -27,13 +27,13 @@ func TestSessions(t *testing.T) {
t.Fatalf("couldn't init sessions: %v", err)
}
p := player.ID{1}
p := player.ID{uuid.UUID{1}}
now := time.Unix(0, 0)
id, err := player.StartSession(ctx, conn, p, now)
if err != nil {
t.Fatalf("couldn't start session: %v", err)
}
if id == uuid.Nil {
if id.UUID == uuid.Nil {
t.Errorf("got zero session id at start")
}
@@ -41,7 +41,7 @@ func TestSessions(t *testing.T) {
if err == nil {
t.Errorf("no error on expired session")
}
if u != uuid.Nil {
if u.UUID != uuid.Nil {
t.Errorf("got nonzero player %v from expired session", u)
}
@@ -49,7 +49,7 @@ func TestSessions(t *testing.T) {
if err != nil {
t.Errorf("couldn't get player from session: %v", err)
}
if u == uuid.Nil {
if u.UUID == uuid.Nil {
t.Errorf("got zero player from session")
}
@@ -57,7 +57,7 @@ func TestSessions(t *testing.T) {
if err != nil {
t.Errorf("couldn't get player from extended session: %v", err)
}
if u == uuid.Nil {
if u.UUID == uuid.Nil {
t.Errorf("got zero player from extended session")
}
@@ -69,7 +69,7 @@ func TestSessions(t *testing.T) {
if err == nil {
t.Errorf("no error on logged out session")
}
if u != uuid.Nil {
if u.UUID != uuid.Nil {
t.Errorf("got nonzero player %v from logged out session", u)
}
}