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 {
|
func (c *MongoClient) DiscriminatorCheckAndApplyToData(ctx context.Context, name string, data map[string]any) error {
|
||||||
cdef, ok := c.Registry[name]
|
cdef, ok := c.Registry[name]
|
||||||
if ok && cdef.Discriminator != nil {
|
if ok && cdef.Discriminator != nil {
|
||||||
|
if data == nil {
|
||||||
|
data = map[string]any{}
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Discriminator found for %s; will use it", name)
|
log.Printf("Discriminator found for %s; will use it", name)
|
||||||
|
|
||||||
// get from context
|
// 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 {
|
func (c *MongoClient) DiscriminatorCheckAndApplyToFilter(ctx context.Context, name string, filter bson.M) error {
|
||||||
cdef, ok := c.Registry[name]
|
cdef, ok := c.Registry[name]
|
||||||
if ok && cdef.Discriminator != nil {
|
if ok && cdef.Discriminator != nil {
|
||||||
|
if filter == nil {
|
||||||
|
filter = bson.M{}
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Discriminator found for %s; will use it", name)
|
log.Printf("Discriminator found for %s; will use it", name)
|
||||||
|
|
||||||
// get from context
|
// get from context
|
||||||
|
|||||||
10
filter.go
10
filter.go
@@ -3,13 +3,13 @@ package mongo
|
|||||||
import "log"
|
import "log"
|
||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Filter map[string]any
|
Filter map[string]any `json:"filter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
Name string
|
Name string `json:"name"`
|
||||||
Op string
|
Op string `json:"op"`
|
||||||
Value string
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeFilter(name string, value any) *Filter {
|
func makeFilter(name string, value any) *Filter {
|
||||||
@@ -64,7 +64,7 @@ func Mongofy(q *Query) map[string]any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(conditions) == 0 {
|
if len(conditions) == 0 {
|
||||||
return nil
|
return map[string]any{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if logic == "or" {
|
if logic == "or" {
|
||||||
|
|||||||
9
find.go
9
find.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
|
"git.gsuntres.com/general/commons"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Find is used to fetch the first page of data.
|
// Find is used to fetch the first page of data.
|
||||||
@@ -89,6 +90,10 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
|
|||||||
|
|
||||||
finalLimit := max(limit, c.Limit)
|
finalLimit := max(limit, c.Limit)
|
||||||
|
|
||||||
|
if err := c.DiscriminatorCheckAndApplyToFilter(ctx, name, filter); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
f := Mongofy(&Query{
|
f := Mongofy(&Query{
|
||||||
Filter: filter,
|
Filter: filter,
|
||||||
})
|
})
|
||||||
@@ -116,8 +121,8 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
|
|||||||
|
|
||||||
var totalValue any
|
var totalValue any
|
||||||
if len(metadata) != 0 {
|
if len(metadata) != 0 {
|
||||||
metadataRoot := metadata[0].(bson.M)
|
metadataRoot := metadata[0].(bson.D)
|
||||||
totalValue = metadataRoot["total"]
|
totalValue, _ = commons.BsonDGetAny(metadataRoot, "total")
|
||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module git.gsuntres.com/general/mongo
|
|||||||
go 1.25.0
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
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
|
git.gsuntres.com/general/sys v0.0.0-20260329160429-49966ca31027
|
||||||
github.com/go-viper/mapstructure/v2 v2.5.0
|
github.com/go-viper/mapstructure/v2 v2.5.0
|
||||||
github.com/google/go-cmp v0.7.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=
|
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 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-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 h1:4pmcjxEDM4rzv+iimQ7wTgCAQ1VnAoeGiHLuf6wC6Fw=
|
||||||
git.gsuntres.com/general/sys v0.0.0-20260329160429-49966ca31027/go.mod h1:OVs7w4/tJO1GT7cLIeEsb90LuZqH2xYIVQODI5P1GJs=
|
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=
|
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
|
MongoPass string
|
||||||
MongoDebugQuery bool
|
MongoDebugQuery bool
|
||||||
MongoDBPrefix string
|
MongoDBPrefix string
|
||||||
|
AllowTruncatingDoubles bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(props *MongoStartProps) error {
|
func Start(props *MongoStartProps) error {
|
||||||
@@ -262,7 +263,10 @@ func Start(props *MongoStartProps) error {
|
|||||||
NilSliceAsEmpty: true,
|
NilSliceAsEmpty: true,
|
||||||
NilMapAsEmpty: true,
|
NilMapAsEmpty: true,
|
||||||
}).
|
}).
|
||||||
SetRegistry(GetCustomRegistry())
|
SetRegistry(GetCustomRegistry()).
|
||||||
|
SetBSONOptions(&options.BSONOptions{
|
||||||
|
AllowTruncatingDoubles: props.AllowTruncatingDoubles,
|
||||||
|
})
|
||||||
|
|
||||||
client.DebugQuery = props.MongoDebugQuery
|
client.DebugQuery = props.MongoDebugQuery
|
||||||
if client.DebugQuery {
|
if client.DebugQuery {
|
||||||
|
|||||||
Reference in New Issue
Block a user