Skip to content

Commit

Permalink
feat: enhance model base service with BSON ID normalization
Browse files Browse the repository at this point in the history
- Added utility function to normalize BSON ObjectId in query parameters for gRPC methods.
- Updated GetOne, GetMany, DeleteOne, DeleteMany, UpdateOne, UpdateMany, ReplaceOne, and UpsertOne methods to utilize the new normalization function.
- Introduced new DependencyConfigSetup model instance in base model definitions for improved structure.
  • Loading branch information
tikazyq committed Dec 21, 2024
1 parent 29af5a3 commit e44b416
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
23 changes: 17 additions & 6 deletions core/grpc/server/model_base_service_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"context"
"encoding/json"
"github.com/crawlab-team/crawlab/core/utils"
"go.mongodb.org/mongo-driver/mongo/options"
"reflect"
"sync"
Expand Down Expand Up @@ -58,13 +59,14 @@ func (svr ModelBaseServiceServer) GetOne(_ context.Context, req *grpc.ModelServi
if err != nil {
return HandleError(err)
}
var options mongo.FindOptions
err = json.Unmarshal(req.FindOptions, &options)
query = utils.NormalizeBsonMObjectId(query)
var opts mongo.FindOptions
err = json.Unmarshal(req.FindOptions, &opts)
if err != nil {
return HandleError(err)
}
modelSvc := service.NewModelServiceWithColName[bson.M](typeNameColNameMap[req.ModelType])
data, err := modelSvc.GetOne(query, &options)
data, err := modelSvc.GetOne(query, &opts)
if err != nil {
return HandleError(err)
}
Expand All @@ -77,13 +79,14 @@ func (svr ModelBaseServiceServer) GetMany(_ context.Context, req *grpc.ModelServ
if err != nil {
return HandleError(err)
}
var options mongo.FindOptions
err = json.Unmarshal(req.FindOptions, &options)
query = utils.NormalizeBsonMObjectId(query)
var opts mongo.FindOptions
err = json.Unmarshal(req.FindOptions, &opts)
if err != nil {
return HandleError(err)
}
modelSvc := service.NewModelServiceWithColName[bson.M](typeNameColNameMap[req.ModelType])
data, err := modelSvc.GetMany(query, &options)
data, err := modelSvc.GetMany(query, &opts)
if err != nil {
return HandleError(err)
}
Expand All @@ -109,6 +112,7 @@ func (svr ModelBaseServiceServer) DeleteOne(_ context.Context, req *grpc.ModelSe
if err != nil {
return HandleError(err)
}
query = utils.NormalizeBsonMObjectId(query)
modelSvc := GetModelService[bson.M](req.ModelType)
err = modelSvc.DeleteOne(query)
if err != nil {
Expand All @@ -123,6 +127,7 @@ func (svr ModelBaseServiceServer) DeleteMany(_ context.Context, req *grpc.ModelS
if err != nil {
return HandleError(err)
}
query = utils.NormalizeBsonMObjectId(query)
modelSvc := GetModelService[bson.M](req.ModelType)
err = modelSvc.DeleteMany(query)
if err != nil {
Expand Down Expand Up @@ -155,11 +160,13 @@ func (svr ModelBaseServiceServer) UpdateOne(_ context.Context, req *grpc.ModelSe
if err != nil {
return HandleError(err)
}
query = utils.NormalizeBsonMObjectId(query)
var update bson.M
err = json.Unmarshal(req.Update, &update)
if err != nil {
return HandleError(err)
}
update = utils.NormalizeBsonMObjectId(update)
modelSvc := GetModelService[bson.M](req.ModelType)
err = modelSvc.UpdateOne(query, update)
if err != nil {
Expand All @@ -174,11 +181,13 @@ func (svr ModelBaseServiceServer) UpdateMany(_ context.Context, req *grpc.ModelS
if err != nil {
return HandleError(err)
}
query = utils.NormalizeBsonMObjectId(query)
var update bson.M
err = json.Unmarshal(req.Update, &update)
if err != nil {
return HandleError(err)
}
update = utils.NormalizeBsonMObjectId(update)
modelSvc := GetModelService[bson.M](req.ModelType)
err = modelSvc.UpdateMany(query, update)
if err != nil {
Expand Down Expand Up @@ -213,6 +222,7 @@ func (svr ModelBaseServiceServer) ReplaceOne(_ context.Context, req *grpc.ModelS
if err != nil {
return HandleError(err)
}
query = utils.NormalizeBsonMObjectId(query)
model := GetOneInstanceModel(req.ModelType)
modelType := reflect.TypeOf(model)
modelValuePtr := reflect.New(modelType).Interface()
Expand Down Expand Up @@ -283,6 +293,7 @@ func (svr ModelBaseServiceServer) UpsertOne(_ context.Context, req *grpc.ModelSe
if err != nil {
return HandleError(err)
}
query = utils.NormalizeBsonMObjectId(query)
modelSvc := GetModelService[bson.M](req.ModelType)
opts := &options.ReplaceOptions{}
opts.SetUpsert(true)
Expand Down
1 change: 1 addition & 0 deletions core/models/models/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func GetModelInstances() []any {
*new(Dependency),
*new(DependencyLog),
*new(DependencyConfig),
*new(DependencyConfigSetup),
*new(DependencyRepo),
*new(Environment),
*new(Git),
Expand Down

0 comments on commit e44b416

Please sign in to comment.