diff --git a/example-protobuf/main.go b/example-protobuf/main.go index 88aadb9..d59ed87 100644 --- a/example-protobuf/main.go +++ b/example-protobuf/main.go @@ -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") + } +}