-
Notifications
You must be signed in to change notification settings - Fork 196
Mechanism to generate Extension functions #35
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
Comments
You could always define a type like type Any interface {
msgp.Marshaler
msgp.Unmarshaler
}
type Abstract struct {
Type int8
Value Any
}
type MyStruct struct {
Thing1 Abstract
Thing2 Abstract
/* ... and so forth */
} But I'm not quite sure I understand your use-case. |
Use case is to have an arbitrary type as a member of a generic message.
then, do something like:
Could accomplish basically the same thing if Msg looked like
But was looking to do something a bit more automagical. |
I think this still may not be clear. gob does this by encoding the type description when it first encounters a type msgp does this via extension types I was looking to lower the amount of hand written code, per type, to make this happen. |
Ok. I've given this some thought. I would discourage you from using extensions to achieve polymorphism; that is not the intended purpose of extensions in the protocol spec. They are intended for opaque application-specific binary that is not (necessarily) self-describing. ( What you want to do is encode your polymorphic objects as I'm going to see if I can find a nice way to generate code that will do this for you, but for now I would recommend doing it manually using the facilities provided in the package. I would also encourage you to take a look at the struct decoding rules in the wiki. Since the generated methods decode an intersection of the encoded data and the struct, you can set up your structs with nullable fields and then check post-decoding what values existed in the message. That is the preferred method of "polymorphism." |
Yeah, I was thinking about this a bit more too. And I ended up going with your initial (and this) suggestion of having a type + data. I was able to minimize the work needed by just having a DecodeBody that does a switch and constructs a struct to return (I made a Command interface that all structs comply with) works fine Not sure if there's anything you can do easily generation wise other than maybe have a type map // generate: msgp -auto Foo which would create a FooRegistry where you can FooRegistry.Add(1,MyType{}) you would have to reflect on the encode function to know the type num to use Still, feels somewhat counter to your goal on this project which I take to be about speed and relative simplicity for the user. I'd probably leave it out all together and make knuckleheads like me do nullable fields or 2 elem array style, but hand rolled as I think no generalized version would be worth the added complexity to your API Thank you for your help, and an awesome library. Feel free to close this, I'm leaving open for now so you see the response. |
Cool. Let me know if anything else comes up. |
When one simply wants to create a set of custom types to be used as a field of type interface{}, you really just want to register a type number with a normal struct.
Currently, you must add 4 boiler plate functions to satisfy the Extension interface.
Is there a reason for making the extension interface not use the same function names as (Un)Marshaller and add the additional ExtensionType function?
Example code I have copy/pasted (and renamed struct) in my code:
The text was updated successfully, but these errors were encountered: