Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
af3bcc6d
Commit
af3bcc6d
authored
4 months ago
by
Anton Sattarov
Browse files
Options
Downloads
Patches
Plain Diff
Добавлен запуск Gitlab Triage Bot для perxis-go
parent
2fcf77f2
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/schema/field/object.go
+50
-10
50 additions, 10 deletions
pkg/schema/field/object.go
with
50 additions
and
10 deletions
pkg/schema/field/object.go
+
50
−
10
View file @
af3bcc6d
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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment