Skip to content
Snippets Groups Projects
Commit af3bcc6d authored by Anton Sattarov's avatar Anton Sattarov
Browse files

Добавлен запуск Gitlab Triage Bot для perxis-go

parent 2fcf77f2
No related branches found
No related tags found
No related merge requests found
package field
import (
"cmp"
"context"
"fmt"
"reflect"
"regexp"
"slices"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/errors"
"git.perx.ru/perxis/perxis-go/pkg/expr"
"github.com/hashicorp/go-multierror"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var objectType = &ObjectType{}
......@@ -21,10 +27,10 @@ type ObjectParameters struct {
func (ObjectParameters) Type() Type { return objectType }
func (p ObjectParameters) Clone(reset bool) Parameters {
func (p *ObjectParameters) Clone(reset bool) Parameters {
if reset {
p.Fields = nil
return &p
return p
}
flds := make(map[string]*Field)
......@@ -33,10 +39,10 @@ func (p ObjectParameters) Clone(reset bool) Parameters {
}
p.Fields = flds
return &p
return p
}
func (p ObjectParameters) GetField(f *Field, name string) *Field {
func (p *ObjectParameters) GetField(f *Field, name string) *Field {
// Поиск поля в текущем объекте
if fld, ok := p.Fields[name]; ok {
return f.SetFieldState(name, fld)
......@@ -57,7 +63,7 @@ func (p ObjectParameters) GetField(f *Field, name string) *Field {
return nil
}
func (p ObjectParameters) ListFields(f *Field, filterFunc ...FieldFilterFunc) []*Field {
func (p *ObjectParameters) ListFields(f *Field, filterFunc ...FieldFilterFunc) []*Field {
var fields []*Field
for k, fld := range p.Fields {
f.SetFieldState(k, fld)
......@@ -70,7 +76,7 @@ func (p ObjectParameters) ListFields(f *Field, filterFunc ...FieldFilterFunc) []
}
// IsInlineObject определяет являться ли поле name инлайн объектом
func (p ObjectParameters) IsInlineObject(name string) bool {
func (p *ObjectParameters) IsInlineObject(name string) bool {
fld, ok := p.Fields[name]
if !ok {
return false
......@@ -86,13 +92,13 @@ func (p ObjectParameters) IsInlineObject(name string) bool {
// GetFields возвращает поля объекта.
// Указание withInline позволяет так же включить поля указанные во вложенных inline объектам, и получиться поля для
// всех данных относящихся к текущему объекту.
func (p ObjectParameters) GetFields(withInline bool) map[string]*Field {
func (p *ObjectParameters) GetFields(withInline bool) map[string]*Field {
fields := make(map[string]*Field)
p.getFields(withInline, fields)
return fields
}
func (p ObjectParameters) getFields(withInline bool, fields map[string]*Field) {
func (p *ObjectParameters) getFields(withInline bool, fields map[string]*Field) {
for k, f := range p.Fields {
if obj, ok := f.Params.(*ObjectParameters); ok && obj.Inline {
obj.getFields(withInline, fields)
......@@ -125,6 +131,40 @@ func (p *ObjectParameters) Merge(parameters Parameters) error {
return nil
}
func (p *ObjectParameters) GetMongoIndexes(path string, f *Field) []mongo.IndexModel {
if !f.Indexed && !f.Unique {
return nil
}
var obj mongo.IndexModel
obj.Options = options.Index().SetName(path)
if f.Unique {
partial := bson.D{{Key: "template", Value: nil}, {Key: "deleted", Value: nil}}
// Если поле является не обязательным ИЛИ является переводом,
// то уникальный индекс применяется только для заполненных значений
if _, required := f.Options["required"]; !required || strings.HasPrefix(path, "translations.") {
partial = append(partial, bson.E{Key: path, Value: bson.D{{Key: "$exists", Value: true}}})
}
obj.Options.SetUnique(true)
obj.Options.SetPartialFilterExpression(partial)
}
flds := p.ListFields(f, func(fld *Field) bool { return true })
slices.SortFunc(flds, func(a, b *Field) int {
return cmp.Compare(a.State.DataPath, b.State.DataPath)
})
keys, _ := obj.Keys.(bson.D)
for _, fld := range flds {
keys = append(keys, bson.E{Key: fld.State.DataPath, Value: 1})
}
obj.Keys = keys
return []mongo.IndexModel{obj}
}
type ObjectType struct{}
func (ObjectType) Name() string {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment