From 92e9b8722719cc226db3e24fb4d0b8a5fa4116b8 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Wed, 4 Jun 2025 12:32:47 +0200 Subject: [PATCH] feat: better types overriding --- internal/repository/api_services.sql.go | 29 ++++---- internal/repository/models.go | 43 ++++++------ internal/repository/users.sql.go | 5 +- sqlc.yaml | 92 ++++++++++++++++++++++++- 4 files changed, 126 insertions(+), 43 deletions(-) diff --git a/internal/repository/api_services.sql.go b/internal/repository/api_services.sql.go index d795101..e8d9d66 100644 --- a/internal/repository/api_services.sql.go +++ b/internal/repository/api_services.sql.go @@ -9,7 +9,6 @@ import ( "context" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgtype" ) const activateApiService = `-- name: ActivateApiService :exec @@ -33,14 +32,14 @@ INSERT INTO api_services ( ` type CreateApiServiceParams struct { - ClientID string `json:"client_id"` - ClientSecret string `json:"client_secret"` - Name string `json:"name"` - Description pgtype.Text `json:"description"` - RedirectUris []string `json:"redirect_uris"` - Scopes []string `json:"scopes"` - GrantTypes []string `json:"grant_types"` - IsActive bool `json:"is_active"` + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` + Name string `json:"name"` + Description *string `json:"description"` + RedirectUris []string `json:"redirect_uris"` + Scopes []string `json:"scopes"` + GrantTypes []string `json:"grant_types"` + IsActive bool `json:"is_active"` } func (q *Queries) CreateApiService(ctx context.Context, arg CreateApiServiceParams) (ApiService, error) { @@ -185,12 +184,12 @@ RETURNING id, client_id, client_secret, name, redirect_uris, scopes, grant_types ` type UpdateApiServiceParams struct { - ClientID string `json:"client_id"` - Name string `json:"name"` - Description pgtype.Text `json:"description"` - RedirectUris []string `json:"redirect_uris"` - Scopes []string `json:"scopes"` - GrantTypes []string `json:"grant_types"` + ClientID string `json:"client_id"` + Name string `json:"name"` + Description *string `json:"description"` + RedirectUris []string `json:"redirect_uris"` + Scopes []string `json:"scopes"` + GrantTypes []string `json:"grant_types"` } func (q *Queries) UpdateApiService(ctx context.Context, arg UpdateApiServiceParams) (ApiService, error) { diff --git a/internal/repository/models.go b/internal/repository/models.go index a750278..2f18de7 100644 --- a/internal/repository/models.go +++ b/internal/repository/models.go @@ -8,32 +8,31 @@ import ( "time" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgtype" ) type ApiService struct { - ID uuid.UUID `json:"id"` - ClientID string `json:"client_id"` - ClientSecret string `json:"client_secret"` - Name string `json:"name"` - RedirectUris []string `json:"redirect_uris"` - Scopes []string `json:"scopes"` - GrantTypes []string `json:"grant_types"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - IsActive bool `json:"is_active"` - Description pgtype.Text `json:"description"` + ID uuid.UUID `json:"id"` + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` + Name string `json:"name"` + RedirectUris []string `json:"redirect_uris"` + Scopes []string `json:"scopes"` + GrantTypes []string `json:"grant_types"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + IsActive bool `json:"is_active"` + Description *string `json:"description"` } type User struct { - ID uuid.UUID `json:"id"` - Email string `json:"email"` - FullName string `json:"full_name"` - PasswordHash string `json:"password_hash"` - IsAdmin bool `json:"is_admin"` - CreatedAt pgtype.Timestamptz `json:"created_at"` - UpdatedAt pgtype.Timestamptz `json:"updated_at"` - LastLogin pgtype.Timestamptz `json:"last_login"` - PhoneNumber pgtype.Text `json:"phone_number"` - ProfilePicture pgtype.Text `json:"profile_picture"` + ID uuid.UUID `json:"id"` + Email string `json:"email"` + FullName string `json:"full_name"` + PasswordHash string `json:"password_hash"` + IsAdmin bool `json:"is_admin"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + LastLogin *time.Time `json:"last_login"` + PhoneNumber *string `json:"phone_number"` + ProfilePicture *string `json:"profile_picture"` } diff --git a/internal/repository/users.sql.go b/internal/repository/users.sql.go index 176d07e..5de83ad 100644 --- a/internal/repository/users.sql.go +++ b/internal/repository/users.sql.go @@ -9,7 +9,6 @@ import ( "context" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgtype" ) const findAllUsers = `-- name: FindAllUsers :many @@ -126,8 +125,8 @@ WHERE id = $2 ` type UpdateProfilePictureParams struct { - ProfilePicture pgtype.Text `json:"profile_picture"` - ID uuid.UUID `json:"id"` + ProfilePicture *string `json:"profile_picture"` + ID uuid.UUID `json:"id"` } func (q *Queries) UpdateProfilePicture(ctx context.Context, arg UpdateProfilePictureParams) error { diff --git a/sqlc.yaml b/sqlc.yaml index 9e5d630..e43b741 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -1,4 +1,3 @@ - version: "2" sql: - engine: "postgresql" @@ -14,8 +13,95 @@ sql: - db_type: "uuid" go_type: import: "github.com/google/uuid" - type: "UUID" - - db_type: "timestamptz" + type: UUID + - db_type: "uuid" + nullable: true + go_type: + import: "github.com/google/uuid" + type: UUID + pointer: true + # ───── bool ────────────────────────────────────────── + - db_type: "pg_catalog.bool" # or just "bool" + go_type: { type: "bool" } + - db_type: "bool" # or just "bool" + go_type: { type: "bool" } + + - db_type: "pg_catalog.bool" + nullable: true + go_type: + type: "bool" + pointer: true # ⇒ *bool for NULLable columns + + - db_type: "bool" + nullable: true + go_type: + type: "bool" + pointer: true # ⇒ *bool for NULLable columns + + # ───── text ────────────────────────────────────────── + - db_type: "pg_catalog.text" + go_type: { type: "string" } + - db_type: "text" # or just "bool" + go_type: { type: "string" } + + - db_type: "pg_catalog.text" + nullable: true + go_type: + type: "string" + pointer: true # ⇒ *bool for NULLable columns + + - db_type: "text" + nullable: true + go_type: + type: "string" + pointer: true # ⇒ *bool for NULLable columns + + # ───── timestamp (WITHOUT TZ) ──────────────────────── + - db_type: "pg_catalog.timestamp" # or "timestamp" go_type: import: "time" type: "Time" + + - db_type: "timestamp" # or "timestamp" + go_type: + import: "time" + type: "Time" + + - db_type: "pg_catalog.timestamp" + nullable: true + go_type: + import: "time" + type: "Time" + pointer: true + + - db_type: "timestamp" + nullable: true + go_type: + import: "time" + type: "Time" + pointer: true + + # ───── timestamptz (WITH TZ) ───────────────────────── + - db_type: "pg_catalog.timestamptz" # or "timestamptz" + go_type: + import: "time" + type: "Time" + + - db_type: "timestamptz" # or "timestamptz" + go_type: + import: "time" + type: "Time" + + - db_type: "pg_catalog.timestamptz" + nullable: true + go_type: + import: "time" + type: "Time" + pointer: true + + - db_type: "timestamptz" + nullable: true + go_type: + import: "time" + type: "Time" + pointer: true