Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Create new method with the FieldMask WKT #9

Closed
johanbrandhorst opened this issue Mar 17, 2018 · 45 comments
Closed

Create new method with the FieldMask WKT #9

johanbrandhorst opened this issue Mar 17, 2018 · 45 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@johanbrandhorst
Copy link
Member

Need to find how to work around https://github.com/grpc-ecosystem/grpc-gateway/blob/58f78b988bc393694cef62b92c5cde77e4742ff5/runtime/query.go#L275 in gRPC-Gateway.

@johanbrandhorst johanbrandhorst added the enhancement New feature or request label Mar 17, 2018
@johanbrandhorst
Copy link
Member Author

I tried playing around with this and I've come to several conclusions for now:

  1. This definitely won't work with gogo/protobuf as it is currently implemented in the gRPC-Gateway. This is because as is evident from the source, it only checks if the proto.MessageName equals a certain value against those registered with golang/protobuf. Attempting to use the golang/protobuf generated protofile isn't an option here either, as gogo/protobuf assumes nested fields implement Marshal and Unmarshal.
  2. I don't know if this even works as intended in the gRPC-Gateway today. See my comment on the PR that introduced this.

@johanbrandhorst
Copy link
Member Author

Tagging @timonwong to see if he has anything to add to this discussion.

@johanbrandhorst johanbrandhorst added the question Further information is requested label Mar 17, 2018
@awalterschulze
Copy link
Member

Nice. Yes lets first see how it should work with golang/protobuf and then try to get it working with gogo/protobuf. Thanks so much for pushing this forward :)

@johanbrandhorst
Copy link
Member Author

I was doing some more testing of field masks in the grpc-gateway, and surprisingly it works with protoc-gen-go, so there's potentially something here that protoc-gen-gogo can do differently, but I'm not entirely sure what yet.

I created https://github.com/johanbrandhorst/field-mask-test as an example of using the gRPC gateway with a field mask, and it works (???). I will try and reproduce the same thing in the gogo/grpc-example repo and see where it breaks next.

@johanbrandhorst
Copy link
Member Author

I'm also continuing the discussion in grpc-ecosystem/grpc-gateway#529 (comment)

@johanbrandhorst
Copy link
Member Author

Interesting development; if gogo/protobuf WKT implemented XXX_MessageName it could resolve the issue with using golang/protobuf.MessageName specifically: grpc-ecosystem/grpc-gateway#529 (comment). What are you thoughts here @awalterschulze?

@awalterschulze
Copy link
Member

IF it really helps to solve the problem then:

  • I am more than open to adding that method to each message that needs it
  • I could even add a flag to allow this method to be generated or not, even for other messages
    But I am worried that this still requires being registered in the correct registry.

@johanbrandhorst
Copy link
Member Author

It would only solve it in the proto.MessageName case, it doesn't solve the general proto.MessageType lookup case. I think it would be worthwhile implementing for this as I can't seem to find anywhere the gRPC-Gateway uses proto.MessageType directly. Shall I raise an issue?

@awalterschulze
Copy link
Member

Ok so they are only using proto.MessageName and this fixes the compatibility issue?
Because then I am totally in!

@johanbrandhorst
Copy link
Member Author

I did a quick search for proto.MessageType and couldn't find any uses, so yeah, perhaps? It doesn't solve the problem of the jsonpb marshaler using golang/protobuf but that's completely configurable by the user at least.

@johanbrandhorst
Copy link
Member Author

I think we should consider restricting it to the WKT though, since the XXX_Stuff is kind of restricted?

@awalterschulze
Copy link
Member

Ok what about we make a pull request on gogoprotobuf types folder and add XXX_MessageName methods to all the wkts and see if fieldmask starts working?

@awalterschulze
Copy link
Member

If it solves a problem, we merge it :)

@johanbrandhorst
Copy link
Member Author

@awalterschulze lets do it

@awalterschulze
Copy link
Member

cool :)
Do you want to experiment with it locally or should I make a pull request?

@johanbrandhorst
Copy link
Member Author

Uh, I suppose I could try it out first, sure, let me get back to you.

@awalterschulze
Copy link
Member

Thank you so much. I really appreciate it.

@timonwong
Copy link

timonwong commented Mar 29, 2018

@johanbrandhorst
It seems we will get more benefits by implementing XXX_MessageName:

  1. grpc.Status.WithDetails should work correctly since it invokes ptypes.MarshalAny() function, previously we will get wrong type url:

https://github.com/grpc/grpc-go/blob/f72b28a6d170d0938afd483331ba4e5d733785ae/status/status.go#L142-L158

  1. It should also address this issue as well: Impossible to use gogo/protobuf registered types in gRPC Status errors grpc-ecosystem/grpc-gateway#576

@johanbrandhorst
Copy link
Member Author

@timonwong I can investigate that at the same time, that would be huge.

@awalterschulze
Copy link
Member

Oh wow

// MarshalAny takes the protocol buffer and encodes it into google.protobuf.Any.
func MarshalAny(pb proto.Message) (*Any, error) {
	value, err := proto.Marshal(pb)
	if err != nil {
		return nil, err
	}
	return &Any{TypeUrl: googleApis + proto.MessageName(pb), Value: value}, nil
}

Seems like that should work :)

So then generating XXX_MessageName for all messages should solve MarshalAny.

@awalterschulze
Copy link
Member

Looking at the code it looks like even UnmarshalAny should work.
This is huge

@timonwong
Copy link

