Skip to content

Commit

Permalink
fix: exclude unexported default fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lxnre-codes committed Oct 17, 2023
1 parent ca9733d commit 34823fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
32 changes: 22 additions & 10 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,45 @@ type IDefaultSchema interface {
// DefaultSchema is a struct that implements the IDefaultSchema interface.
// It contains the default fields (ID,CreatedAt,UpdatedAt) that are added to a document.
type DefaultSchema struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"`
ID *primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
CreatedAt *time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"`
}

func (s *DefaultSchema) GenerateID() {
s.ID = primitive.NewObjectID()
id := primitive.NewObjectID()
s.ID = &id
}

func (s *DefaultSchema) GenerateCreatedAt() {
s.CreatedAt = time.Now()
c := time.Now()
s.CreatedAt = &c
}

func (s *DefaultSchema) GenerateUpdatedAt() {
s.UpdatedAt = time.Now()
u := time.Now()
s.UpdatedAt = &u
}

func (s DefaultSchema) GetID() primitive.ObjectID {
return s.ID
if s.ID == nil {
return primitive.NilObjectID
}
return *s.ID
}

func (s DefaultSchema) GetCreatedAt() time.Time {
return s.CreatedAt
if s.CreatedAt == nil {
return time.Time{}
}
return *s.CreatedAt
}

func (s DefaultSchema) GetUpdatedAt() time.Time {
return s.UpdatedAt
if s.UpdatedAt == nil {
return time.Time{}
}
return *s.UpdatedAt
}

func (s DefaultSchema) GetUpdatedAtTag(t string) string {
Expand All @@ -91,7 +103,7 @@ func (s DefaultSchema) GetUpdatedAtTag(t string) string {
// }

func (s *DefaultSchema) SetUpdatedAt(t time.Time) {
s.UpdatedAt = t
s.UpdatedAt = &t
}

// Document is a struct that represents a document in a MongoDB collection.
Expand Down
3 changes: 3 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

// SessionFunc is a callback function that's executed within a session transaction.
type SessionFunc func(sessCtx mongo.SessionContext) (interface{}, error)

// SessionLike is a Union type of all mongo types that a seeion can be derived from.
type SessionLike interface {
*mongo.Database | *mongo.Collection | *mongo.SessionContext | *mongo.Client | *mongo.Session
}
Expand Down Expand Up @@ -424,6 +426,7 @@ func (model *Model[T, P]) UpdateMany(ctx context.Context, query bson.M, update b

// WithTransaction executes the callback function in a transaction.
// When a transaction is started with [mongo.SessionContext] options are ignored because the session is already created.
// This method only closes the session if it was created by this method.
func WithTransaction[T SessionLike](ctx context.Context, sess T, fn SessionFunc, opts ...*options.TransactionOptions) (any, error) {
var session mongo.Session
var err error
Expand Down

0 comments on commit 34823fb

Please sign in to comment.