aboutsummaryrefslogtreecommitdiffstats
path: root/entity/merge.go
blob: 7c3e71c846608af18834c2c9be1978731f073f3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package entity

import (
	"fmt"
)

// 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

	Id     Id
	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")
	}
}

func NewMergeError(err error, id Id) MergeResult {
	return MergeResult{
		Err: err,
		Id:  id,
	}
}

func NewMergeStatus(status MergeStatus, id Id, entity Interface) MergeResult {
	return MergeResult{
		Id:     id,
		Status: status,

		// Entity is not set for an invalid merge result
		Entity: entity,
	}
}

func NewMergeInvalidStatus(id Id, reason string) MergeResult {
	return MergeResult{
		Id:     id,
		Status: MergeStatusInvalid,
		Reason: reason,
	}
}