@@ -299,6 +299,27 @@ func (s *Persistence) DebugClearAll(ctx context.Context) error {
299299 return err
300300}
301301
302+ // marshalStoredActor serializes dbActor for storage, clearing the identity
303+ // fields as they are stored in the key instead of the value. It mutates
304+ // dbActor: callers pass a clone they own.
305+ func marshalStoredActor (dbActor * ateapipb.Actor ) ([]byte , error ) {
306+ dbActor .Atespace = ""
307+ dbActor .ActorId = ""
308+ return protojson .Marshal (dbActor )
309+ }
310+
311+ // unmarshalStoredActor parses a stored actor value and backfills the identity
312+ // from the key components.
313+ func unmarshalStoredActor (data []byte , atespace , id string ) (* ateapipb.Actor , error ) {
314+ actor := & ateapipb.Actor {}
315+ if err := protojson .Unmarshal (data , actor ); err != nil {
316+ return nil , err
317+ }
318+ actor .Atespace = atespace
319+ actor .ActorId = id
320+ return actor , nil
321+ }
322+
302323func (s * Persistence ) GetActor (ctx context.Context , atespace , id string ) (* ateapipb.Actor , error ) {
303324 dbKey := actorDBKey (atespace , id )
304325
@@ -310,15 +331,11 @@ func (s *Persistence) GetActor(ctx context.Context, atespace, id string) (*ateap
310331 return nil , fmt .Errorf ("while getting actor key %q: %w" , dbKey , err )
311332 }
312333
313- actor := & ateapipb. Actor {}
314- if err := protojson . Unmarshal ( dbActorBytes , actor ); err != nil {
334+ actor , err := unmarshalStoredActor ( dbActorBytes , atespace , id )
335+ if err != nil {
315336 return nil , fmt .Errorf ("while unmarshaling actor: %w" , err )
316337 }
317338
318- if actor .GetActorId () != id || actor .GetAtespace () != atespace {
319- return nil , fmt .Errorf ("(impossible) mismatch between stored id/atespace and key" )
320- }
321-
322339 return actor , nil
323340}
324341
@@ -330,9 +347,9 @@ func (s *Persistence) CreateActor(ctx context.Context, actor *ateapipb.Actor) er
330347 dbActor := proto .Clone (actor ).(* ateapipb.Actor )
331348 dbActor .Version = 1
332349
333- dbActorBytes , err := protojson . Marshal (dbActor )
350+ dbActorBytes , err := marshalStoredActor (dbActor )
334351 if err != nil {
335- return fmt .Errorf ("in protojson.Marshal : %w" , err )
352+ return fmt .Errorf ("while marshaling actor : %w" , err )
336353 }
337354
338355 ok , err := s .rdb .SetNX (ctx , dbKey , dbActorBytes , 0 ).Result ()
@@ -529,22 +546,16 @@ func (s *Persistence) UpdateActor(ctx context.Context, actor *ateapipb.Actor, ex
529546 return store .ErrPersistenceRetry
530547 }
531548 dbActor .Version = currentActor .GetVersion () + 1
532- if currentActor .GetActorId () != dbActor .GetActorId () {
533- return fmt .Errorf ("actor_id is immutable" )
534- }
535- if currentActor .GetAtespace () != dbActor .GetAtespace () {
536- return fmt .Errorf ("atespace is immutable" )
537- }
538549 if currentActor .GetActorTemplateNamespace () != dbActor .GetActorTemplateNamespace () {
539550 return fmt .Errorf ("actor_template_namespace is immutable" )
540551 }
541552 if currentActor .GetActorTemplateName () != dbActor .GetActorTemplateName () {
542553 return fmt .Errorf ("actor_template_name is immutable" )
543554 }
544555
545- newVal , err := protojson . Marshal (dbActor )
556+ newVal , err := marshalStoredActor (dbActor )
546557 if err != nil {
547- return fmt .Errorf ("in protojson.Marshal : %w" , err )
558+ return fmt .Errorf ("while marshaling actor : %w" , err )
548559 }
549560
550561 _ , err = tx .TxPipelined (ctx , func (pipe redis.Pipeliner ) error {
@@ -755,7 +766,7 @@ func (s *Persistence) fetchActors(ctx context.Context, master *redis.Client, key
755766 }
756767
757768 var actors []* ateapipb.Actor
758- for _ , cmd := range cmds {
769+ for i , cmd := range cmds {
759770 getCmd , ok := cmd .(* redis.StringCmd )
760771 if ! ok {
761772 continue
@@ -767,9 +778,13 @@ func (s *Persistence) fetchActors(ctx context.Context, master *redis.Client, key
767778 return nil , fmt .Errorf ("while getting actor: %w" , getCmd .Err ())
768779 }
769780
770- actor := & ateapipb.Actor {}
771- if err := protojson .Unmarshal ([]byte (getCmd .Val ()), actor ); err != nil {
772- return nil , fmt .Errorf ("in protojson.Unmarshal: %w" , err )
781+ parts := strings .Split (keys [i ], ":" )
782+ if len (parts ) != 3 {
783+ return nil , fmt .Errorf ("bad key format %q" , keys [i ])
784+ }
785+ actor , err := unmarshalStoredActor ([]byte (getCmd .Val ()), parts [1 ], parts [2 ])
786+ if err != nil {
787+ return nil , fmt .Errorf ("while unmarshaling actor: %w" , err )
773788 }
774789 actors = append (actors , actor )
775790 }
0 commit comments