forked from Tochemey/ego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
131 lines (118 loc) · 4.26 KB
/
entity.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
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2022-2023 Tochemey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package ego
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/tochemey/ego/egopb"
"github.com/tochemey/goakt/actors"
)
var (
// ErrEngineRequired is returned when the eGo engine is not set
ErrEngineRequired = errors.New("eGo engine is not defined")
// ErrEngineNotStarted is returned when the eGo engine has not started
ErrEngineNotStarted = errors.New("eGo engine has not started")
// ErrUndefinedEntity is returned when sending a command to an undefined entity
ErrUndefinedEntity = errors.New("eGo entity is not defined")
)
// Entity defines the event sourced persistent entity
// This handles commands in order
type Entity[T State] struct {
actor actors.PID
}
// NewEntity creates an instance of Entity
func NewEntity[T State](ctx context.Context, behavior EntityBehavior[T], engine *Engine) (*Entity[T], error) {
// check whether the ego engine is defined
if engine == nil {
return nil, ErrEngineRequired
}
// check whether the eGo engine has started or not
if !engine.started.Load() {
return nil, ErrEngineNotStarted
}
// create the instance of the actor
pid, err := engine.actorSystem.Spawn(ctx, behavior.ID(), newActor(behavior, engine.eventsStore, engine.eventStream))
// return the error in case there is one
if err != nil {
return nil, err
}
return &Entity[T]{
actor: pid,
}, nil
}
// SendCommand sends command to a given entity ref. This will return:
// 1. the resulting state after the command has been handled and the emitted event persisted
// 2. nil when there is no resulting state or no event persisted
// 3. an error in case of error
func (x Entity[T]) SendCommand(ctx context.Context, command Command) (resultingState T, revision uint64, err error) {
// define a nil state
var nilT T
// check whether the underlying actor is set and running
if x.actor == nil || !x.actor.IsRunning() {
return nilT, 0, ErrUndefinedEntity
}
// send the command to the actor
reply, err := actors.Ask(ctx, x.actor, command, time.Second)
// handle the error
if err != nil {
return nilT, 0, err
}
// cast the reply to a command reply because that is the expected return type
commandReply, ok := reply.(*egopb.CommandReply)
// when casting is successful
if ok {
// parse the command reply and return the appropriate responses
return parseCommandReply[T](commandReply)
}
// casting failed
return nilT, 0, errors.New("failed to parse command reply")
}
// parseCommandReply parses the command reply
func parseCommandReply[T State](reply *egopb.CommandReply) (T, uint64, error) {
var (
state T
err error
)
// parse the command reply
switch r := reply.GetReply().(type) {
case *egopb.CommandReply_StateReply:
// unmarshal the state
msg, err := r.StateReply.GetState().UnmarshalNew()
// return the error in case there is one
if err != nil {
return state, 0, err
}
// unpack the state properly
switch v := msg.(type) {
case T:
return v, r.StateReply.GetSequenceNumber(), nil
default:
return state, 0, fmt.Errorf("got %s", r.StateReply.GetState().GetTypeUrl())
}
case *egopb.CommandReply_ErrorReply:
err = errors.New(r.ErrorReply.GetMessage())
return state, 0, err
}
return state, 0, errors.New("no state received")
}