This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
generic_cmds.go
166 lines (137 loc) · 4.87 KB
/
generic_cmds.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package command
import (
"github.com/openziti/fabric/controller/change"
"github.com/openziti/fabric/controller/fields"
"github.com/openziti/fabric/controller/models"
"github.com/openziti/fabric/common/pb/cmd_pb"
"github.com/openziti/storage/boltz"
"github.com/pkg/errors"
)
// EntityMarshaller instances can marshal and unmarshal entities of the type that they manage
// as well as knowing their entity type
type EntityMarshaller[T any] interface {
// GetEntityTypeId returns the entity type id. This is distinct from the Store entity id
// which may be shared by types, such as service and router. The entity type is unique
// for each type
GetEntityTypeId() string
// Marshall marshals the entity to a bytes encoded format
Marshall(entity T) ([]byte, error)
// Unmarshall unmarshalls the bytes back into an entity
Unmarshall(bytes []byte) (T, error)
}
// EntityCreator instances can apply a create entity command to create entities of a given type
type EntityCreator[T models.Entity] interface {
EntityMarshaller[T]
// ApplyCreate creates the entity described by the given command
ApplyCreate(cmd *CreateEntityCommand[T], ctx boltz.MutateContext) error
}
// EntityUpdater instances can apply an update entity command to update entities of a given type
type EntityUpdater[T models.Entity] interface {
EntityMarshaller[T]
// ApplyUpdate updates the entity described by the given command
ApplyUpdate(cmd *UpdateEntityCommand[T], ctx boltz.MutateContext) error
}
// EntityDeleter instances can apply a delete entity command to delete entities of a given type
type EntityDeleter interface {
GetEntityTypeId() string
// ApplyDelete deletes the entity described by the given command
ApplyDelete(cmd *DeleteEntityCommand, ctx boltz.MutateContext) error
}
// EntityManager instances can handle create, update and delete entities of a specific type
type EntityManager[T models.Entity] interface {
EntityCreator[T]
EntityUpdater[T]
EntityDeleter
}
type CreateEntityCommand[T models.Entity] struct {
Context *change.Context
Creator EntityCreator[T]
Entity T
PostCreateHook func(ctx boltz.MutateContext, entity T) error
Flags uint32
}
func (self *CreateEntityCommand[T]) Apply(ctx boltz.MutateContext) error {
return self.Creator.ApplyCreate(self, ctx)
}
func (self *CreateEntityCommand[T]) Encode() ([]byte, error) {
entityType := self.Creator.GetEntityTypeId()
encodedEntity, err := self.Creator.Marshall(self.Entity)
if err != nil {
return nil, errors.Wrapf(err, "error mashalling entity of type %T (%v)", self.Entity, entityType)
}
return cmd_pb.EncodeProtobuf(&cmd_pb.CreateEntityCommand{
Ctx: self.Context.ToProtoBuf(),
EntityType: entityType,
EntityData: encodedEntity,
Flags: self.Flags,
})
}
func (self *CreateEntityCommand[T]) GetChangeContext() *change.Context {
return self.Context
}
type UpdateEntityCommand[T models.Entity] struct {
Context *change.Context
Updater EntityUpdater[T]
Entity T
UpdatedFields fields.UpdatedFields
Flags uint32
}
func (self *UpdateEntityCommand[T]) Apply(ctx boltz.MutateContext) error {
return self.Updater.ApplyUpdate(self, ctx)
}
func (self *UpdateEntityCommand[T]) Encode() ([]byte, error) {
entityType := self.Updater.GetEntityTypeId()
encodedEntity, err := self.Updater.Marshall(self.Entity)
if err != nil {
return nil, errors.Wrapf(err, "error mashalling entity of type %T (%v)", self.Entity, entityType)
}
updatedFields, err := fields.UpdatedFieldsToSlice(self.UpdatedFields)
if err != nil {
return nil, err
}
return cmd_pb.EncodeProtobuf(&cmd_pb.UpdateEntityCommand{
Ctx: self.Context.ToProtoBuf(),
EntityType: entityType,
EntityData: encodedEntity,
UpdatedFields: updatedFields,
Flags: self.Flags,
})
}
type DeleteEntityCommand struct {
Context *change.Context
Deleter EntityDeleter
Id string
}
func (self *UpdateEntityCommand[T]) GetChangeContext() *change.Context {
return self.Context
}
func (self *DeleteEntityCommand) Apply(ctx boltz.MutateContext) error {
return self.Deleter.ApplyDelete(self, ctx)
}
func (self *DeleteEntityCommand) Encode() ([]byte, error) {
return cmd_pb.EncodeProtobuf(&cmd_pb.DeleteEntityCommand{
Ctx: self.Context.ToProtoBuf(),
EntityId: self.Id,
EntityType: self.Deleter.GetEntityTypeId(),
})
}
func (self *DeleteEntityCommand) GetChangeContext() *change.Context {
return self.Context
}
type SyncSnapshotCommand struct {
SnapshotId string
Snapshot []byte
SnapshotSink func(cmd *SyncSnapshotCommand) error
}
func (self *SyncSnapshotCommand) Apply(boltz.MutateContext) error {
return self.SnapshotSink(self)
}
func (self *SyncSnapshotCommand) Encode() ([]byte, error) {
return cmd_pb.EncodeProtobuf(&cmd_pb.SyncSnapshotCommand{
SnapshotId: self.SnapshotId,
Snapshot: self.Snapshot,
})
}
func (self *SyncSnapshotCommand) GetChangeContext() *change.Context {
return nil
}