more
This commit is contained in:
@@ -9,8 +9,14 @@ import (
|
||||
"github.com/gogo/protobuf/proto"
|
||||
)
|
||||
|
||||
type ApplyResult struct {
|
||||
Type string `json:"type"`
|
||||
Mutation proto.Message `json:"mutation"`
|
||||
Error error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type MutationRegistry interface {
|
||||
Apply(grain any, msg ...proto.Message) error
|
||||
Apply(grain any, msg ...proto.Message) ([]ApplyResult, error)
|
||||
RegisterMutations(handlers ...MutationHandler)
|
||||
Create(typeName string) (proto.Message, bool)
|
||||
GetTypeName(msg proto.Message) (string, bool)
|
||||
@@ -145,12 +151,14 @@ func (r *ProtoMutationRegistry) Create(typeName string) (proto.Message, bool) {
|
||||
// Returns updated grain if successful.
|
||||
//
|
||||
// If the mutation is not registered, returns (nil, ErrMutationNotRegistered).
|
||||
func (r *ProtoMutationRegistry) Apply(grain any, msg ...proto.Message) error {
|
||||
func (r *ProtoMutationRegistry) Apply(grain any, msg ...proto.Message) ([]ApplyResult, error) {
|
||||
results := make([]ApplyResult, 0, len(msg))
|
||||
|
||||
if grain == nil {
|
||||
return fmt.Errorf("nil grain")
|
||||
return results, fmt.Errorf("nil grain")
|
||||
}
|
||||
if msg == nil {
|
||||
return fmt.Errorf("nil mutation message")
|
||||
return results, fmt.Errorf("nil mutation message")
|
||||
}
|
||||
|
||||
for _, m := range msg {
|
||||
@@ -159,18 +167,18 @@ func (r *ProtoMutationRegistry) Apply(grain any, msg ...proto.Message) error {
|
||||
entry, ok := r.mutationRegistry[rt]
|
||||
r.mutationRegistryMu.RUnlock()
|
||||
if !ok {
|
||||
return ErrMutationNotRegistered
|
||||
}
|
||||
if err := entry.Handle(grain, m); err != nil {
|
||||
return err
|
||||
results = append(results, ApplyResult{Error: ErrMutationNotRegistered, Type: rt.Name(), Mutation: m})
|
||||
continue
|
||||
}
|
||||
err := entry.Handle(grain, m)
|
||||
results = append(results, ApplyResult{Error: err, Type: rt.Name(), Mutation: m})
|
||||
}
|
||||
|
||||
// if entry.updateTotals {
|
||||
// grain.UpdateTotals()
|
||||
// }
|
||||
|
||||
return nil
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// RegisteredMutations returns metadata for all registered mutations (snapshot).
|
||||
|
||||
Reference in New Issue
Block a user