From c67d5b165a61824ec32ec673d5edaeda2b1226c7 Mon Sep 17 00:00:00 2001 From: "a.petraki" <a.petraki@perx.ru> Date: Fri, 9 Aug 2024 14:20:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F,=20=D1=8F=D0=B2=D0=BB=D1=8F=D0=B5=D1=82=D1=81=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B8=20=D0=BF=D0=BE=D0=BB=D0=B5=20SingleLocale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/schema/schema.go | 34 +++++++++++++++++++++++++++++++ pkg/schema/schema_test.go | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 pkg/schema/schema_test.go diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 4fec4c17..28fad257 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -5,7 +5,9 @@ import ( "io/fs" "path/filepath" "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/expr" "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 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 +} diff --git a/pkg/schema/schema_test.go b/pkg/schema/schema_test.go new file mode 100644 index 00000000..462f3b8f --- /dev/null +++ b/pkg/schema/schema_test.go @@ -0,0 +1,42 @@ +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) + } + }) + } +} -- GitLab