Closed hotrungnhan closed 2 years ago
Hi @hotrungnhan
We can not customize docs based on specific needs.
But you have another solution, the base mgm structs are just simple hook handlers. you can add your own base model and use it instead of our base models (and even after any update from mgm
you can update your base model with the new changes if wanted)
e.g.,
package app
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// DefaultModel struct contains a model's default fields.
type DefaultModel struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
}
// PrepareID method prepares the ID value to be used for filtering
// e.g convert hex-string ID value to bson.ObjectId
func (f *DefaultModel) PrepareID(id interface{}) (interface{}, error) {
if idStr, ok := id.(string); ok {
return primitive.ObjectIDFromHex(idStr)
}
// Otherwise id must be ObjectId
return id, nil
}
func (f *DefaultModel) GetID() interface{} {
return f.ID
}
func (f *DefaultModel) SetID(id interface{}) {
f.ID = id.(primitive.ObjectID)
}
func (f *DefaultModel) Creating() error {
f.CreatedAt = time.Now().UTC()
return nil
}
func (f *DefaultModel) Saving() error {
f.UpdatedAt = time.Now().UTC()
return nil
}
// Define your models:
package app
type Book struct {
DefaultModel `bson:",inline"`
Name string `json:"name" bson:"name"`
Pages int `json:"pages" bson:"pages"`
}
Is your feature request related to a problem? Please describe. My job now requires me to use swaggo to generate struct to open API documents. But there is an issue that we can't customize struct tag in the default MGM base model.
Describe the solution you'd like Could you please add id and DateTime swaggo struct tag to the base model? Ex:
if maintain team agree with me , I will take responsibility for working on it and contribute it soon. https://github.com/Kamva/mgm/blob/20296397b184f1cc4b942407deb0b3eb36b853ac/field.go#L10-L19