Skip to content

Commit

Permalink
refactor: Format "ID"
Browse files Browse the repository at this point in the history
  • Loading branch information
miilord committed Dec 12, 2023
1 parent 349930f commit a39e2a3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestRepo_UpdateByID(t *testing.T) {
require.NoError(t, err)

// Call the UpdateByID function
id := result.Id
id := result.ID
update := &TestUser{Age: 3}
modifiedCount, err := repo.UpdateByID(ctx, id, update)
require.NoError(t, err)
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestRepo_Get(t *testing.T) {
require.NotNil(t, user.UpdatedAt)

// Call the FindOne function
doc, err := repo.Get(ctx, user.Id)
doc, err := repo.Get(ctx, user.ID)
require.NoError(t, err)

// Validate that one document was retrieved
Expand Down
20 changes: 10 additions & 10 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import (

// DefaultField represents a structure with default fields for MongoDB documents.
type DefaultField struct {
Id primitive.ObjectID `bson:"_id,omitempty" json:"id"`
CreatedAt time.Time `bson:"created_at,omitempty" json:"createdAt"`
UpdatedAt time.Time `bson:"updated_at,omitempty" json:"updatedAt"`
ID primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
CreatedAt time.Time `bson:"created_at,omitempty" json:"created_at"`
UpdatedAt time.Time `bson:"updated_at,omitempty" json:"updated_at"`
}

// DefaultUpdatedAt sets the default value for the updatedAt field.
// DefaultUpdatedAt sets the default value for the UpdatedAt field.
func (df *DefaultField) DefaultUpdatedAt() {
df.UpdatedAt = time.Now()
}

// DefaultCreatedAt sets the default value for the createdAt field if it's zero.
// DefaultCreatedAt sets the default value for the CreatedAt field if it's zero.
func (df *DefaultField) DefaultCreatedAt() {
if df.CreatedAt.IsZero() {
df.CreatedAt = time.Now()
}
}

// DefaultId sets the default value for the _id field if it's zero.
func (df *DefaultField) DefaultId() {
if df.Id.IsZero() {
df.Id = primitive.NewObjectID()
// DefaultID sets the default value for the _id field if it's zero.
func (df *DefaultField) DefaultID() {
if df.ID.IsZero() {
df.ID = primitive.NewObjectID()
}
}

// BeforeInsert is a hook to set default field values before inserting a document.
func (df *DefaultField) BeforeInsert(ctx context.Context) {
df.DefaultId()
df.DefaultID()
df.DefaultCreatedAt()
df.DefaultUpdatedAt()
}
Expand Down
2 changes: 1 addition & 1 deletion field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestDefaultFieldHooks(t *testing.T) {
// Test the BeforeInsert hook
ctx := context.TODO()
df.BeforeInsert(ctx)
if df.Id.IsZero() {
if df.ID.IsZero() {
t.Fatalf("BeforeInsert did not set a default ID")
}
if df.CreatedAt.IsZero() {
Expand Down

0 comments on commit a39e2a3

Please sign in to comment.