2019-03-27 23:44:11 +03:00
|
|
|
package entity
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
2019-03-27 23:44:11 +03:00
|
|
|
|
|
|
|
// MergeStatus represent the result of a merge operation of an entity
|
|
|
|
type MergeStatus int
|
|
|
|
|
|
|
|
const (
|
2021-01-04 01:59:25 +03:00
|
|
|
_ MergeStatus = iota
|
|
|
|
MergeStatusNew // a new Entity was created locally
|
|
|
|
MergeStatusInvalid // the remote data is invalid
|
|
|
|
MergeStatusUpdated // a local Entity has been updated
|
|
|
|
MergeStatusNothing // no changes were made to a local Entity (already up to date)
|
|
|
|
MergeStatusError // a terminal error happened
|
2019-03-27 23:44:11 +03:00
|
|
|
)
|
|
|
|
|
2021-01-04 01:59:25 +03:00
|
|
|
// MergeResult hold the result of a merge operation on an Entity.
|
2019-03-27 23:44:11 +03:00
|
|
|
type MergeResult struct {
|
|
|
|
// Err is set when a terminal error occur in the process
|
|
|
|
Err error
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
Id Id
|
2019-03-27 23:44:11 +03:00
|
|
|
Status MergeStatus
|
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
// Only set for Invalid status
|
2019-03-27 23:44:11 +03:00
|
|
|
Reason string
|
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
// Only set for New or Updated status
|
2019-03-27 23:44:11 +03:00
|
|
|
Entity Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mr MergeResult) String() string {
|
|
|
|
switch mr.Status {
|
|
|
|
case MergeStatusNew:
|
|
|
|
return "new"
|
|
|
|
case MergeStatusInvalid:
|
|
|
|
return fmt.Sprintf("invalid data: %s", mr.Reason)
|
|
|
|
case MergeStatusUpdated:
|
|
|
|
return "updated"
|
|
|
|
case MergeStatusNothing:
|
|
|
|
return "nothing to do"
|
2019-08-13 20:51:14 +03:00
|
|
|
case MergeStatusError:
|
2021-02-14 13:36:32 +03:00
|
|
|
if mr.Id != "" {
|
|
|
|
return fmt.Sprintf("merge error on %s: %s", mr.Id, mr.Err.Error())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("merge error: %s", mr.Err.Error())
|
2019-03-27 23:44:11 +03:00
|
|
|
default:
|
|
|
|
panic("unknown merge status")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
func NewMergeNewStatus(id Id, entity Interface) MergeResult {
|
2019-03-27 23:44:11 +03:00
|
|
|
return MergeResult{
|
2019-08-13 20:51:14 +03:00
|
|
|
Id: id,
|
2021-01-24 21:45:21 +03:00
|
|
|
Status: MergeStatusNew,
|
|
|
|
Entity: entity,
|
2019-03-27 23:44:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
func NewMergeInvalidStatus(id Id, reason string) MergeResult {
|
2019-03-27 23:44:11 +03:00
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
2021-01-24 21:45:21 +03:00
|
|
|
Status: MergeStatusInvalid,
|
|
|
|
Reason: reason,
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 23:44:11 +03:00
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
func NewMergeUpdatedStatus(id Id, entity Interface) MergeResult {
|
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
|
|
|
Status: MergeStatusUpdated,
|
2019-03-27 23:44:11 +03:00
|
|
|
Entity: entity,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 21:45:21 +03:00
|
|
|
func NewMergeNothingStatus(id Id) MergeResult {
|
2019-03-27 23:44:11 +03:00
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
2021-01-24 21:45:21 +03:00
|
|
|
Status: MergeStatusNothing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMergeError(err error, id Id) MergeResult {
|
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
|
|
|
Status: MergeStatusError,
|
|
|
|
Err: err,
|
2019-03-27 23:44:11 +03:00
|
|
|
}
|
|
|
|
}
|