This repository has been archived by the owner on Mar 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.go
115 lines (101 loc) · 2.97 KB
/
model.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
package valueentity
import (
"errors"
"github.com/cloudstateio/go-support/cloudstate/encoding"
"github.com/cloudstateio/go-support/cloudstate/protocol"
"github.com/cloudstateio/go-support/cloudstate/value"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
)
type ValueEntityTckModelEntity struct {
state string
}
func NewValueEntityTckModelEntity(id value.EntityID) value.EntityHandler {
return &ValueEntityTckModelEntity{}
}
func (e *ValueEntityTckModelEntity) HandleCommand(ctx *value.Context, name string, msg proto.Message) (*any.Any, error) {
var forward = false
var failure error = nil
switch m := msg.(type) {
case *Request:
for _, action := range m.GetActions() {
switch a := action.Action.(type) {
case *RequestAction_Update:
e.state = a.Update.Value
failure = ctx.Update(encoding.MarshalAny(&Persisted{
Value: e.state,
}))
case *RequestAction_Delete:
ctx.Delete()
e.state = ""
case *RequestAction_Forward:
forward = true
req, err := encoding.MarshalAny(&Request{
Id: a.Forward.Id,
})
if err != nil {
return nil, err
}
ctx.Forward(&protocol.Forward{
ServiceName: "cloudstate.tck.model.valueentity.ValueEntityTwo",
CommandName: "Call",
Payload: req,
})
case *RequestAction_Effect:
req, err := encoding.MarshalAny(&Request{
Id: a.Effect.Id,
})
if err != nil {
return nil, err
}
ctx.SideEffect(&protocol.SideEffect{
ServiceName: "cloudstate.tck.model.valueentity.ValueEntityTwo",
CommandName: "Call",
Payload: req,
Synchronous: a.Effect.GetSynchronous(),
})
case *RequestAction_Fail:
failure = protocol.ClientError{Err: errors.New(a.Fail.GetMessage())}
}
}
}
if forward {
return nil, nil
}
if failure != nil {
return nil, failure
}
return encoding.MarshalAny(&Response{
Message: e.state,
})
}
func (e *ValueEntityTckModelEntity) HandleState(ctx *value.Context, state *any.Any) error {
var p Persisted
if err := encoding.UnmarshalAny(state, &p); err != nil {
return err
}
e.state = p.GetValue()
return nil
}
type ValueEntityTckModelEntityTwo struct {
}
func NewValueEntityTckModelEntityTwo(id value.EntityID) value.EntityHandler {
return &ValueEntityTckModelEntityTwo{}
}
func (e *ValueEntityTckModelEntityTwo) HandleCommand(ctx *value.Context, name string, msg proto.Message) (*any.Any, error) {
return encoding.MarshalAny(&Response{})
}
func (e *ValueEntityTckModelEntityTwo) HandleState(ctx *value.Context, state *any.Any) error {
return nil
}
type ValueEntityConfiguredEntity string
func NewValueEntityConfiguredEntity(id value.EntityID) value.EntityHandler {
r := ValueEntityConfiguredEntity("state")
return &r
}
func (v *ValueEntityConfiguredEntity) HandleCommand(ctx *value.Context, name string, msg proto.Message) (*any.Any, error) {
return encoding.MarshalAny(&Response{})
}
func (v *ValueEntityConfiguredEntity) HandleState(ctx *value.Context, state *any.Any) error {
return nil
}