testing promotion actions
This commit is contained in:
@@ -15,11 +15,32 @@ type ApplyResult struct {
|
||||
Error error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type MutationProcessor interface {
|
||||
Process(grain any) error
|
||||
}
|
||||
|
||||
type BasicMutationProcessor[V any] struct {
|
||||
processor func(any) error
|
||||
}
|
||||
|
||||
func NewMutationProcessor[V any](process func(V) error) MutationProcessor {
|
||||
return &BasicMutationProcessor[V]{
|
||||
processor: func(v any) error {
|
||||
return process(v.(V))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *BasicMutationProcessor[V]) Process(grain any) error {
|
||||
return p.processor(grain)
|
||||
}
|
||||
|
||||
type MutationRegistry interface {
|
||||
Apply(grain any, msg ...proto.Message) ([]ApplyResult, error)
|
||||
RegisterMutations(handlers ...MutationHandler)
|
||||
Create(typeName string) (proto.Message, bool)
|
||||
GetTypeName(msg proto.Message) (string, bool)
|
||||
RegisterProcessor(processor ...MutationProcessor)
|
||||
//GetStorageEvent(msg proto.Message) StorageEvent
|
||||
//FromStorageEvent(event StorageEvent) (proto.Message, error)
|
||||
}
|
||||
@@ -27,6 +48,7 @@ type MutationRegistry interface {
|
||||
type ProtoMutationRegistry struct {
|
||||
mutationRegistryMu sync.RWMutex
|
||||
mutationRegistry map[reflect.Type]MutationHandler
|
||||
processors []MutationProcessor
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -114,9 +136,14 @@ func NewMutationRegistry() MutationRegistry {
|
||||
return &ProtoMutationRegistry{
|
||||
mutationRegistry: make(map[reflect.Type]MutationHandler),
|
||||
mutationRegistryMu: sync.RWMutex{},
|
||||
processors: make([]MutationProcessor, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProtoMutationRegistry) RegisterProcessor(processors ...MutationProcessor) {
|
||||
r.processors = append(r.processors, processors...)
|
||||
}
|
||||
|
||||
func (r *ProtoMutationRegistry) RegisterMutations(handlers ...MutationHandler) {
|
||||
r.mutationRegistryMu.Lock()
|
||||
defer r.mutationRegistryMu.Unlock()
|
||||
@@ -198,6 +225,12 @@ func (r *ProtoMutationRegistry) Apply(grain any, msg ...proto.Message) ([]ApplyR
|
||||
results = append(results, ApplyResult{Error: err, Type: rt.Name(), Mutation: m})
|
||||
}
|
||||
|
||||
for _, processor := range r.processors {
|
||||
err := processor.Process(grain)
|
||||
if err != nil {
|
||||
return results, err
|
||||
}
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user