Another caveat about jsonpb codec in grpc-gateway to handle google.rpc.* (eg. google.rpc.RetryInfo) correctly, we may have two options:

  1. specify AnyResolver explicitly, because even we have XXX_MessageName implemented, it requires google/protobuf type registry (proto.MessageType)
  2. import google.golang.org/genproto/googleapis/rpc/errdetails along with gogo/googleapis along

@johanbrandhorst
Copy link
Member Author

johanbrandhorst commented Mar 29, 2018

The issue is handled by supplying a custom JSON marshaller, we'll never be able to work around the problem of proto.MessageType this easily.

@johanbrandhorst
Copy link
Member Author

The latest merge to this repo shows how to use the gogo/googleapis types with the gRPC-Gateway already.

@johanbrandhorst
Copy link
Member Author

I've made some progress on this. In addition to adding XXX_MessageName() string to the FieldMask type, I've had to patch the JSON marshaller I was using (the cockroachdb one). It wasn't able to unmarshal pointers to types that implement the proto.Message. I've not yet figured it out exactly what needs doing as I'm still getting some marshalling errors, but I think we can go ahead and start work on defining XXX_MessageName() string for the WKT types @awalterschulze. I'm not sure what it would entail exactly, and I consider that your area of expertise ;).

@johanbrandhorst
Copy link
Member Author

Replacing the cockroachdb Marshaler with https://github.com/abursavich/gogomarshal (from gogo/protobuf#166) works out of the box (when XXX_MessageName() string is implemented in FieldMask).

It does output these worrying warnings though:

proto: no encoder for wall uint64 [GetProperties]
proto: no encoder for ext int64 [GetProperties]
proto: no encoder for loc *time.Location [GetProperties]

Now I'm sure I've seen this before, but I can't remember where or why. @awalterschulze any ideas about what causes this?

@johanbrandhorst
Copy link
Member Author

They don't happen with the cockroachdb Marshaler 🤔

@johanbrandhorst
Copy link
Member Author

I've just pushed up #11 which is a working example of using google.protobuf.FieldMask. I've switch marshaler and manually implemented XXX_MessageName on FieldMask.

Outstanding fixes:

  • Figure out why it outputs errors:
    proto: no encoder for wall uint64 [GetProperties]
    proto: no encoder for ext int64 [GetProperties]
    proto: no encoder for loc *time.Location [GetProperties]
    
  • Properly implement XXX_MessageName on WKTs.

@awalterschulze
Copy link
Member

@johanbrandhorst

On the warnings the solution seems to be in this issue
gogo/protobuf#216

And here is an original issue report
gogo/protobuf#212

The solution seems to have something to do with

defaultMarshaler = &JSONPb{OrigName: true}

But, since I am not gRPC user :( I never confirmed it.

@awalterschulze
Copy link
Member

@johanbrandhorst on XXX_MessageName

I just want to confirm, this won't only be needed for FieldMask, but potentially for all user defined protobuf messages.

  1. If so then I am thinking of adding another message/file option messagename=true that will result in a XXX_MessageName method being generated for the enabled messages.

  2. If not then we can write some manual code in gogoprotobuf for the specific types that need them.

What do you think?

@johanbrandhorst
Copy link
Member Author

Yeah I'm not sure which yet, this could potentially be an alternative to goproto_registration. Implement it as an option to start with I think. I guess there's no harm in users turning it on for their messages or files.

@johanbrandhorst
Copy link
Member Author

Doesn't seem like either of those issue figured out why the messages appeared, so I'll do some more digging. Setting OrigName: true does not solve the issue.

@awalterschulze
Copy link
Member

Oh that is very interesting. Hmmm.

@awalterschulze
Copy link
Member

In the mean time. What exactly would the MessageName of fieldmask, just as an example.

@awalterschulze
Copy link
Member

In other words, will this work?

func (*FieldMask) XXX_MessageName() string {
       return "google.protobuf.FieldMask"
}

@johanbrandhorst
Copy link
Member Author

Yes, that works. It has to be the same as the name that is being registered.

@awalterschulze
Copy link
Member

It seems as though cockroachdb is calling jsonpb.Unmarshal
https://github.com/cockroachdb/cockroach/blob/master/pkg/util/protoutil/jsonpb_marshal.go#L107

While gogomarshal is calling encoding/json Unmarshal
https://github.com/abursavich/gogomarshal/blob/master/jsonpb.go#L106

That is probably a bug.

@awalterschulze
Copy link
Member

But I don't know how that is resulting in GetProperties being called.

@awalterschulze
Copy link
Member

I know that it is stdtime=true that is causing this, since GetProperties is failing on time.Time

@awalterschulze
Copy link
Member

the option to generate XXX_MessageName has now been pushed to master and I have also generated it for all the well known types, including fieldmask.
gogo/protobuf@1ef32a8

@johanbrandhorst
Copy link
Member Author

Wow, that was fast!

@awalterschulze
Copy link
Member

Just like you :)
It was easy, I just wanted to be sure before I did it :)

@johanbrandhorst
Copy link
Member Author

With the FieldMask now officially implementing MessageName() string and the gogo/gateway marshaler, FieldMask requests work out of the box! I will clean up the MR a little and add some more tests, but then we can close this issue.

The next question is if the MessageName() string means we can use grpc/status instead of gogo/status. @awalterschulze any chance we can regenerate gogo/googleapis packages with the MessageName() string?

@awalterschulze
Copy link
Member

Done googleapis now has XXX_MessageName generated for each type.

@glerchundi
Copy link

Great and fast job guys! 👏

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants