-
Notifications
You must be signed in to change notification settings - Fork 2
Add EnsureParanoidIndices method #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't switch everything to pointer, there is no need
mongo/document/base.go
Outdated
@@ -13,11 +13,11 @@ type Base struct { | |||
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"` | |||
} | |||
|
|||
func (d Base) IsPersisted() bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, no need of pointer, nothing is modified
mongo/document/base.go
Outdated
return !d.CreatedAt.IsZero() | ||
} | ||
|
||
func (d Base) getID() bson.ObjectId { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need of pointer, nothing is modified
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then document Base
does not satisfy the document
interface as getID
is implemented for Base
but the ensure*
methods are for pointer of Base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go is smarter than that :P If the struct is passed as a pointer it can access non-pointer methods: https://play.golang.com/p/NicC6AlMbd7
(Not the over way around though: https://play.golang.com/p/o-eDYPih2V4)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep but interface does not work that way. If you have the following struct and interface:
type T struct {}
type I interface {
A()
B()
}
And you want the struct T
to implement the interface I
, you can NOT do the following:
func (t T) A() {}
func (t *T) B() {}
With this example, the struct T
does not satisfy the interface I
mongo/document/paranoid.go
Outdated
@@ -12,15 +12,15 @@ type Paranoid struct { | |||
DeletedAt *time.Time `bson:"deleted_at,omitempty" json:"deleted_at,omitempty"` | |||
} | |||
|
|||
func (p Paranoid) IsDeleted() bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No pointer
mongo/document/paranoid.go
Outdated
return p.DeletedAt != nil | ||
} | ||
|
||
func (d *Paranoid) setDeletedAt(t time.Time) { | ||
d.DeletedAt = &t | ||
} | ||
|
||
func (d Paranoid) scope(query bson.M) bson.M { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No pointer
@@ -22,14 +24,23 @@ type document interface { | |||
Validable | |||
} | |||
|
|||
var _ document = &Base{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No pointer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Base
implementation of the document
interface is on pointer. Cf. https://github.com/Scalingo/go-utils/pull/49/files#diff-9732770faadda6716a617078c80ccbe9L24
mongo/document/document.go
Outdated
@@ -221,3 +234,26 @@ func Update(ctx context.Context, collectionName string, update bson.M, doc docum | |||
log.WithField("query", update).Debugf("update %v", collectionName) | |||
return c.UpdateId(doc.getID(), update) | |||
} | |||
|
|||
func EnsureParanoidIndices(ctx context.Context, collectionNames ...string) { | |||
go func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why in a goroutine?
I think it's mandatory to have everything as a pointer. For instance, the setUpdatedAt(time.Time)
getID() bson.ObjectId The |
d8b7c4b
to
cb0a6aa
Compare
sorry...
Is it what you meant @johnsudaar ?
Fix #12