Skip to content
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

Imports for custom/casttype are generated incorrectly under certain conditions #389

Closed
pltr opened this issue Feb 28, 2018 · 7 comments
Closed

Comments

@pltr
Copy link

pltr commented Feb 28, 2018

Consider the following structure
foo/bar/A.proto

syntax = "proto3";
package bar;

message A {
    string f1 = 1;
}

foo/bar/a.go

package bar;

type B struct {
    A
}

foo/moo/C.proto

syntax = "proto3";
package moo;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "foo/bar/A.proto";

message C {
     bar.A f2 = 1 [(gogoproto.customtype) = "foo/bar.B"];
}

Now the generated file (header) for C.proto:

package moo

import proto "github.com/gogo/protobuf/proto"
import fmt "fmt"
import math "math"
import _ "github.com/gogo/protobuf/gogoproto"
import bar "foo/bar"

import foo_bar "foo/bar"

As you can see "foo/bar" gets imported twice in this case, which causes a compilation error:

# foo/moo
moo/C.pb.go:19:8: imported and not used: "foo/bar"
@awalterschulze
Copy link
Member

Very interesting.

But customtype is meant for bytes fields.

https://github.com/gogo/protobuf/blob/master/custom_types.md

#125

syntax = "proto3";
package moo;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "foo/bar/A.proto";

message C {
     bytes f2 = 1 [(gogoproto.customtype) = "foo/bar.B"];
}

@pltr
Copy link
Author

pltr commented Feb 28, 2018

But the very first example on that page seems to be not bytes?
Nonetheless bytes compiles but then the generated unmarshaler always overwrites it and allocates f2 on the heap, not sure if it's possible to change that behavior. I solved it temporarily with declaring an empty message T{} and using it instead, which works and only allocates if .f2 == nil, otherwise it calls f2.Unmarshal() as expected.
But, of course, this way it won't generate properly in other languages.

@awalterschulze
Copy link
Member

Good point. Hmmm.
Sorry I was trying to answer this in a hurry.

I will look at it later again, when I can give it more focus.
Sorry.

@awalterschulze
Copy link
Member

Sorry I have not forgotten about you.
I am just struggling to find some time that I can use to focus.
Sorry.

@awalterschulze
Copy link
Member

Sorry again :(
This is probably one of my worst service moments.
I just came from a 3 day meeting in a Berlin and have some catch up homework to do for a class this Wednesday and then I have friends visiting as well.
I hope to get to this next weekend, in less than a week.

The reason I thought customtype needs to bytes is because this was always the case, until recently when the feature was extended to also allow messages.

Really sorry about this delay.

@awalterschulze
Copy link
Member

Thanks for reporting this.
I have pushed a fix with a test that shows how it is working now.
1ae71f0

@awalterschulze
Copy link
Member

Really sorry for taking so long to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants