forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
118 lines (96 loc) · 3.71 KB
/
repo.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2014 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eventhorizon
import (
"context"
"errors"
"github.com/google/uuid"
)
// ReadRepo is a read repository for entities.
type ReadRepo interface {
// Parent returns the parent read repository, if there is one.
// Useful for iterating a wrapped set of repositories to get a specific one.
Parent() ReadRepo
// Find returns an entity for an ID.
Find(context.Context, uuid.UUID) (Entity, error)
// FindAll returns all entities in the repository.
FindAll(context.Context) ([]Entity, error)
}
// WriteRepo is a write repository for entities.
type WriteRepo interface {
// Save saves a entity in the storage.
Save(context.Context, Entity) error
// Remove removes a entity by ID from the storage.
Remove(context.Context, uuid.UUID) error
}
// ReadWriteRepo is a combined read and write repo, mainly useful for testing.
type ReadWriteRepo interface {
ReadRepo
WriteRepo
}
// Versionable is an item that has a version number,
// used by version.ReadRepo.FindMinVersion().
type Versionable interface {
// AggregateVersion returns the version of the item.
AggregateVersion() int
}
// Iter is a stateful iterator object that when called Next() readies the next
// value that can be retrieved from Value(). Enables incremental object retrieval
// from repos that support it. You must call Close() on each Iter even when
// results were delivered without apparent error.
type Iter interface {
Next(context.Context) bool
Value() interface{}
// Close must be called after the last Next() to retrieve error if any
Close(context.Context) error
}
// RepoError is an error in the read repository, with the namespace.
type RepoError struct {
// Err is the error.
Err error
// BaseErr is an optional underlying error, for example from the DB driver.
BaseErr error
// Namespace is the namespace for the error.
Namespace string
}
// Error implements the Error method of the errors.Error interface.
func (e RepoError) Error() string {
errStr := e.Err.Error()
if e.BaseErr != nil {
errStr += ": " + e.BaseErr.Error()
}
return errStr + " (" + e.Namespace + ")"
}
// Unwrap implements the errors.Unwrap method.
func (e RepoError) Unwrap() error {
return e.Err
}
// Cause implements the github.com/pkg/errors Unwrap method.
func (e RepoError) Cause() error {
return e.Unwrap()
}
// ErrEntityNotFound is when a entity could not be found.
var ErrEntityNotFound = errors.New("could not find entity")
// ErrCouldNotLoadEntity is when a entity could not be loaded.
var ErrCouldNotLoadEntity = errors.New("could not load entity")
// ErrCouldNotSaveEntity is when a entity could not be saved.
var ErrCouldNotSaveEntity = errors.New("could not save entity")
// ErrCouldNotRemoveEntity is when a entity could not be removed.
var ErrCouldNotRemoveEntity = errors.New("could not remove entity")
// ErrMissingEntityID is when a entity has no ID.
var ErrMissingEntityID = errors.New("missing entity ID")
// ErrEntityHasNoVersion is when an entity has no version number.
var ErrEntityHasNoVersion = errors.New("entity has no version")
// ErrIncorrectEntityVersion is when an entity has an incorrect version.
var ErrIncorrectEntityVersion = errors.New("incorrect entity version")