Skip to content

Low-level support for custom options in C##2317

Merged
jskeet merged 1 commit intoprotocolbuffers:masterfrom
jskeet:custom-options
Jan 19, 2017
Merged

Low-level support for custom options in C##2317
jskeet merged 1 commit intoprotocolbuffers:masterfrom
jskeet:custom-options

Conversation

@jskeet
Copy link
Copy Markdown
Contributor

@jskeet jskeet commented Nov 2, 2016

Not ready for submission yet - more tests required.
Creating as PR for feedback before going any further.

Will eventually resolve #2143.

// cc @gabm.

@jskeet jskeet added the c# label Nov 2, 2016
@jskeet jskeet force-pushed the custom-options branch 3 times, most recently from a7dadd8 to 02165d5 Compare January 5, 2017 08:15
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its better to decide which messages need to store unknown fields in protoc. It's a waste of code for descriptors other than options. Though only for descriptor.proto file, but worth to do so to make the code more clear.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make sense. Will take a while to rework this - am at a conference at the moment. Will look next week.

Copy link
Copy Markdown
Contributor

@anandolee anandolee Jan 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about remove this file and put them in code gen?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly - that's what I'll do next week. I expect the C# code in the support library to stay the same though, so it's still worth reviewing that.

Copy link
Copy Markdown
Contributor

@anandolee anandolee Jan 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other languages use templates, so users can get the options by GetExtension() instead of TryGet*. Is it possible to use template too? We promised to users the API of protobuf will be stable after 3.0.0, so we should think the APIs carefully.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean using generics, I wouldn't want to here... there are really only certain types that are feasible. We have generics for the message type, but I think it makes sense to stick to specific types for other things - we don't want to allow TryGet<Guid> for example.

For singular options, I think this is fine - and we may write code-gen to generate extra code per option anyway, which would "know" about both the type and the field number. But that can be added without changing this API at all (and indeed I'd expect it to use this).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I mean generics, I know there's some difference between C# generics and C++ template so make sense to me if you think it is better to stick to specific types.

Generate extra code per option to know the type and field number sounds good, so users can use field name instead of field number.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - but that can come later. This much unblocks users immediately, giving us more time to get the codegen right. We should think about repeated options, but I think that can come a bit later too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!
Does c++ support repeated options? Should be supported in c++ because it is an extension, but I didn't see unittest for that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any tests for that either, but I protoc allows it IIRC. The lack of any examples is one reason I don't think it's too crucial to do right now :) (In particular, I don't think it works for messages, because if an option is specified multiple times that basically means "Merge each occurrence into a single message.")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have replied to comments, but not pushed the new version yet as I'm still tracking down a bug. I may well add tests for the min/max values as well...

@jskeet
Copy link
Copy Markdown
Contributor Author

jskeet commented Jan 16, 2017

Rewritten to generate the appropriate code just for options, rather than using partial methods. The CustomOptions method now skips inappropriate wire formats instead of just returning that they should be skipped. Public API is the same as previous iteration.

PTAL @anandolee

Copy link
Copy Markdown
Contributor

@anandolee anandolee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in general. Thanks for the change

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. (Tabs are annoying...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more tests which has defined in the proto file? Like TestMessageWithCustomOptions etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for every possible option (other than oneof) and a couple for aggregate options.
I'm sure more tests could be added, but I don't think they'd test anything extra at this stage.
Adding the tests has found a bug though - I haven't yet fixed it; looking into it... (The value -9876543210 is being read as 19753086419 and I don't know why.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!
Does c++ support repeated options? Should be supported in c++ because it is an extension, but I didn't see unittest for that.

@jskeet
Copy link
Copy Markdown
Contributor Author

jskeet commented Jan 18, 2017

Looks like the problem is with zigzag encoding, which I'm not currently handling properly. Will need to change the CustomOptions class to mirror CodedInputStream, with all the different protobuf types (SInt32 etc). Will probably do that Friday. Definitely shows the benefits of testing - thanks!

@jskeet
Copy link
Copy Markdown
Contributor Author

jskeet commented Jan 18, 2017

Okay, pushed a second commit with requested changes. I've left it as a second commit at the moment rather than amending the first so that the change is obvious - I'll squash before merging.

Good job you requested more tests, so I was able to find the bug which suggested the API change. We dodged a bullet there :) This has also given me thoughts about what the generated options should look like when we get there - I can share that in a "not for submission" PR later...

PTAL.

@anandolee
Copy link
Copy Markdown
Contributor

LGTM

This consists of:
- Changing the codegen for the fixed set of options protos, to parse unknown fields instead of skipping them
- Add a new CustomOptions type in the C# support library
- Expose CustomOptions properties from the immutable proto wrappers in the support library

Only single-value options are currently supported, and fetching options values requires getting the type right
and knowing the field number. Both of these can be addressed at a later time.

Fixes protocolbuffers#2143, at least as a first pass.
@jskeet
Copy link
Copy Markdown
Contributor Author

jskeet commented Jan 18, 2017

Thanks. Found a rogue old file in Makefile.am - have pushed a new single commit for the whole PR; will merge on green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support custom options in C#

4 participants