code runs. Writes out marshal'ed entries to a file, but is that anything other than garbage

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-10 17:41:58 -07:00
parent 1c974ed95e
commit c34dc5f401
1 changed files with 32 additions and 84 deletions

View File

@ -1,85 +1,56 @@
package main package main
import "io"
import "io/ioutil"
import "log" import "log"
import "fmt"
import "bytes" import "bytes"
import "os" import "os"
import "bufio"
// import "io"
import "io/ioutil"
import "github.com/golang/protobuf/proto" import "github.com/golang/protobuf/proto"
import pb "git.wit.com/jcarr/witProtobuf" import pb "git.wit.com/jcarr/witProtobuf"
func writeEvent(e *pb.Event) {
log.Println("Event ID:", e.Id)
log.Println(" Name:", e.Name)
if e.Email != "" {
log.Println(" E-mail address:", e.Email)
}
}
func writePerson(w io.Writer, e *pb.Event) {
fmt.Fprintln(w, "Event ID:", e.Id)
fmt.Fprintln(w, " Name:", e.Name)
if e.Email != "" {
fmt.Fprintln(w, " E-mail address:", e.Email)
}
/*
for _, pn := range p.Phones {
switch pn.Type {
case pb.Person_MOBILE:
fmt.Fprint(w, " Mobile phone #: ")
case pb.Person_HOME:
fmt.Fprint(w, " Home phone #: ")
case pb.Person_WORK:
fmt.Fprint(w, " Work phone #: ")
}
fmt.Fprintln(w, pn.Number)
}
*/
}
/*
func listPeople(w io.Writer, book *pb.AddressBook) {
for _, p := range book.People {
writePerson(w, p)
}
}
*/
// Main reads the entire address book from a file and prints all the // Main reads the entire address book from a file and prints all the
// information inside. // information inside.
func main() { func main() {
TestWriteEvent() TestWriteEvent()
/*
if len(os.Args) != 2 {
log.Fatalf("Usage: %s ADDRESS_BOOK_FILE\n", os.Args[0])
}
fname := os.Args[1]
// [START unmarshal_proto] in, err := ioutil.ReadFile("/tmp/testing4.protobuf")
// Read the existing address book.
in, err := ioutil.ReadFile(fname)
if err != nil { if err != nil {
log.Fatalln("Error reading file:", err) log.Fatalln("Error reading file:", err)
} }
book := &pb.AddressBook{} allEvents := &pb.Event{}
if err := proto.Unmarshal(in, book); err != nil { if err := proto.Unmarshal(in, allEvents); err != nil {
log.Fatalln("Failed to parse address book:", err) log.Fatalln("Failed to parse events:", err)
} }
// [END unmarshal_proto]
listPeople(os.Stdout, book) // 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() { func TestWriteEvent() {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
// [START populate_proto] // [START populate_proto]
e := pb.Event{ e := &pb.Event{
Id: 1234, Id: 1234,
Name: "jcarrEvent1", Name: "jcarrEvent1",
Email: "jcarr@wit.com", Email: "jcarr@wit.com",
@ -89,41 +60,17 @@ func TestWriteEvent() {
} }
// [END populate_proto] // [END populate_proto]
writePerson(buf, &e)
e.Id += 1 e.Id += 1
e.Name = "jcarrEvent2" e.Name = "jcarrEvent2"
writePerson(buf, &e)
got := buf.String() got := buf.String()
log.Println(got) log.Println(got)
out := ioutil.WriteFile("/tmp/testing1.protobuf", buf.Bytes(), os.FileMode(0777)) newfile, _ := os.Create("/tmp/testing4.protobuf")
log.Println("ioutil.WriteFile() = out =", out) myWriter := bufio.NewWriter(newfile)
marshalWriteToFile(myWriter, e)
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() marshalUnmarshal()
/*
want := `Person ID: 1234
Name: John Doe
E-mail address: jdoe@example.com
Home phone #: 555-4321
`
if got != want {
t.Errorf("writePerson(%s) =>\n\t%q, want %q", p.String(), got, want)
}
*/
} }
func marshalUnmarshal() { func marshalUnmarshal() {
@ -139,6 +86,7 @@ func marshalUnmarshal() {
if err != nil { if err != nil {
log.Fatal("marshaling error: ", err) log.Fatal("marshaling error: ", err)
} }
newTest := &pb.Event{} newTest := &pb.Event{}
err = proto.Unmarshal(data, newTest) err = proto.Unmarshal(data, newTest)
if err != nil { if err != nil {