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 (
|
|
|
|
_ MergeStatus = iota
|
|
|
|
MergeStatusNew
|
|
|
|
MergeStatusInvalid
|
|
|
|
MergeStatusUpdated
|
|
|
|
MergeStatusNothing
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Only set for invalid status
|
|
|
|
Reason string
|
|
|
|
|
|
|
|
// Not set for invalid status
|
|
|
|
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"
|
|
|
|
default:
|
|
|
|
panic("unknown merge status")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
func NewMergeError(err error, id Id) MergeResult {
|
2019-03-27 23:44:11 +03:00
|
|
|
return MergeResult{
|
|
|
|
Err: err,
|
|
|
|
Id: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
func NewMergeStatus(status MergeStatus, id Id, entity Interface) MergeResult {
|
2019-03-27 23:44:11 +03:00
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
|
|
|
Status: status,
|
|
|
|
|
|
|
|
// Entity is not set for an invalid merge result
|
|
|
|
Entity: entity,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
func NewMergeInvalidStatus(id Id, reason string) MergeResult {
|
2019-03-27 23:44:11 +03:00
|
|
|
return MergeResult{
|
|
|
|
Id: id,
|
|
|
|
Status: MergeStatusInvalid,
|
|
|
|
Reason: reason,
|
|
|
|
}
|
|
|
|
}
|