2019-05-10 14:36:36 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "bytes"
|
2019-05-10 16:57:33 -05:00
|
|
|
import "os"
|
2019-05-10 19:41:58 -05:00
|
|
|
import "bufio"
|
|
|
|
// import "io"
|
|
|
|
import "io/ioutil"
|
2019-05-10 14:36:36 -05:00
|
|
|
|
2019-05-10 16:57:33 -05:00
|
|
|
import "github.com/golang/protobuf/proto"
|
2019-05-11 14:59:46 -05:00
|
|
|
import pb "git.wit.com/wit/witProtobuf"
|
2019-05-10 14:36:36 -05:00
|
|
|
|
|
|
|
// Main reads the entire address book from a file and prints all the
|
|
|
|
// information inside.
|
|
|
|
func main() {
|
|
|
|
TestWriteEvent()
|
|
|
|
|
2019-05-10 19:41:58 -05:00
|
|
|
in, err := ioutil.ReadFile("/tmp/testing4.protobuf")
|
2019-05-10 14:36:36 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error reading file:", err)
|
|
|
|
}
|
2019-05-10 19:41:58 -05:00
|
|
|
allEvents := &pb.Event{}
|
|
|
|
if err := proto.Unmarshal(in, allEvents); err != nil {
|
|
|
|
log.Fatalln("Failed to parse events:", err)
|
2019-05-10 14:36:36 -05:00
|
|
|
}
|
|
|
|
|
2019-05-10 19:41:58 -05:00
|
|
|
// listPeople(os.Stdout, allEvents)
|
|
|
|
// got := in.String()
|
|
|
|
log.Println(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Event) {
|
|
|
|
buf, err := proto.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("marshaling error: ", err)
|
|
|
|
}
|
|
|
|
tmp, err := myWriter.Write(buf)
|
|
|
|
myWriter.Flush()
|
|
|
|
log.Println("bufio.Write() tmp, err = ", tmp, err)
|
|
|
|
|
|
|
|
e.Id += 1
|
|
|
|
e.Name = "jcarrEvent21"
|
|
|
|
buf, err = proto.Marshal(e)
|
|
|
|
tmp2, err := myWriter.Write(buf)
|
|
|
|
myWriter.Flush()
|
|
|
|
log.Println("bufio.Write() tmp2, err = ", tmp2, err)
|
2019-05-10 14:36:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteEvent() {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
// [START populate_proto]
|
2019-05-10 19:41:58 -05:00
|
|
|
e := &pb.Event{
|
2019-05-10 14:36:36 -05:00
|
|
|
Id: 1234,
|
|
|
|
Name: "jcarrEvent1",
|
|
|
|
Email: "jcarr@wit.com",
|
2019-05-11 14:59:46 -05:00
|
|
|
Results: []*pb.Response{
|
2019-05-10 14:36:36 -05:00
|
|
|
{Name: "555-4321", Type: pb.EventType_ADD},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// [END populate_proto]
|
|
|
|
|
2019-05-10 16:57:33 -05:00
|
|
|
e.Id += 1
|
|
|
|
e.Name = "jcarrEvent2"
|
|
|
|
|
2019-05-10 14:36:36 -05:00
|
|
|
got := buf.String()
|
|
|
|
log.Println(got)
|
|
|
|
|
2019-05-10 19:41:58 -05:00
|
|
|
newfile, _ := os.Create("/tmp/testing4.protobuf")
|
|
|
|
myWriter := bufio.NewWriter(newfile)
|
|
|
|
marshalWriteToFile(myWriter, e)
|
2019-05-10 16:57:33 -05:00
|
|
|
|
|
|
|
marshalUnmarshal()
|
2019-05-10 14:36:36 -05:00
|
|
|
}
|
2019-05-10 16:57:33 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2019-05-10 19:41:58 -05:00
|
|
|
|
2019-05-10 16:57:33 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|