successfully marshal a protobuf witProtobuf.Event and write it to a file

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-10 14:57:33 -07:00
parent 5882fa20ac
commit 1c974ed95e
1 changed files with 45 additions and 4 deletions

View File

@ -1,14 +1,13 @@
package main
import "io"
import "io/ioutil"
import "log"
import "fmt"
import "bytes"
import "os"
// import "os"
// import "io/ioutil"
// import "github.com/golang/protobuf/proto"
import "github.com/golang/protobuf/proto"
import pb "git.wit.com/jcarr/witProtobuf"
@ -92,9 +91,29 @@ func TestWriteEvent() {
writePerson(buf, &e)
e.Id += 1
e.Name = "jcarrEvent2"
writePerson(buf, &e)
got := buf.String()
log.Println(got)
out := ioutil.WriteFile("/tmp/testing1.protobuf", buf.Bytes(), os.FileMode(0777))
log.Println("ioutil.WriteFile() = out =", out)
tmp, err := proto.Marshal(&e)
if err != nil {
log.Fatal("marshaling error: ", err)
}
out = ioutil.WriteFile("/tmp/testing2.protobuf", tmp, os.FileMode(0777))
/*
out = ioutil.WriteFile("/tmp/testing2.protobuf", e, os.FileMode(0777))
log.Println("ioutil.WriteFile() = out =", out)
*/
marshalUnmarshal()
/*
want := `Person ID: 1234
Name: John Doe
@ -106,3 +125,25 @@ func TestWriteEvent() {
}
*/
}
func marshalUnmarshal() {
test := &pb.Event{
Id: 1234,
Name: "jcarrEvent1",
Email: "jcarr@wit.com",
Result: []*pb.Response{
{Name: "555-4321", Type: pb.EventType_ADD},
},
}
data, err := proto.Marshal(test)
if err != nil {
log.Fatal("marshaling error: ", err)
}
newTest := &pb.Event{}
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Fatal("unmarshaling error: ", err)
} else {
log.Println("proto.Marshal() and proto.Unmarshal() worked")
}
}