From 24d6714dd59a5c385c21991caa47e5727de043d0 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Wed, 27 Mar 2019 21:44:11 +0100 Subject: cache: properly push/pull identities and bugs --- entity/doc.go | 8 +++++++ entity/interface.go | 8 +++++++ entity/merge.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 entity/doc.go create mode 100644 entity/interface.go create mode 100644 entity/merge.go (limited to 'entity') diff --git a/entity/doc.go b/entity/doc.go new file mode 100644 index 00000000..4682d545 --- /dev/null +++ b/entity/doc.go @@ -0,0 +1,8 @@ +// Package entity contains the base common code to define an entity stored +// in a chain of git objects, supporting actions like Push, Pull and Merge. +package entity + +// TODO: Bug and Identity are very similar, right ? I expect that this package +// will eventually hold the common code to define an entity and the related +// helpers, errors and so on. When this work is done, it will become easier +// to add new entities, for example to support pull requests. diff --git a/entity/interface.go b/entity/interface.go new file mode 100644 index 00000000..62b92a58 --- /dev/null +++ b/entity/interface.go @@ -0,0 +1,8 @@ +package entity + +type Interface interface { + // Id return the Entity identifier + Id() string + // HumanId return the Entity identifier truncated for human consumption + HumanId() string +} diff --git a/entity/merge.go b/entity/merge.go new file mode 100644 index 00000000..544f2168 --- /dev/null +++ b/entity/merge.go @@ -0,0 +1,68 @@ +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 string + 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 string) MergeResult { + return MergeResult{ + Err: err, + Id: id, + } +} + +func NewMergeStatus(status MergeStatus, id string, entity Interface) MergeResult { + return MergeResult{ + Id: id, + Status: status, + + // Entity is not set for an invalid merge result + Entity: entity, + } +} + +func NewMergeInvalidStatus(id string, reason string) MergeResult { + return MergeResult{ + Id: id, + Status: MergeStatusInvalid, + Reason: reason, + } +} -- cgit