1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00
any-sync/commonspace/object/tree/treechangeproto/treechange.go
2023-05-23 14:47:24 +02:00

53 lines
1.5 KiB
Go

package treechangeproto
import "github.com/anyproto/any-sync/net/rpc/rpcerr"
func WrapHeadUpdate(update *TreeHeadUpdate, rootChange *RawTreeChangeWithId) *TreeSyncMessage {
return &TreeSyncMessage{
Content: &TreeSyncContentValue{
Value: &TreeSyncContentValue_HeadUpdate{HeadUpdate: update},
},
RootChange: rootChange,
}
}
func WrapFullRequest(request *TreeFullSyncRequest, rootChange *RawTreeChangeWithId) *TreeSyncMessage {
return &TreeSyncMessage{
Content: &TreeSyncContentValue{
Value: &TreeSyncContentValue_FullSyncRequest{FullSyncRequest: request},
},
RootChange: rootChange,
}
}
func WrapFullResponse(response *TreeFullSyncResponse, rootChange *RawTreeChangeWithId) *TreeSyncMessage {
return &TreeSyncMessage{
Content: &TreeSyncContentValue{
Value: &TreeSyncContentValue_FullSyncResponse{FullSyncResponse: response},
},
RootChange: rootChange,
}
}
func WrapError(err error, rootChange *RawTreeChangeWithId) *TreeSyncMessage {
return &TreeSyncMessage{
Content: &TreeSyncContentValue{
Value: &TreeSyncContentValue_ErrorResponse{ErrorResponse: &TreeErrorResponse{ErrCode: rpcerr.Code(err)}},
},
RootChange: rootChange,
}
}
func GetHeads(msg *TreeSyncMessage) (heads []string) {
content := msg.GetContent()
switch {
case content.GetHeadUpdate() != nil:
return content.GetHeadUpdate().Heads
case content.GetFullSyncRequest() != nil:
return content.GetFullSyncRequest().Heads
case content.GetFullSyncResponse() != nil:
return content.GetFullSyncResponse().Heads
default:
return nil
}
}