mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-10 18:10:49 +09:00
GO-3041 CopyStruct add copyVals arg
This commit is contained in:
parent
bc6f4c0ab2
commit
a150c7f7fc
17 changed files with 40 additions and 32 deletions
|
@ -66,7 +66,7 @@ func (bs *basic) collectDetailUpdates(details []*pb.RpcObjectSetDetailsDetail, s
|
|||
}
|
||||
|
||||
func applyDetailUpdates(oldDetails *types.Struct, updates []*detailUpdate) *types.Struct {
|
||||
newDetails := pbtypes.CopyStruct(oldDetails)
|
||||
newDetails := pbtypes.CopyStruct(oldDetails, false)
|
||||
if newDetails == nil || newDetails.Fields == nil {
|
||||
newDetails = &types.Struct{
|
||||
Fields: make(map[string]*types.Value),
|
||||
|
|
|
@ -50,7 +50,7 @@ func (h *history) Undo(ctx session.Context) (info HistoryInfo, err error) {
|
|||
}
|
||||
|
||||
if action.Details != nil {
|
||||
s.SetDetails(pbtypes.CopyStruct(action.Details.Before))
|
||||
s.SetDetails(pbtypes.CopyStruct(action.Details.Before, true))
|
||||
}
|
||||
if err = h.Apply(s, smartblock.NoHistory, smartblock.NoRestrictions); err != nil {
|
||||
return
|
||||
|
@ -82,7 +82,7 @@ func (h *history) Redo(ctx session.Context) (info HistoryInfo, err error) {
|
|||
s.SetObjectTypeKeys(ot)
|
||||
}
|
||||
if action.Details != nil {
|
||||
s.SetDetails(pbtypes.CopyStruct(action.Details.After))
|
||||
s.SetDetails(pbtypes.CopyStruct(action.Details.After, true))
|
||||
}
|
||||
if err = h.Apply(s, smartblock.NoHistory, smartblock.NoRestrictions); err != nil {
|
||||
return
|
||||
|
|
|
@ -57,7 +57,7 @@ func ExtractCustomState(st *state.State) (userState *state.State, err error) {
|
|||
}
|
||||
newState := state.NewDocWithUniqueKey(st.RootId(), blocksMap, uk).(*state.State)
|
||||
newState.AddRelationLinks(st.GetRelationLinks()...)
|
||||
newStateDetails := pbtypes.CopyStruct(st.Details())
|
||||
newStateDetails := pbtypes.CopyStruct(st.Details(), true)
|
||||
newName := pbtypes.GetString(newStateDetails, bundle.RelationKeyName.String()) + " [migrated]"
|
||||
newStateDetails.Fields[bundle.RelationKeyName.String()] = pbtypes.String(newName)
|
||||
newStateDetails.Fields[bundle.RelationKeyIsHidden.String()] = pbtypes.Bool(false)
|
||||
|
|
|
@ -874,7 +874,7 @@ func (sb *smartBlock) getDetailsFromStore() (*types.Struct, error) {
|
|||
if err != nil || storedDetails == nil {
|
||||
return nil, err
|
||||
}
|
||||
return pbtypes.CopyStruct(storedDetails.GetDetails()), nil
|
||||
return pbtypes.CopyStruct(storedDetails.GetDetails(), true), nil
|
||||
}
|
||||
|
||||
func (sb *smartBlock) appendPendingDetails(details *types.Struct) (resultDetails *types.Struct, hasPendingLocalDetails bool) {
|
||||
|
|
|
@ -267,7 +267,7 @@ func (s *State) changeBlockDetailsSet(set *pb.ChangeDetailsSet) error {
|
|||
// TODO: GO-2062 Need to refactor details shortening, as it could cut string incorrectly
|
||||
// set.Value = shortenValueToLimit(s.rootId, set.Key, set.Value)
|
||||
if s.details == nil || s.details.Fields == nil {
|
||||
s.details = pbtypes.CopyStruct(det)
|
||||
s.details = pbtypes.CopyStruct(det, false)
|
||||
}
|
||||
if set.Value != nil {
|
||||
s.details.Fields[set.Key] = set.Value
|
||||
|
@ -285,7 +285,7 @@ func (s *State) changeBlockDetailsUnset(unset *pb.ChangeDetailsUnset) error {
|
|||
}
|
||||
}
|
||||
if s.details == nil || s.details.Fields == nil {
|
||||
s.details = pbtypes.CopyStruct(det)
|
||||
s.details = pbtypes.CopyStruct(det, false)
|
||||
}
|
||||
delete(s.details.Fields, unset.Key)
|
||||
return nil
|
||||
|
|
|
@ -661,7 +661,7 @@ func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage,
|
|||
if s.parent != nil && s.details != nil {
|
||||
prev := s.parent.Details()
|
||||
if diff := pbtypes.StructDiff(prev, s.details); diff != nil {
|
||||
action.Details = &undo.Details{Before: pbtypes.CopyStruct(prev), After: pbtypes.CopyStruct(s.details)}
|
||||
action.Details = &undo.Details{Before: pbtypes.CopyStruct(prev, false), After: pbtypes.CopyStruct(s.details, false)}
|
||||
msgs = append(msgs, WrapEventMessages(false, StructDiffIntoEvents(s.RootId(), diff))...)
|
||||
s.parent.details = s.details
|
||||
} else if !s.details.Equal(s.parent.details) {
|
||||
|
@ -905,7 +905,7 @@ func (s *State) SetLocalDetail(key string, value *types.Value) {
|
|||
return
|
||||
}
|
||||
}
|
||||
s.localDetails = pbtypes.CopyStruct(s.parent.LocalDetails())
|
||||
s.localDetails = pbtypes.CopyStruct(s.parent.LocalDetails(), false)
|
||||
}
|
||||
if s.localDetails == nil || s.localDetails.Fields == nil {
|
||||
s.localDetails = &types.Struct{Fields: map[string]*types.Value{}}
|
||||
|
@ -956,7 +956,7 @@ func (s *State) SetDetail(key string, value *types.Value) {
|
|||
return
|
||||
}
|
||||
}
|
||||
s.details = pbtypes.CopyStruct(d)
|
||||
s.details = pbtypes.CopyStruct(d, false)
|
||||
}
|
||||
if s.details == nil || s.details.Fields == nil {
|
||||
s.details = &types.Struct{Fields: map[string]*types.Value{}}
|
||||
|
@ -1340,13 +1340,13 @@ func (s *State) Copy() *State {
|
|||
ctx: s.ctx,
|
||||
blocks: blocks,
|
||||
rootId: s.rootId,
|
||||
details: pbtypes.CopyStruct(s.Details()),
|
||||
localDetails: pbtypes.CopyStruct(s.LocalDetails()),
|
||||
details: pbtypes.CopyStruct(s.Details(), false),
|
||||
localDetails: pbtypes.CopyStruct(s.LocalDetails(), false),
|
||||
relationLinks: s.GetRelationLinks(), // Get methods copy inside
|
||||
objectTypeKeys: objTypes,
|
||||
noObjectType: s.noObjectType,
|
||||
migrationVersion: s.migrationVersion,
|
||||
store: pbtypes.CopyStruct(s.Store()),
|
||||
store: pbtypes.CopyStruct(s.Store(), false),
|
||||
storeLastChangeIdByPath: s.StoreLastChangeIdByPath(), // todo: do we need to copy it?
|
||||
storeKeyRemoved: storeKeyRemovedCopy,
|
||||
uniqueKeyInternal: s.uniqueKeyInternal,
|
||||
|
@ -1403,7 +1403,7 @@ func (s *State) IsTheHeaderChange() bool {
|
|||
}
|
||||
|
||||
func (s *State) RemoveDetail(keys ...string) (ok bool) {
|
||||
det := pbtypes.CopyStruct(s.Details())
|
||||
det := pbtypes.CopyStruct(s.Details(), false)
|
||||
if det != nil && det.Fields != nil {
|
||||
for _, key := range keys {
|
||||
if _, ex := det.Fields[key]; ex {
|
||||
|
@ -1419,7 +1419,7 @@ func (s *State) RemoveDetail(keys ...string) (ok bool) {
|
|||
}
|
||||
|
||||
func (s *State) RemoveLocalDetail(keys ...string) (ok bool) {
|
||||
det := pbtypes.CopyStruct(s.LocalDetails())
|
||||
det := pbtypes.CopyStruct(s.LocalDetails(), false)
|
||||
if det != nil && det.Fields != nil {
|
||||
for _, key := range keys {
|
||||
if _, ex := det.Fields[key]; ex {
|
||||
|
@ -1441,7 +1441,7 @@ func (s *State) createOrCopyStoreFromParent() {
|
|||
if s.store != nil {
|
||||
return
|
||||
}
|
||||
s.store = pbtypes.CopyStruct(s.Store())
|
||||
s.store = pbtypes.CopyStruct(s.Store(), true)
|
||||
// copy map[string]struct{} to map[string]struct{}
|
||||
m := s.StoreKeysRemoved()
|
||||
s.storeKeyRemoved = make(map[string]struct{}, len(m))
|
||||
|
|
|
@ -104,6 +104,6 @@ func (w *Workspaces) onApply(info smartblock.ApplyInfo) error {
|
|||
}
|
||||
|
||||
func (w *Workspaces) onWorkspaceChanged(state *state.State) {
|
||||
details := pbtypes.CopyStruct(state.CombinedDetails())
|
||||
details := pbtypes.CopyStruct(state.CombinedDetails(), true)
|
||||
w.spaceService.OnWorkspaceChanged(w.SpaceID(), details)
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ func (s *service) createObjectType(ctx context.Context, space clientspace.Space,
|
|||
if err != nil {
|
||||
return "", nil, fmt.Errorf("getUniqueKeyOrGenerate: %w", err)
|
||||
}
|
||||
object := pbtypes.CopyStruct(details)
|
||||
object := pbtypes.CopyStruct(details, false)
|
||||
|
||||
if _, ok := object.Fields[bundle.RelationKeyRecommendedLayout.String()]; !ok {
|
||||
object.Fields[bundle.RelationKeyRecommendedLayout.String()] = pbtypes.Int64(int64(model.ObjectType_basic))
|
||||
|
|
|
@ -34,7 +34,7 @@ func (s *service) createRelation(ctx context.Context, space clientspace.Space, d
|
|||
return "", nil, fmt.Errorf("missing relation name")
|
||||
}
|
||||
|
||||
object = pbtypes.CopyStruct(details)
|
||||
object = pbtypes.CopyStruct(details, false)
|
||||
key := pbtypes.GetString(details, bundle.RelationKeyRelationKey.String())
|
||||
if key == "" {
|
||||
key = bson.NewObjectId().Hex()
|
||||
|
|
|
@ -34,7 +34,7 @@ func (s *service) createRelationOption(ctx context.Context, space clientspace.Sp
|
|||
return "", nil, fmt.Errorf("getUniqueKeyOrGenerate: %w", err)
|
||||
}
|
||||
|
||||
object = pbtypes.CopyStruct(details)
|
||||
object = pbtypes.CopyStruct(details, false)
|
||||
object.Fields[bundle.RelationKeyUniqueKey.String()] = pbtypes.String(uniqueKey.Marshal())
|
||||
object.Fields[bundle.RelationKeyLayout.String()] = pbtypes.Int64(int64(model.ObjectType_relationOption))
|
||||
|
||||
|
|
|
@ -114,7 +114,7 @@ func (s *service) CreateTemplateStateWithDetails(
|
|||
}
|
||||
|
||||
func extractTargetDetails(originDetails *types.Struct, templateDetails *types.Struct) *types.Struct {
|
||||
targetDetails := pbtypes.CopyStruct(originDetails)
|
||||
targetDetails := pbtypes.CopyStruct(originDetails, true)
|
||||
if templateDetails == nil {
|
||||
return targetDetails
|
||||
}
|
||||
|
|
|
@ -774,7 +774,7 @@ func (mw *Middleware) ObjectSetInternalFlags(cctx context.Context, req *pb.RpcOb
|
|||
}
|
||||
err := mw.doBlockService(func(bs *block.Service) (err error) {
|
||||
return bs.ModifyDetails(req.ContextId, func(current *types.Struct) (*types.Struct, error) {
|
||||
d := pbtypes.CopyStruct(current)
|
||||
d := pbtypes.CopyStruct(current, true)
|
||||
return internalflag.PutToDetails(d, req.InternalFlags), nil
|
||||
})
|
||||
})
|
||||
|
|
|
@ -199,7 +199,7 @@ func (s *dsObjectStore) updateObjectLinks(txn *badger.Txn, id string, links []st
|
|||
}
|
||||
|
||||
func (s *dsObjectStore) sendUpdatesToSubscriptions(id string, details *types.Struct) {
|
||||
detCopy := pbtypes.CopyStruct(details)
|
||||
detCopy := pbtypes.CopyStruct(details, false)
|
||||
detCopy.Fields[database.RecordIDField] = pbtypes.ToValue(id)
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
|
|
@ -150,7 +150,7 @@ func Block(b *model.Block) (res *model.Block) {
|
|||
}
|
||||
|
||||
func Struct(in *types.Struct) (res *types.Struct) {
|
||||
res = pbtypes.CopyStruct(in)
|
||||
res = pbtypes.CopyStruct(in, true)
|
||||
if res != nil && res.Fields != nil {
|
||||
for k, v := range res.Fields {
|
||||
if k != "featuredRelations" {
|
||||
|
|
|
@ -122,16 +122,20 @@ func StructDiff(st1, st2 *types.Struct) *types.Struct {
|
|||
func StructMerge(st1, st2 *types.Struct, copyVals bool) *types.Struct {
|
||||
var res *types.Struct
|
||||
if st1 == nil || st1.Fields == nil {
|
||||
return CopyStruct(st2)
|
||||
return CopyStruct(st2, copyVals)
|
||||
}
|
||||
|
||||
if st2 == nil || st2.Fields == nil {
|
||||
return CopyStruct(st1)
|
||||
return CopyStruct(st1, copyVals)
|
||||
}
|
||||
|
||||
res = CopyStruct(st1)
|
||||
res = CopyStruct(st1, copyVals)
|
||||
for k, v := range st2.Fields {
|
||||
res.Fields[k] = CopyVal(v)
|
||||
if copyVals {
|
||||
res.Fields[k] = CopyVal(v)
|
||||
} else {
|
||||
res.Fields[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
|
|
|
@ -30,7 +30,7 @@ func CopyBlock(in *model.Block) (out *model.Block) {
|
|||
return
|
||||
}
|
||||
|
||||
func CopyStruct(s *types.Struct) *types.Struct {
|
||||
func CopyStruct(s *types.Struct, copyVals bool) *types.Struct {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -45,7 +45,11 @@ func CopyStruct(s *types.Struct) *types.Struct {
|
|||
}
|
||||
|
||||
for key, value := range s.Fields {
|
||||
copiedStruct.Fields[key] = CopyVal(value)
|
||||
if copyVals {
|
||||
copiedStruct.Fields[key] = CopyVal(value)
|
||||
} else {
|
||||
copiedStruct.Fields[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return copiedStruct
|
||||
|
@ -68,7 +72,7 @@ func CopyVal(v *types.Value) *types.Value {
|
|||
case *types.Value_BoolValue:
|
||||
copiedValue.Kind = &types.Value_BoolValue{BoolValue: kind.BoolValue}
|
||||
case *types.Value_StructValue:
|
||||
copiedValue.Kind = &types.Value_StructValue{StructValue: CopyStruct(kind.StructValue)}
|
||||
copiedValue.Kind = &types.Value_StructValue{StructValue: CopyStruct(kind.StructValue, true)}
|
||||
case *types.Value_ListValue:
|
||||
copiedValue.Kind = &types.Value_ListValue{ListValue: CopyListVal(kind.ListValue)}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ func TestCopyStruct(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
copy := CopyStruct(original)
|
||||
copy := CopyStruct(original, true)
|
||||
|
||||
assert.NotSame(t, original, copy)
|
||||
assert.Equal(t, original, copy)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue