fix: use simple slices instead of []*T

This commit is contained in:
Muyao CHEN 2024-10-24 22:32:57 +02:00
parent de7c6f7223
commit 716a58d44c
6 changed files with 45 additions and 13 deletions

View File

@ -564,3 +564,16 @@ querier.
Since this repo layer is just a wrapping layer between the `sqlc.models` and
my own models, I can extract the conversion part to functions and test them.
I'm not testing the whole thing but I test what I can.
### 2024/10/24
When writing the tests. I am asking myself the differences between `[]T`,
`[]*T` and `*[]T`.
`*[]T` is simple, it is a reference to the original slice. So modifying it
means modifying the original slice.
But between `[]*T` and `[]T`, the only difference that I see (pointed out by
`ChatGPT`) is how the memory is allocated. With `[]T` it might be better for
the GC to deal with the memory free. I thing for my project I will stick to
`[]T`.

View File

@ -62,7 +62,7 @@ func convToEventRetrieved(eventDTO *sqlc.GetEventByIDRow) (*model.EventRetrieved
return nil, err
}
var users []*model.UserBaseRetrieved
var users []model.UserBaseRetrieved
err = json.Unmarshal(eventDTO.Users, &users)
if err != nil {
// Unexpected
@ -99,8 +99,8 @@ func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.Even
return convToEventRetrieved(&eventDTO)
}
func convToEventList(eventsDTO []sqlc.ListEventsByUserIDRow) ([]*model.EventListRetrieved, error) {
var events []*model.EventListRetrieved
func convToEventList(eventsDTO []sqlc.ListEventsByUserIDRow) ([]model.EventListRetrieved, error) {
var events []model.EventListRetrieved
for _, evDTO := range eventsDTO {
var owner model.UserBaseRetrieved
@ -110,7 +110,7 @@ func convToEventList(eventsDTO []sqlc.ListEventsByUserIDRow) ([]*model.EventList
log.ErrorLog("json unmarshal error", "err", err)
return nil, err
}
ev := &model.EventListRetrieved{
ev := model.EventListRetrieved{
ID: int(evDTO.ID),
Name: evDTO.Name,
Description: evDTO.Description.String,
@ -128,7 +128,7 @@ func convToEventList(eventsDTO []sqlc.ListEventsByUserIDRow) ([]*model.EventList
func (e *eventRepository) ListEventsByUserID(
ctx context.Context,
userID int,
) ([]*model.EventListRetrieved, error) {
) ([]model.EventListRetrieved, error) {
eventsDTO, err := e.queries.ListEventsByUserID(ctx, int32(userID))
if err != nil {
log.ErrorLog("query error", "err", err)

View File

@ -59,7 +59,7 @@ func TestConvToEventRetrieved(t *testing.T) {
FirstName: "owner",
LastName: "owner",
},
Users: []*model.UserBaseRetrieved{
Users: []model.UserBaseRetrieved{
{
ID: 1,
FirstName: "owner",
@ -91,7 +91,7 @@ func TestConvToEventList(t *testing.T) {
},
}
want := []*model.EventListRetrieved{
want := []model.EventListRetrieved{
{
ID: 123,
Name: "event",

View File

@ -57,7 +57,7 @@ type EventInfoResponse struct {
CreatedAt time.Time
UpdatedAt time.Time
Users []*UserBaseResponse
Users []UserBaseResponse
}
// }}}
@ -93,7 +93,7 @@ type EventRetrieved struct {
Name string
Description string
Users []*UserBaseRetrieved
Users []UserBaseRetrieved
TotalAmount Money
DefaultCurrency Currency
@ -121,9 +121,9 @@ type Event struct {
Description string
// lazy get using participation join
Users []*UserDO
Users []UserDO
// lazy get
Expenses []*Expense
Expenses []Expense
TotalAmount Money
DefaultCurrency Currency

View File

@ -53,7 +53,26 @@ type ExpenseGetResponse Expense
// }}}
// {{{ Retrieved
type ExpensesListRetrieved ExpensesListResponse
type (
ExpensesListRetrieved ExpensesListResponse
ExpenseRetrieved Expense
)
type PaymentRetrieved struct {
PayerID int `json:"payer_id"`
PayerFirstName string `json:"payer_first_name"`
PayerLastName string `json:"payer_last_name"`
Amount int `json:"amount"`
Currency string `json:"currency"`
}
type BenefitRetrieved struct {
RecipientID int `json:"recipient_id"`
RecipientFirstName string `json:"recipient_first_name"`
RecipientLastName string `json:"recipient_last_name"`
Amount int `json:"amount"`
Currency string `json:"currency"`
}
// }}}
// {{{ Entity

View File

@ -37,7 +37,7 @@ type EventRepository interface {
GetByID(ctx context.Context, eventID int) (*model.EventRetrieved, error)
// related to events of a user
ListEventsByUserID(ctx context.Context, userID int) ([]*model.EventListRetrieved, error)
ListEventsByUserID(ctx context.Context, userID int) ([]model.EventListRetrieved, error)
}
type ExpenseRepository interface {