1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 17:44:59 +09:00

Merge pull request #1745 from anytypeio/fix-objectToSet-go-707

objectCreator: fix createSet details.
This commit is contained in:
Roman Khafizianov 2023-01-28 14:35:18 +01:00 committed by GitHub
commit 1c0e037c87
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 25 deletions

View file

@ -170,10 +170,13 @@ func (s *Service) ObjectToSet(id string, source []string) (string, error) {
}); err != nil {
return "", err
}
// pass layout in case it was provided by client in the RPC
details.Fields[bundle.RelationKeySetOf.String()] = pbtypes.StringList(source)
newID, _, err := s.objectCreator.CreateObject(&pb.RpcObjectCreateSetRequest{
Source: source,
// cleanup details
delete(details.Fields, bundle.RelationKeyLayout.String())
delete(details.Fields, bundle.RelationKeyType.String())
newID, _, err := s.objectCreator.CreateObject(&pb.RpcObjectCreateRequest{
Details: details,
}, bundle.TypeKeySet)
if err != nil {

View file

@ -88,6 +88,8 @@ func (c *Creator) CreateSmartBlockFromTemplate(ctx context.Context, sbType cores
return c.CreateSmartBlockFromState(ctx, sbType, details, createState)
}
// CreateSmartBlockFromState create new object from the provided `createState` and `details`. If you pass `details` into the function, it will automatically add missing relationLinks and override the details from the `createState`
// It will return error if some of the relation keys in `details` not installed in the workspace.
func (c *Creator) CreateSmartBlockFromState(ctx context.Context, sbType coresb.SmartBlockType, details *types.Struct, createState *state.State) (id string, newDetails *types.Struct, err error) {
if createState == nil {
createState = state.NewDoc("", nil).(*state.State)
@ -241,29 +243,16 @@ func (c *Creator) CreateSet(req *pb.RpcObjectCreateSetRequest) (setID string, ne
newState := state.NewDoc("", nil).NewState()
name := pbtypes.GetString(req.Details, bundle.RelationKeyName.String())
icon := pbtypes.GetString(req.Details, bundle.RelationKeyIconEmoji.String())
if req.Details != nil && req.Details.Fields != nil {
for k, v := range req.Details.Fields {
relID := addr.RelationKeyToIdPrefix + k
if _, err2 := c.objectStore.GetRelationById(relID); err != nil {
err = fmt.Errorf("failed to get installed relation %s: %w", relID, err2)
return
}
newState.SetDetail(k, v)
}
}
tmpls := []template.StateTransformer{
template.WithForcedDetail(bundle.RelationKeyName, pbtypes.String(name)),
template.WithForcedDetail(bundle.RelationKeyIconEmoji, pbtypes.String(icon)),
template.WithRequiredRelations(),
}
var blockContent *model.BlockContentOfDataview
if dvSchema != nil {
blockContent = &dvContent
}
if len(req.Source) > 0 {
req.Details.Fields[bundle.RelationKeySource.String()] = pbtypes.StringList(req.Source)
}
if blockContent != nil {
for i, view := range blockContent.Dataview.Views {
if view.Relations == nil {
@ -273,11 +262,6 @@ func (c *Creator) CreateSet(req *pb.RpcObjectCreateSetRequest) (setID string, ne
tmpls = append(tmpls,
template.WithDataview(*blockContent, false),
)
if len(req.Source) > 0 {
tmpls = append(tmpls,
template.WithForcedDetail(bundle.RelationKeySetOf, pbtypes.StringList(req.Source)),
)
}
}
if err = template.InitTemplate(newState, tmpls...); err != nil {
@ -285,7 +269,7 @@ func (c *Creator) CreateSet(req *pb.RpcObjectCreateSetRequest) (setID string, ne
}
// TODO: here can be a deadlock if this is somehow created from workspace (as set)
return c.CreateSmartBlockFromState(context.TODO(), coresb.SmartBlockTypeSet, nil, newState)
return c.CreateSmartBlockFromState(context.TODO(), coresb.SmartBlockTypeSet, req.Details, newState)
}
// TODO: it must be in another component
@ -355,6 +339,7 @@ func (c *Creator) CreateObject(req block.DetailsGetter, forcedType bundle.TypeKe
Details: details,
})
case bundle.TypeKeySet.URL():
details.Fields[bundle.RelationKeyLayout.String()] = pbtypes.Float64(float64(model.ObjectType_set))
return c.CreateSet(&pb.RpcObjectCreateSetRequest{
Details: details,
InternalFlags: internalFlags,