Fix broken filtering through discriminator
This commit is contained in:
@@ -11,6 +11,10 @@ import (
|
||||
func (c *MongoClient) DiscriminatorCheckAndApplyToData(ctx context.Context, name string, data map[string]any) error {
|
||||
cdef, ok := c.Registry[name]
|
||||
if ok && cdef.Discriminator != nil {
|
||||
if data == nil {
|
||||
data = map[string]any{}
|
||||
}
|
||||
|
||||
log.Printf("Discriminator found for %s; will use it", name)
|
||||
|
||||
// get from context
|
||||
@@ -30,6 +34,10 @@ func (c *MongoClient) DiscriminatorCheckAndApplyToData(ctx context.Context, name
|
||||
func (c *MongoClient) DiscriminatorCheckAndApplyToFilter(ctx context.Context, name string, filter bson.M) error {
|
||||
cdef, ok := c.Registry[name]
|
||||
if ok && cdef.Discriminator != nil {
|
||||
if filter == nil {
|
||||
filter = bson.M{}
|
||||
}
|
||||
|
||||
log.Printf("Discriminator found for %s; will use it", name)
|
||||
|
||||
// get from context
|
||||
|
||||
10
filter.go
10
filter.go
@@ -3,13 +3,13 @@ package mongo
|
||||
import "log"
|
||||
|
||||
type Query struct {
|
||||
Filter map[string]any
|
||||
Filter map[string]any `json:"filter"`
|
||||
}
|
||||
|
||||
type Filter struct {
|
||||
Name string
|
||||
Op string
|
||||
Value string
|
||||
Name string `json:"name"`
|
||||
Op string `json:"op"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func makeFilter(name string, value any) *Filter {
|
||||
@@ -64,7 +64,7 @@ func Mongofy(q *Query) map[string]any {
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
return nil
|
||||
return map[string]any{}
|
||||
}
|
||||
|
||||
if logic == "or" {
|
||||
|
||||
11
find.go
11
find.go
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"git.gsuntres.com/general/commons"
|
||||
)
|
||||
|
||||
// Find is used to fetch the first page of data.
|
||||
@@ -89,10 +90,14 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
|
||||
|
||||
finalLimit := max(limit, c.Limit)
|
||||
|
||||
if err := c.DiscriminatorCheckAndApplyToFilter(ctx, name, filter); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f := Mongofy(&Query{
|
||||
Filter: filter,
|
||||
})
|
||||
|
||||
|
||||
pipeline := BuildPaginationPipeline(offset, finalLimit, f, nil)
|
||||
|
||||
// 2. Query
|
||||
@@ -116,8 +121,8 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
|
||||
|
||||
var totalValue any
|
||||
if len(metadata) != 0 {
|
||||
metadataRoot := metadata[0].(bson.M)
|
||||
totalValue = metadataRoot["total"]
|
||||
metadataRoot := metadata[0].(bson.D)
|
||||
totalValue, _ = commons.BsonDGetAny(metadataRoot, "total")
|
||||
}
|
||||
|
||||
var total int64
|
||||
|
||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module git.gsuntres.com/general/mongo
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
git.gsuntres.com/general/commons v0.0.0-20260416141603-7a6c5b6c3c8c
|
||||
git.gsuntres.com/general/commons v0.0.0-20260418004121-f52793a7641f
|
||||
git.gsuntres.com/general/sys v0.0.0-20260329160429-49966ca31027
|
||||
github.com/go-viper/mapstructure/v2 v2.5.0
|
||||
github.com/google/go-cmp v0.7.0
|
||||
|
||||
2
go.sum
2
go.sum
@@ -2,6 +2,8 @@ dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
||||
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
||||
git.gsuntres.com/general/commons v0.0.0-20260416141603-7a6c5b6c3c8c h1:3jh+CfW0j4JQOa1tRN7UFWcbv5LxBRdHAeM8SoPDbJM=
|
||||
git.gsuntres.com/general/commons v0.0.0-20260416141603-7a6c5b6c3c8c/go.mod h1:ZawSPCI/Irjx7P83qJRcknKGuLLJ9c7hhP4OXgILnCY=
|
||||
git.gsuntres.com/general/commons v0.0.0-20260418004121-f52793a7641f h1:lSjhzGQBpON/Cc+wXZXcMrpgm0RQ+waipLoC/abmX7I=
|
||||
git.gsuntres.com/general/commons v0.0.0-20260418004121-f52793a7641f/go.mod h1:ZawSPCI/Irjx7P83qJRcknKGuLLJ9c7hhP4OXgILnCY=
|
||||
git.gsuntres.com/general/sys v0.0.0-20260329160429-49966ca31027 h1:4pmcjxEDM4rzv+iimQ7wTgCAQ1VnAoeGiHLuf6wC6Fw=
|
||||
git.gsuntres.com/general/sys v0.0.0-20260329160429-49966ca31027/go.mod h1:OVs7w4/tJO1GT7cLIeEsb90LuZqH2xYIVQODI5P1GJs=
|
||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
|
||||
|
||||
6
main.go
6
main.go
@@ -232,6 +232,7 @@ type MongoStartProps struct {
|
||||
MongoPass string
|
||||
MongoDebugQuery bool
|
||||
MongoDBPrefix string
|
||||
AllowTruncatingDoubles bool
|
||||
}
|
||||
|
||||
func Start(props *MongoStartProps) error {
|
||||
@@ -262,7 +263,10 @@ func Start(props *MongoStartProps) error {
|
||||
NilSliceAsEmpty: true,
|
||||
NilMapAsEmpty: true,
|
||||
}).
|
||||
SetRegistry(GetCustomRegistry())
|
||||
SetRegistry(GetCustomRegistry()).
|
||||
SetBSONOptions(&options.BSONOptions{
|
||||
AllowTruncatingDoubles: props.AllowTruncatingDoubles,
|
||||
})
|
||||
|
||||
client.DebugQuery = props.MongoDebugQuery
|
||||
if client.DebugQuery {
|
||||
|
||||
@@ -23,7 +23,7 @@ func (c *MongoClient) Replace(ctx context.Context, database, name string, id str
|
||||
if err := c.DiscriminatorCheckAndApplyToData(ctx, name, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
updateResult, err := collection.ReplaceOne(ctx, filter, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user