Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): Item fields pruning #1035

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions server/internal/usecase/interactor/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
Item: vi.Value(),
Model: m,
Schema: s,
GroupSchemas: groupSchemas,
GroupSchemas: lo.Values(groupSchemas),
ReferencedItems: i.getReferencedItems(ctx, fields),
},
Operator: operator.Operator(),
Expand Down Expand Up @@ -393,6 +393,9 @@
}
}

sp := schema.NewPackage(s, nil, groupSchemas)
itv.Prune(sp)

if err := i.repos.Item.Save(ctx, itv); err != nil {
return nil, err
}
Expand All @@ -410,7 +413,7 @@
Item: itv,
Model: m,
Schema: s,
GroupSchemas: groupSchemas,
GroupSchemas: lo.Values(groupSchemas),
ReferencedItems: i.getReferencedItems(ctx, fields),
Changes: item.CompareFields(itv.Fields(), oldFields),
},
Expand Down Expand Up @@ -718,11 +721,14 @@
return nil
}

func (i Item) handleGroupFields(ctx context.Context, params []interfaces.ItemFieldParam, s *schema.Schema, mId id.ModelID, itemFields item.Fields) (item.Fields, schema.List, error) {
func (i Item) handleGroupFields(ctx context.Context, params []interfaces.ItemFieldParam, s *schema.Schema, mId id.ModelID, itemFields item.Fields) (item.Fields, map[id.GroupID]*schema.Schema, error) {
var res item.Fields
var groupSchemas schema.List
groupSchemas := map[id.GroupID]*schema.Schema{}
for _, field := range itemFields.FieldsByType(value.TypeGroup) {
sf := s.Field(field.FieldID())
if sf == nil {
continue

Check warning on line 730 in server/internal/usecase/interactor/item.go

View check run for this annotation

Codecov / codecov/patch

server/internal/usecase/interactor/item.go#L729-L730

Added lines #L729 - L730 were not covered by tests
}
var fieldGroup *schema.FieldGroup
sf.TypeProperty().Match(schema.TypePropertyMatch{
Group: func(f *schema.FieldGroup) {
Expand All @@ -741,7 +747,7 @@
}

if groupSchema != nil {
groupSchemas = append(groupSchemas, groupSchema)
groupSchemas[group.ID()] = groupSchema

Check warning on line 750 in server/internal/usecase/interactor/item.go

View check run for this annotation

Codecov / codecov/patch

server/internal/usecase/interactor/item.go#L750

Added line #L750 was not covered by tests
}

mvg, ok := field.Value().ValuesGroup()
Expand Down
19 changes: 19 additions & 0 deletions server/pkg/item/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,22 @@
func (i *Item) SetOriginalItem(iid id.ItemID) {
i.originalItem = &iid
}

func (i *Item) Prune(sp *schema.Package) {
var filtered, res Fields = lo.Filter(slices.Clone(i.Fields()), func(f *Field, index int) bool {
return sp.Field(f.FieldID()) != nil
}), Fields{}
for _, fd := range filtered {
if fd.Type() == value.TypeGroup {
gvl, _ := fd.Value().ValuesGroup()
for _, gv := range gvl {
res = append(res, filtered.FieldsByGroup(gv)...)
}

Check warning on line 268 in server/pkg/item/item.go

View check run for this annotation

Codecov / codecov/patch

server/pkg/item/item.go#L259-L268

Added lines #L259 - L268 were not covered by tests
}
if fd.ItemGroup() == nil {
res = append(res, fd)
}

Check warning on line 272 in server/pkg/item/item.go

View check run for this annotation

Codecov / codecov/patch

server/pkg/item/item.go#L270-L272

Added lines #L270 - L272 were not covered by tests
}

i.fields = res

Check warning on line 275 in server/pkg/item/item.go

View check run for this annotation

Codecov / codecov/patch

server/pkg/item/item.go#L275

Added line #L275 was not covered by tests
}
8 changes: 5 additions & 3 deletions server/pkg/schema/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ func (p *Package) Field(fieldID id.FieldID) *Field {
if f != nil {
return f
}
f = p.metaSchema.Field(fieldID)
if f != nil {
return f
if p.metaSchema != nil {
f = p.metaSchema.Field(fieldID)
if f != nil {
return f
}
}
for _, s := range p.groupSchemas {
f = s.Field(fieldID)
Expand Down
Loading