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
c67d5b16
Commit
c67d5b16
authored
11 months ago
by
Alena Petraki
Browse files
Options
Downloads
Patches
Plain Diff
Добавлен метод для определения, является ли поле SingleLocale
parent
7e3853fc
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/schema/schema.go
+34
-0
34 additions, 0 deletions
pkg/schema/schema.go
pkg/schema/schema_test.go
+42
-0
42 additions, 0 deletions
pkg/schema/schema_test.go
with
76 additions
and
0 deletions
pkg/schema/schema.go
+
34
−
0
View file @
c67d5b16
...
@@ -5,7 +5,9 @@ import (
...
@@ -5,7 +5,9 @@ import (
"io/fs"
"io/fs"
"path/filepath"
"path/filepath"
"reflect"
"reflect"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/data"
"git.perx.ru/perxis/perxis-go/pkg/errors"
"git.perx.ru/perxis/perxis-go/pkg/errors"
"git.perx.ru/perxis/perxis-go/pkg/expr"
"git.perx.ru/perxis/perxis-go/pkg/expr"
"git.perx.ru/perxis/perxis-go/pkg/schema/field"
"git.perx.ru/perxis/perxis-go/pkg/schema/field"
...
@@ -290,3 +292,35 @@ func (s *Schema) Introspect(ctx context.Context, data map[string]interface{}) (m
...
@@ -290,3 +292,35 @@ func (s *Schema) Introspect(ctx context.Context, data map[string]interface{}) (m
return
val
,
mutatedSchema
,
nil
return
val
,
mutatedSchema
,
nil
}
}
func
(
s
*
Schema
)
IsFieldSingleLocale
(
path
string
)
bool
{
if
s
.
SingleLocale
{
return
true
}
subpaths
:=
getSubpaths
(
path
)
fields
:=
s
.
GetFields
(
func
(
_
*
field
.
Field
,
p
string
)
bool
{
return
data
.
Contains
(
p
,
subpaths
)
})
if
len
(
fields
)
!=
len
(
subpaths
)
{
return
false
// Поле не найдено
}
for
_
,
f
:=
range
fields
{
if
f
.
SingleLocale
{
return
true
}
if
f
.
Path
==
path
{
return
f
.
SingleLocale
}
}
return
false
}
func
getSubpaths
(
path
string
)
(
res
[]
string
)
{
var
cur
string
for
_
,
p
:=
range
strings
.
Split
(
path
,
field
.
FieldSeparator
)
{
if
cur
!=
""
{
cur
+=
field
.
FieldSeparator
}
cur
+=
p
res
=
append
(
res
,
cur
)
}
return
res
}
This diff is collapsed.
Click to expand it.
pkg/schema/schema_test.go
0 → 100644
+
42
−
0
View file @
c67d5b16
package
schema
import
(
"testing"
"git.perx.ru/perxis/perxis-go/pkg/schema/field"
)
func
TestSchema_IsFieldSingleLocale
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
path
string
want
bool
}{
{
"string field on first level"
,
"b"
,
true
},
{
"string field on first level"
,
"d"
,
false
},
{
"string nested field"
,
"a.a"
,
false
},
{
"string nested field"
,
"a.b"
,
true
},
{
"string field inside SingleLocale object"
,
"c.a"
,
true
},
{
"string field inside SingleLocale object"
,
"c.b"
,
true
},
{
"object"
,
"c"
,
true
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
s
:=
New
(
"a"
,
field
.
Object
(
"a"
,
field
.
String
(),
"b"
,
field
.
String
()
.
SetSingleLocale
(
true
),
),
"b"
,
field
.
String
()
.
SetSingleLocale
(
true
),
"c"
,
field
.
Object
(
"a"
,
field
.
String
(),
"b"
,
field
.
String
(),
)
.
SetSingleLocale
(
true
),
"d"
,
field
.
String
(),
)
if
got
:=
s
.
IsFieldSingleLocale
(
tt
.
path
);
got
!=
tt
.
want
{
t
.
Errorf
(
"IsFieldSingleLocale() = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
}
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