Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vspinu opened this issue Jun 11, 2021 · 3 comments

Comments

@vspinu
Copy link
Contributor

vspinu commented Jun 11, 2021

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
@eddelbuettel
Copy link
Owner

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

@vspinu
Copy link
Contributor Author

vspinu commented Jun 12, 2021

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 protocolbuffers/protobuf#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.

@eddelbuettel
Copy link
Owner

That sounds like a plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants