Automatic protobuf serialization for ActorRefs

I’d like to configure scalaPB to automatically convert actorRefs to and from their serialized string format.

This can be done in theory using scalaPB TypeMappers, and such a type mapper could looks as simple as this:

    TypeMapper[String, ActorRef](system.provider.resolveActorRef)(Serialization.serializedActorPath)

I.e. we instruct scalaPB to serialize from actorRef to string using Serialization.serializedActorPath, and from string to actorRef using system.provider.resolveActorRef.

Then you could use it like this in a protobuf message:

  string recipient = 1 [(scalapb.field).type = ""];

Now, the problem is that system is of course not available to ScalaPB at that time in the TypeMapper. At least not the way it’s done above.

Is there any other way how I can automatize the serialization/deserialization of actorRefs, so that I don’t have to add an extra wrapper class for each actorRef in messages? A way how I can make the actorSystem available “at compile time”? Maybe a suggestion involving dependency injection frameworks? Or a solution based on Akka serialization adapters?

PS: With implicit conversion (implicit unwrapping) I get half way, i.e. by having an implicit converter that takes an implicit ActorSystem, and implicitly returns ActorRef. Only wrappedRef ! message does not work, because the tell also has an implicit sender argument, which becomes just too many implicits for the compiler to handle.

The way I’m doing it now is to provide the ActorSystem to ScalaPB by registering the system on a companion Object (singleton), which is done through an AkkaExtension that can be loaded through the application.conf. Now I can pass ActorRefs straight in and out of protobuf messages without any implicits or manual conversion.

If someone has a better idea, I’m happy to hear. Otherwise, this works great. All I have to do is add the extension to the .conf, and from then on I don’t have to worry about ActorRef serialization.

You should be able to get the ActorSystem from Serialization.getCurrentTransportInformation().

Incidentally I created a ticket last week about documenting this. If you get it working you could add a note to that ticket.

Incidentally I created a ticket last week

That’s indeed quite coincidental. Your solution works and I have posted my implementation.