From e72cb8a0c1e05865e60ce8d890cea46fd162e838 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 11 May 2019 11:53:43 -0700 Subject: [PATCH] add an example that makes some sample data and writes it out to a file Signed-off-by: Jeff Carr --- .gitignore | 1 + example-protobuf/Makefile | 2 + example-protobuf/main.go | 99 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 example-protobuf/Makefile create mode 100644 example-protobuf/main.go diff --git a/.gitignore b/.gitignore index e69de29..0e516bb 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +example-protobuf/example-protobuf diff --git a/example-protobuf/Makefile b/example-protobuf/Makefile new file mode 100644 index 0000000..e07773d --- /dev/null +++ b/example-protobuf/Makefile @@ -0,0 +1,2 @@ +run: + go run *.go diff --git a/example-protobuf/main.go b/example-protobuf/main.go new file mode 100644 index 0000000..df751a2 --- /dev/null +++ b/example-protobuf/main.go @@ -0,0 +1,99 @@ +package main + +import "log" +import "bytes" +import "os" +import "bufio" +// import "io" +import "io/ioutil" + +import "github.com/golang/protobuf/proto" +import pb "git.wit.com/wit/witProtobuf" + +// +// writes out several files in /tmp/ +// + +func main() { + TestWriteEvent() + + in, err := ioutil.ReadFile("/tmp/testing4.protobuf") + if err != nil { + log.Fatalln("Error reading file:", err) + } + allEvents := &pb.Event{} + if err := proto.Unmarshal(in, allEvents); err != nil { + log.Fatalln("Failed to parse events:", err) + } + + // 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) +} + +func TestWriteEvent() { + buf := new(bytes.Buffer) + + // [START populate_proto] + e := &pb.Event{ + Id: 1234, + Name: "jcarrEvent1", + Email: "jcarr@wit.com", + Results: []*pb.Event_Response{ + {Name: "foobar", Type: pb.Event_ADD}, + }, + } + // [END populate_proto] + + e.Id += 1 + e.Name = "jcarrEvent2" + + got := buf.String() + log.Println(got) + + newfile, _ := os.Create("/tmp/testing4.protobuf") + myWriter := bufio.NewWriter(newfile) + marshalWriteToFile(myWriter, e) + + marshalUnmarshal() +} + +func marshalUnmarshal() { + test := &pb.Event{ + Id: 1234, + Name: "jcarrEvent1", + Email: "jcarr@wit.com", + Results: []*pb.Event_Response{ + {Name: "555-4321", Type: pb.Event_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") + } +}