1

Question as per title. Tried to compile in 2 ways gogoproto and golangprotobuf.

Wrote tests for both, and both won't marshal.

msg.proto

syntax = "proto3";

import "google/protobuf/timestamp.proto";

package msg;

message Message {
    string Name = 1;
    google.protobuf.Timestamp TimeStamp = 2;
}

demo_test.go

package msg

import (
    "testing"
    "time"

    // gogo "github.com/gogo/protobuf/proto"
    "github.com/golang/protobuf/proto"
    "github.com/golang/protobuf/ptypes/timestamp"
)

var msg = Message{
    Name:      "demo",
    TimeStamp: &timestamp.Timestamp{Seconds: int64(time.Now().Second())},
}

//func TestGogoMessage_Marshal(t *testing.T) {
//  myBytes, err := gogo.Marshal(&msg)
//  if err != nil {
//      t.Fail()
//  }

//  _ = myBytes
//}

func TestProtoMessage_Marshal(t *testing.T) {
    myBytes, err := proto.Marshal(&msg)
    if err != nil {
        t.Fail()
    }

    _ = myBytes
}

compiled with:

protoc --gofast_out=. ./demo/msg.proto works, but running the test:

# github.com/.../demo
package github.com/.../demo (test)
    imports github.com/gogo/protobuf/proto: cannot find package "." in:
    /Users/.../vendor/github.com/gogo/protobuf/proto

protoc --go_out=. ./demo/msg.proto works, but running the test:

# github.com/.../demo [github.com/.../demo.test]
./msg.pb.go:127:28: m.TimeStamp.MarshalToSizedBuffer undefined (type *timestamp.Timestamp has no field or method MarshalToSizedBuffer)
./msg.pb.go:169:18: m.TimeStamp.Size undefined (type *timestamp.Timestamp has no field or method Size)
./msg.pb.go:277:25: m.TimeStamp.Unmarshal undefined (type *timestamp.Timestamp has no field or method Unmarshal)
Gravy
  • 12,264
  • 26
  • 124
  • 193

2 Answers2

0

Both commands work fine for me, so, probably problem is in in your environment.

Regarding undefine error, it looks like you're using Timestamp struct from github.com/golang/protobuf/ptypes/timestamp, which has different interface than Timestamp from https://github.com/gogo/protobuf/blob/master/types/timestamp.pb.go. So if you generate msg.pb.go with protoc --gofast_out=. ./demo/msg.proto you will be getting this error.

Grigoriy Mikhalkin
  • 5,035
  • 1
  • 18
  • 36
  • protoc compiles fine, the problem is when I try to run the test(s) – Gravy Apr 12 '20 at 13:20
  • @Gravy Yes, that's what i meant. `protoc --gofast_out=. ./demo/msg.proto` generates `msg.pb.go` that expects `Timestamp` from gogo, but you use `Timestamp` from golang/protobuf – Grigoriy Mikhalkin Apr 12 '20 at 13:35
0

add more options:

protoc -I xxx --gogofaster_out=plugins=grpc,paths=source_relative,Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types:${PB_DIR} xxx.proto
Color
  • 875
  • 9
  • 11