rprotobuf icon indicating copy to clipboard operation
rprotobuf copied to clipboard

Provide a method to retrieve string value of enums (and add an option to `as.list`)

Open vspinu opened this issue 4 years ago • 3 comments

I don't see a straightforward way to retrieve an enum name from the objects, only the id.

message <- new(tutorial.Person, name = "foo",
               phone = list(new(tutorial.Person.PhoneNumber,
                                number = "+33(0)...", type = "HOME"),
                            new(tutorial.Person.PhoneNumber,
                                number = "+33(0)###", type = "MOBILE")
                            ))
message$phone[[1]]$type
# [1] 1 

I would like to have "HOME" instead.

Similarly, I would also expect as.list to be able to return names instead of numbers.

> as.list(message$phone[[1]])
$number
[1] "+33(0)..."

$type
[1] 1

vspinu avatar Jun 11 '21 15:06 vspinu

Interesting. I have no idea what idiomatic Protocol Buffers code would do or look like with enum fields like these. By chance, did you look into what other bindings (Python ?) do?

As always, we are open to careful PRs especially when they manage to remain narrow (and ideally follow our coding conventions so ChangeLog entries appreciated as are added unit tests).

eddelbuettel avatar Jun 11 '21 15:06 eddelbuettel

Python has two functions MessageToDict and MessageToJson with the same interface:

MessageToDict(ev,
              including_default_value_fields = True,
              preserving_proto_field_name = True,
              use_integers_for_enums = False)

With #79 toJSON has almost the same powers except that use_integers_for_enums is not exposed. We can fix that.

So I would suggest enhancing the current as.list with the same interface as toJSON, or maybe creating a new method toList. Note that python functions and toJSON are recursive and I think that's what most users would want.

Unfortunately json parsing does not preserve the protobuf type. I have opened https://github.com/protocolbuffers/protobuf/issues/8727 for that but I don't think it will be addressed soon, so I would rather get the full featured as.list in here. Ideally such a functions should be able to handle google built-in types like Date, Timestamp etc. and convert into R types automatically.

vspinu avatar Jun 12 '21 10:06 vspinu

That sounds like a plan.

eddelbuettel avatar Jun 12 '21 11:06 eddelbuettel