more
All checks were successful
Build and Publish / Metadata (push) Successful in 8s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m12s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m9s

This commit is contained in:
matst80
2025-10-14 12:30:12 +02:00
parent 7a79efbb9f
commit 0b9c14c231
9 changed files with 201 additions and 25 deletions

View File

@@ -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).