-
Notifications
You must be signed in to change notification settings - Fork 15
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
Cleanups/improvements to protobuf package #3
Comments
I can take on a couple of these changes. I particularly agree that the Read/Write functions would be very useful to have, and the change to |
With this and GenerateProto I would suggest having an options type with members for each option. So for Decode eg. type DecodeOptions struct {
Constructors []Constructor
}
func Decode(buf []byte, ptr interface{}, options *DecodeOptions) {} This is more idiomatic Go as it is type-safe and discoverable.
YAGNI?
Similar to above I think this should be an
Yes, good idea. These were left public from when GenerateProto was an external package.
This might be nice but I don't think it's necessary. It is idiomatic Go as long as the panic doesn't escape (eg. the JSON package uses panic/recover internally). |
Thanks Alec for your feedback. Having a DecodeOptions struct is certainly a reasonable approach; the main downside is that the only way to add new options in the long-term is to modify the definition of that struct, and hence risk breaking compatibility (e.g., with code that tries to create a DecodeOptions instance with a positional struct-initializer). One could of course simply state in a comment that code should not depend on the exact membership of struct DecodeOptions, but that feels slightly less than satisfactory. Another very minor nitpick is that callers always have to provide a third option - even if only 'nil' - when they have no options to pass. The reason I like the 'options ...interface{}' approach is because it's syntactically very clean especially in the no-options case (i.e., you can just call Decode(buf, ptr)), and because of Go's type-switch construct it's very easy and fairly efficient to "parse" an options-list on the call: e.g., within Decode(): var cons Constructors In the common case when the options list is empty or nearly empty, this should add very little runtime overhead (although I haven't measured exactly "how much") vs just using a struct. |
Worst case with an explicit options struct is a compile-time error. Worst case (eg. if you remove an option, or change the type of an option) with |
I'd be happy to help with some of the refactoring. @jackowitzd2 , which tasks items were you planning on tackling? |
I've got a branch (cleanup) with bullet points 2-5 addressed, although I still need to write some tests for the new functionality. I think the first point may need some discussion about what exactly the tags should be, but if you can take on the generator stuff that'd be great! |
Sounds good. I'll start working on the generator. |
Great. I believe most of that code was written by @alecthomas, so maybe see if he has any more input on the changes regarding it. |
Will do. @alecthomas , are you free sometime over the next few days to meet up to discuss the code? |
I'm pretty sure he's on a different continent than us :) I just meant seeing where the above discussion goes before jumping in. Sorry for the confusion! |
Also, to revive this thread - I think the only outstanding/controversial issue was the handling of decoding options. In retrospect there's a third approach that at the moment I think I like even better than either the DecodeOptions struct parameter or the options ...interface{} parameter: namely, just create a 'Decoder' struct type, which contains fields and/or methods for setting decoding options, plus a 'Decode(...)' method whose signature is identical to the current 'Decode(...)' function (but is a method of that Decoder struct rather than a function). The regular Decode(...) function, if we keep it, would then be equivalent to '&Decoder{}.Decode(...)'. Sound reasonable? |
Sounds like a good plan to me. +1 |
@jackowitzd2 @WEB3-GForce are either of you actively working on this? I'd like to make some progress on it even if not all at once. Thanks. |
The "cleanup" branch addresses items 2-5 in the initial list in this thread. I'll make sure that the test coverage is good, then open up a pull request. |
I also missed the resolution of the varags/DecodeOptions question. I'll go back and bring things up to date with the Decoder proposal. |
Good, thanks Danny. |
As discussed with you in our meeting on Tuesday, my primary focus is on life insurance at the moment. But, I should have some more time opening up later this week/ this weekend to do this as well. |
This issue has a lot of good ideas and we should try to implement them at some point ! |
Agreed :) |
I'd like to propose the following set of cleanups and other minor improvements (or at least what I see as improvements) for the protobuf package:
@alecthomas and @jackowitzd2 - do these seem reasonable or would any cause problems that you know of (beyond trivial API alignment fixes in dependent code)? Any other suggestions/ideas to improve the protobuf API further?
And can someone start working on these changes in a branch or fork (keeping in mind that this proposal is "tentative" and may further need to change of course)? Perhaps @WEB3-GForce or @jackowitzd2 since you've been playing with encoding/decoding anyway?
Thanks
Bryan
The text was updated successfully, but these errors were encountered: