aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-29 03:06:44 +0900
committernsfisis <nsfisis@gmail.com>2024-07-29 03:06:44 +0900
commit19b19e7f48d6375806f827713a98cffd2c57a434 (patch)
tree5b68e70a65a2434517c7e1ea5ea330fd686049d4 /backend
parentd1c8aa42aec32c8b042ae32d249df9c3c969453d (diff)
downloadphperkaigi-2025-albatross-19b19e7f48d6375806f827713a98cffd2c57a434.tar.gz
phperkaigi-2025-albatross-19b19e7f48d6375806f827713a98cffd2c57a434.tar.zst
phperkaigi-2025-albatross-19b19e7f48d6375806f827713a98cffd2c57a434.zip
refactor(backend): enable emit_pointers_for_null_types in sqlc
Diffstat (limited to 'backend')
-rw-r--r--backend/api/handlers.go30
-rw-r--r--backend/auth/jwt.go6
-rw-r--r--backend/db/models.go6
-rw-r--r--backend/db/query.sql.go28
-rw-r--r--backend/game/hub.go10
-rw-r--r--backend/sqlc.yaml1
6 files changed, 39 insertions, 42 deletions
diff --git a/backend/api/handlers.go b/backend/api/handlers.go
index 91cdbc8..cd8b3b5 100644
--- a/backend/api/handlers.go
+++ b/backend/api/handlers.go
@@ -73,14 +73,14 @@ func (h *ApiHandler) GetGames(ctx context.Context, request GetGamesRequestObject
startedAt = &startedAtTimestamp
}
var problem *Problem
- if row.ProblemID.Valid {
- if !row.Title.Valid || !row.Description.Valid {
+ if row.ProblemID != nil {
+ if row.Title == nil || row.Description == nil {
panic("inconsistent data")
}
problem = &Problem{
- ProblemId: int(row.ProblemID.Int32),
- Title: row.Title.String,
- Description: row.Description.String,
+ ProblemId: int(*row.ProblemID),
+ Title: *row.Title,
+ Description: *row.Description,
}
}
games[i] = Game{
@@ -108,14 +108,14 @@ func (h *ApiHandler) GetGames(ctx context.Context, request GetGamesRequestObject
startedAt = &startedAtTimestamp
}
var problem *Problem
- if row.ProblemID.Valid {
- if !row.Title.Valid || !row.Description.Valid {
+ if row.ProblemID != nil {
+ if row.Title == nil || row.Description == nil {
panic("inconsistent data")
}
problem = &Problem{
- ProblemId: int(row.ProblemID.Int32),
- Title: row.Title.String,
- Description: row.Description.String,
+ ProblemId: int(*row.ProblemID),
+ Title: *row.Title,
+ Description: *row.Description,
}
}
games[i] = Game{
@@ -147,14 +147,14 @@ func (h *ApiHandler) GetGamesGameId(ctx context.Context, request GetGamesGameIdR
startedAt = &startedAtTimestamp
}
var problem *Problem
- if row.ProblemID.Valid && GameState(row.State) != Closed && GameState(row.State) != WaitingEntries {
- if !row.Title.Valid || !row.Description.Valid {
+ if row.ProblemID != nil && GameState(row.State) != Closed && GameState(row.State) != WaitingEntries {
+ if row.Title == nil || row.Description == nil {
panic("inconsistent data")
}
problem = &Problem{
- ProblemId: int(row.ProblemID.Int32),
- Title: row.Title.String,
- Description: row.Description.String,
+ ProblemId: int(*row.ProblemID),
+ Title: *row.Title,
+ Description: *row.Description,
}
}
game := Game{
diff --git a/backend/auth/jwt.go b/backend/auth/jwt.go
index c15fc01..0b92155 100644
--- a/backend/auth/jwt.go
+++ b/backend/auth/jwt.go
@@ -19,15 +19,11 @@ type JWTClaims struct {
}
func NewJWT(user *db.User) (string, error) {
- var iconPath *string
- if user.IconPath.Valid {
- iconPath = &user.IconPath.String
- }
claims := &JWTClaims{
UserID: int(user.UserID),
Username: user.Username,
DisplayName: user.DisplayName,
- IconPath: iconPath,
+ IconPath: user.IconPath,
IsAdmin: user.IsAdmin,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)),
diff --git a/backend/db/models.go b/backend/db/models.go
index cc08817..35ebd03 100644
--- a/backend/db/models.go
+++ b/backend/db/models.go
@@ -15,7 +15,7 @@ type Game struct {
DurationSeconds int32
CreatedAt pgtype.Timestamp
StartedAt pgtype.Timestamp
- ProblemID pgtype.Int4
+ ProblemID *int32
}
type GamePlayer struct {
@@ -33,7 +33,7 @@ type User struct {
UserID int32
Username string
DisplayName string
- IconPath pgtype.Text
+ IconPath *string
IsAdmin bool
CreatedAt pgtype.Timestamp
}
@@ -42,5 +42,5 @@ type UserAuth struct {
UserAuthID int32
UserID int32
AuthType string
- PasswordHash pgtype.Text
+ PasswordHash *string
}
diff --git a/backend/db/query.sql.go b/backend/db/query.sql.go
index 1b9f392..2086815 100644
--- a/backend/db/query.sql.go
+++ b/backend/db/query.sql.go
@@ -25,10 +25,10 @@ type GetGameByIdRow struct {
DurationSeconds int32
CreatedAt pgtype.Timestamp
StartedAt pgtype.Timestamp
- ProblemID pgtype.Int4
- ProblemID_2 pgtype.Int4
- Title pgtype.Text
- Description pgtype.Text
+ ProblemID *int32
+ ProblemID_2 *int32
+ Title *string
+ Description *string
}
func (q *Queries) GetGameById(ctx context.Context, gameID int32) (GetGameByIdRow, error) {
@@ -60,13 +60,13 @@ type GetUserAuthByUsernameRow struct {
UserID int32
Username string
DisplayName string
- IconPath pgtype.Text
+ IconPath *string
IsAdmin bool
CreatedAt pgtype.Timestamp
UserAuthID int32
UserID_2 int32
AuthType string
- PasswordHash pgtype.Text
+ PasswordHash *string
}
func (q *Queries) GetUserAuthByUsername(ctx context.Context, username string) (GetUserAuthByUsernameRow, error) {
@@ -119,10 +119,10 @@ type ListGamesRow struct {
DurationSeconds int32
CreatedAt pgtype.Timestamp
StartedAt pgtype.Timestamp
- ProblemID pgtype.Int4
- ProblemID_2 pgtype.Int4
- Title pgtype.Text
- Description pgtype.Text
+ ProblemID *int32
+ ProblemID_2 *int32
+ Title *string
+ Description *string
}
func (q *Queries) ListGames(ctx context.Context) ([]ListGamesRow, error) {
@@ -170,10 +170,10 @@ type ListGamesForPlayerRow struct {
DurationSeconds int32
CreatedAt pgtype.Timestamp
StartedAt pgtype.Timestamp
- ProblemID pgtype.Int4
- ProblemID_2 pgtype.Int4
- Title pgtype.Text
- Description pgtype.Text
+ ProblemID *int32
+ ProblemID_2 *int32
+ Title *string
+ Description *string
GameID_2 int32
UserID int32
}
diff --git a/backend/game/hub.go b/backend/game/hub.go
index c61f2bb..d4a9231 100644
--- a/backend/game/hub.go
+++ b/backend/game/hub.go
@@ -240,14 +240,14 @@ func (hubs *GameHubs) RestoreFromDB(ctx context.Context) error {
startedAt = &row.StartedAt.Time
}
var problem_ *problem
- if row.ProblemID.Valid {
- if !row.Title.Valid || !row.Description.Valid {
+ if row.ProblemID != nil {
+ if row.Title == nil || row.Description == nil {
panic("inconsistent data")
}
problem_ = &problem{
- problemID: int(row.ProblemID.Int32),
- title: row.Title.String,
- description: row.Description.String,
+ problemID: int(*row.ProblemID),
+ title: *row.Title,
+ description: *row.Description,
}
}
hubs.hubs[int(row.GameID)] = newGameHub(ctx, &game{
diff --git a/backend/sqlc.yaml b/backend/sqlc.yaml
index 6fc3310..3b2d1d0 100644
--- a/backend/sqlc.yaml
+++ b/backend/sqlc.yaml
@@ -8,3 +8,4 @@ sql:
package: "db"
out: "db"
sql_package: "pgx/v5"
+ emit_pointers_for_null_types: true