gogo / protobuf

[Deprecated] Protocol Buffers for Go with Gadgets
Other
5.66k stars 808 forks source link

It seems exitsts a bug in String func [ format lost the '&'] #530

Open JungleYe opened 5 years ago

JungleYe commented 5 years ago

here is my proto file:

message MyTst{
    repeated MT SS = 1 [(gogoproto.nullable) = false];
}
message MT{
    optional string dt = 1 [(gogoproto.nullable) = false];
}

here is my test code:

mt := new(jungle.MT)
    mt.Dt = "https://www.google.com/search?q=gogoproto&rlz=1234"
    mytst := new(jungle.MyTst)
    mytst.SS = make([]jungle.MT, 1)
    mytst.SS[0] = *mt
    fmt.Println(mytst)
    //output : &MyTst{SS:[{https://www.google.com/search?q=gogoprotorlz=1234 {} [] 0}],XXX_unrecognized:[],}

the test outputs lost the character '&'

maybe the problem is here

  func (this *MyTst) String() string {
    if this == nil {
        return "nil"
    }
    s := strings.Join([]string{`&MyTst{`,
        `SS:` + strings.Replace(strings.Replace(fmt.Sprintf("%v", this.SS), "MT", "MT", 1), `&`, ``, 1) + `,`,
        `XXX_unrecognized:` + fmt.Sprintf("%v", this.XXX_unrecognized) + `,`,
        `}`,
    }, "")
    return s
    }

the this.SS is slice type, & should not be replaced with `` ?

Bets Regards

jmarais commented 5 years ago

Hi. I think it is correct? With this example protofile:

message Message1{
    repeated Message2 foo = 1 [(gogoproto.nullable) = false];
}
message Message2{
    optional string bar = 1 [(gogoproto.nullable) = false];
}

In your case you have the option:

 [(gogoproto.nullable) = false];

enabled. This means your struct slice is generated without a pointer and looks like this:

type Message1 struct {
    Foo []Message2 `protobuf:"bytes,1,rep,name=foo" json:"foo"`
}

With this as the output:

&Message1{Foo:[{HelloWorld}],}

if you use:

 [(gogoproto.nullable) = true];

Your struct would look like this:

type Message1 struct {
    Foo []*Message2 `protobuf:"bytes,1,rep,name=foo" json:"foo,omitempty"`
}

And your string:

&Message1{Foo:[&Message2{Bar:HelloWorld,}],}

Is this second case the case you are pointing to where you expected another & in your string?

JungleYe commented 5 years ago

@jmarais in my example

mt.Dt = "https://www.google.com/search?q=gogoproto&rlz=1234"

there is a character '&' between 'gogoproto' and 'rlz' the output lost the character '&':

//output : &MyTst{SS:[{https://www.google.com/search?q=**gogoprotorlz**=1234 {} [] 0}],XXX_unrecognized:[],}

jmarais commented 5 years ago

Thanks for clarifying. I misunderstood your initial message.

I can recreate your problem. I will look into it.

jmarais commented 5 years ago

@JungleYe Can you see if current master 382325b fixes your issue?