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
Reflection support #28
Comments
@mppf commented ->
|
I don't think it will require a significant change in the existing APIs. We will have to add an extra interface over the existing APIs. That interface will provide complete information about the messages in the An example use case of reflection APIs in C#.
public void PrintMessage(IMessage message)
{
var descriptor = message.Descriptor;
foreach (var field in descriptor.Fields.InDeclarationOrder())
{
Console.WriteLine(
"Field {0} ({1}): {2}",
field.FieldNumber,
field.Name,
field.Accessor.GetValue(message);
}
} We can pass any message type to the above generic function and operate on its fields. We can also modify the above code to set field values of the message and serialize it, the converse is also true. The C# proto plugin dumps the entire proto message as a base64 encoded string in the generated file. Please see https://github.com/Aniket21mathur/ProtocolBuffers-Examples/blob/master/c%23/build/FriendRequest.cs#L15. This is then parsed by the language side library to generate message descriptor objects with field accessors and other useful functions.
I think the C++ example creates a bit of confusion, the C# one is more to the point. I don't think it should be possible to read a message from an input stream without knowing the message type, the decoding algorithms should not allow it(As far as I know).
I guess it would be good to start with the generic code, something similar to the above C# example. Able to write generic code is the basic use of reflection that I see. We can move to more advanced usage latter if we find it useful :) |
Re. the PrintMessage example from C#, we can already do this sort of thing: import Reflection;
record myRecord {
var a: int;
var b: real;
}
var exampleRecord = new myRecord(1, 200.0);
for param i in 0..<Reflection.numFields(myRecord) {
writeln("Field number=", i,
" name=", Reflection.getFieldName(myRecord, i),
" value=", Reflection.getField(exampleRecord, i));
} If protobuf users need some other specific "field number" then we might need a particular method to translate, but I think the Reflection module already covers most of this. |
Yes, the reflection module do a lot of job for us. I see we can also set field values of records with the reflection module.
Yes, we should have something which returns the actual field number of the proto field. |
The text was updated successfully, but these errors were encountered: