@@ -307,6 +307,27 @@ func (s *Persistence) DebugClearAll(ctx context.Context) error {
307307 return err
308308}
309309
310+ // marshalStoredActor serializes dbActor for storage, clearing the identity
311+ // fields as they are stored in the key instead of the value. It mutates
312+ // dbActor: callers pass a clone they own.
313+ func marshalStoredActor (dbActor * ateapipb.Actor ) ([]byte , error ) {
314+ dbActor .Atespace = ""
315+ dbActor .ActorId = ""
316+ return protojson .Marshal (dbActor )
317+ }
318+
319+ // unmarshalStoredActor parses a stored actor value and backfills the identity
320+ // from the key components.
321+ func unmarshalStoredActor (data []byte , atespace , id string ) (* ateapipb.Actor , error ) {
322+ actor := & ateapipb.Actor {}
323+ if err := protojson .Unmarshal (data , actor ); err != nil {
324+ return nil , err
325+ }
326+ actor .Atespace = atespace
327+ actor .ActorId = id
328+ return actor , nil
329+ }
330+
310331func (s * Persistence ) GetActor (ctx context.Context , atespace , id string ) (* ateapipb.Actor , error ) {
311332 dbKey := actorDBKey (atespace , id )
312333
@@ -318,15 +339,11 @@ func (s *Persistence) GetActor(ctx context.Context, atespace, id string) (*ateap
318339 return nil , fmt .Errorf ("while getting actor key %q: %w" , dbKey , err )
319340 }
320341
321- actor := & ateapipb. Actor {}
322- if err := protojson . Unmarshal ( dbActorBytes , actor ); err != nil {
342+ actor , err := unmarshalStoredActor ( dbActorBytes , atespace , id )
343+ if err != nil {
323344 return nil , fmt .Errorf ("while unmarshaling actor: %w" , err )
324345 }
325346
326- if actor .GetActorId () != id || actor .GetAtespace () != atespace {
327- return nil , fmt .Errorf ("(impossible) mismatch between stored id/atespace and key" )
328- }
329-
330347 return actor , nil
331348}
332349
@@ -338,9 +355,9 @@ func (s *Persistence) CreateActor(ctx context.Context, actor *ateapipb.Actor) er
338355 dbActor := proto .Clone (actor ).(* ateapipb.Actor )
339356 dbActor .Version = 1
340357
341- dbActorBytes , err := protojson . Marshal (dbActor )
358+ dbActorBytes , err := marshalStoredActor (dbActor )
342359 if err != nil {
343- return fmt .Errorf ("in protojson.Marshal : %w" , err )
360+ return fmt .Errorf ("while marshaling actor : %w" , err )
344361 }
345362
346363 ok , err := s .rdb .SetNX (ctx , dbKey , dbActorBytes , 0 ).Result ()
@@ -538,22 +555,16 @@ func (s *Persistence) UpdateActor(ctx context.Context, actor *ateapipb.Actor, ex
538555 return store .ErrPersistenceRetry
539556 }
540557 dbActor .Version = currentActor .GetVersion () + 1
541- if currentActor .GetActorId () != dbActor .GetActorId () {
542- return fmt .Errorf ("actor_id is immutable" )
543- }
544- if currentActor .GetAtespace () != dbActor .GetAtespace () {
545- return fmt .Errorf ("atespace is immutable" )
546- }
547558 if currentActor .GetActorTemplateNamespace () != dbActor .GetActorTemplateNamespace () {
548559 return fmt .Errorf ("actor_template_namespace is immutable" )
549560 }
550561 if currentActor .GetActorTemplateName () != dbActor .GetActorTemplateName () {
551562 return fmt .Errorf ("actor_template_name is immutable" )
552563 }
553564
554- newVal , err := protojson . Marshal (dbActor )
565+ newVal , err := marshalStoredActor (dbActor )
555566 if err != nil {
556- return fmt .Errorf ("in protojson.Marshal : %w" , err )
567+ return fmt .Errorf ("while marshaling actor : %w" , err )
557568 }
558569
559570 _ , err = tx .TxPipelined (ctx , func (pipe redis.Pipeliner ) error {
@@ -768,7 +779,7 @@ func (s *Persistence) fetchActors(ctx context.Context, master *redis.Client, key
768779 }
769780
770781 var actors []* ateapipb.Actor
771- for _ , cmd := range cmds {
782+ for i , cmd := range cmds {
772783 getCmd , ok := cmd .(* redis.StringCmd )
773784 if ! ok {
774785 continue
@@ -780,9 +791,13 @@ func (s *Persistence) fetchActors(ctx context.Context, master *redis.Client, key
780791 return nil , fmt .Errorf ("while getting actor: %w" , getCmd .Err ())
781792 }
782793
783- actor := & ateapipb.Actor {}
784- if err := protojson .Unmarshal ([]byte (getCmd .Val ()), actor ); err != nil {
785- return nil , fmt .Errorf ("in protojson.Unmarshal: %w" , err )
794+ parts := strings .Split (keys [i ], ":" )
795+ if len (parts ) != 3 {
796+ return nil , fmt .Errorf ("bad key format %q" , keys [i ])
797+ }
798+ actor , err := unmarshalStoredActor ([]byte (getCmd .Val ()), parts [1 ], parts [2 ])
799+ if err != nil {
800+ return nil , fmt .Errorf ("while unmarshaling actor: %w" , err )
786801 }
787802 actors = append (actors , actor )
788803 }
0 commit comments