-
Notifications
You must be signed in to change notification settings - Fork 811
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
Allow usage of message types with customtype override #239
Allow usage of message types with customtype override #239
Conversation
Looks like the build failed for protoc |
Doesn't seem like you regenerated the code. |
8fc2c1e
to
472f7d8
Compare
sweet, regenerated the code using protoc 3.1.0 and golang 1.7.1 |
@awalterschulze what do you think of the change ? |
Ok 125 does not seem to apply here, since it is trying to have a proto message as the customtype and is still keeping the proto field type as type bytes. |
Ok maybe none of them are applicable. |
Basically you just want to be able to have a custom type where the proto field is of some message type and not only of type bytes. If so I am pretty sure you'll need more tests. |
So yes I am interested in adding this feature :) |
yes, that's right
sweet, i'll work on the tests and get back to you |
so i added the tests as requested. currently there are two failres. one related to the text representation of |
No both are just reflection woes in the jsonpb and prototext libraries respectively. |
So I got to fixing the tests for both json and text marshaling. I'm less comfortable with the json marshaling change, since it requires the custom type to implement |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall super positive about this :)
@@ -55,7 +55,11 @@ message A { | |||
message B { | |||
option (gogoproto.description) = true; | |||
optional A A = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true]; | |||
repeated bytes G = 2 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable) = false]; | |||
optional G G = 2 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uint128"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets not change this example or add an extra one
@@ -624,3 +624,11 @@ message Node { | |||
repeated Node Children = 2; | |||
} | |||
|
|||
message NonByteCustomType { | |||
option (gogoproto.compare) = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is compare not supported?
pm, ok := iface.(proto.Message) | ||
if !ok { | ||
if prop.CustomType == "" { | ||
return fmt.Errorf("%v does not implement proto.Message", v.Type()) | ||
} | ||
t := proto.MessageType(prop.CustomType) | ||
// fmt.Printf("################ %T, %T, %s\n", i, t, prop.CustomType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comment
@@ -725,6 +738,15 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe | |||
|
|||
// Handle nested messages. | |||
if targetType.Kind() == reflect.Struct { | |||
if target.CanAddr() { | |||
// fmt.Printf("################ %T\n", target.Interface()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comment
If you want to add documentation as you mentioned for customtype that will be really really awesome. |
6a681e2
to
8bb6cdf
Compare
Removed the comments, made the message comparable and added a new document for custom types. With hindsight, I should not have squashed the commits to make it easier to review, sorry about that. I'm also not sure if the new documentation fixes #201 ? |
@@ -624,3 +624,10 @@ message Node { | |||
repeated Node Children = 2; | |||
} | |||
|
|||
message NonByteCustomType { | |||
optional ProtoType Field1 = 1 [(gogoproto.customtype) = "GoType"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just thought of this the other day, but can we also have structs which use nullable=false and repeated custom types, similar to NidOptCustom, NinOptCustom, NidRepCustom and NinRepCustom
@@ -59,6 +59,7 @@ Issues with customtype include: | |||
* <a href="https://github.com/gogo/protobuf/issues/125">Using a proto message as a customtype is not allowed.</a> | |||
* <a href="https://github.com/gogo/protobuf/issues/200">cusomtype of type map can not UnmarshalText</a> | |||
* <a href="https://github.com/gogo/protobuf/issues/201">customtype of type struct cannot jsonpb unmarshal</a> | |||
* Custom types are required to implement a handful of methods. See [CustomTypes](custom_types.md) for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can move all these issues to the customtypes.md file and then simply reference the customtypes.md file in the table above.
optional bytes Field = 1 [(gogoproto.customtype) = "GoType"]; | ||
} | ||
|
||
message ProtoType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove ProtoType from the bytes example
func (gt *GoType) UnmarshalJSON(data []byte) error | ||
|
||
func (gt GoType) Compare(other GoType) int // only required if the comparable option is set | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about Equal and NewPopulated, etc.
I think the comment about a method being optional can be added above the method.
You can also get the highlighting to keep on working by simply adding curly braces to the function definitions.
func (r GoType) Marshal() ([]byte, error) {}
or alternatively you can declare the field type in the protocol message to be | ||
`bytes`: | ||
|
||
``` protocol-buffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its simply ```proto
The custom type must define the following methods with the given | ||
signatures. Assuming the custom type is called `GoType`: | ||
|
||
``` go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also add a link to your test file go_type.go
Also you are welcome to add yourself to the AUTHORS or CONTRIBUTORS file as appropriate. |
Yes I think might fix #201 @ericlagergren what do you think? |
I really like the docs, I think its really clear and will result in less confusion from users in future. |
Docs look great! I'd suggest using I could give a more in-depth look over on Sunday or Monday, I'm on my phone right now 😉 |
Sorry didn't get a chance to look into the comments until now. They all sound reasonable. I'll try to go through them and push the appropriate fixes as soon as I can. Thanks! |
I think I covered all the feedback y'all gave me. I didn't force push to make it easier to review the changes. Once everything is okay I can squash everything and rebase on master. Let me know what you think |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
``` | ||
|
||
Check [t.go](test/t.go) for a full example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this link actually work?
Really good work. Thank you very much. |
To resolve conflicts I think you simply need to regenerate |
4bab499
to
4e62173
Compare
Sweet. Force pushed the squashed changes. Thanks for the thorough review and accepting this pr. BTW, there were minor changes to the proto3 test suite generated files. I'm not sure if that's ok or not. Let me know if they shouldn't have changed and i can look into it. |
Looks fine. |
this pr tries to fix an issue with gogoproto when a customtype is used in conjuction with a message type, e.g.:
The idea is to decorate the map in golang but allow non Golang (e.g. Ruby)
clients to be generated with reasonable types. The alternative of using
bytes
as the type of
routes
in this case generate non Golang marshal methods thatare dumb and won't do any serialization/checking on the values of the
routes
field.
note this pr is just a demonstration and we don't expect it to be merged as
is. we are happy for feedback and will do the necessary updates to get it
merged in.
fyi, the code sample mentioned above is from the diego bbs models that is used in cloudfoundry