A first for me with google protobuf's
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
e476e50d48
commit
5882fa20ac
|
@ -1,2 +1,3 @@
|
|||
server/server
|
||||
client/client
|
||||
example-protobuf/example-protobuf
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
run:
|
||||
go run *.go
|
||||
|
||||
gaper:
|
||||
# 'gaper' is a simple and smart golang tool that just rebuilds every time you change a file
|
||||
# go get -u github.com/maxcnunes/gaper
|
||||
gaper
|
|
@ -0,0 +1,108 @@
|
|||
package main
|
||||
|
||||
import "io"
|
||||
import "log"
|
||||
import "fmt"
|
||||
import "bytes"
|
||||
|
||||
// import "os"
|
||||
// import "io/ioutil"
|
||||
|
||||
// import "github.com/golang/protobuf/proto"
|
||||
|
||||
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
|
||||
// information inside.
|
||||
func main() {
|
||||
TestWriteEvent()
|
||||
/*
|
||||
if len(os.Args) != 2 {
|
||||
log.Fatalf("Usage: %s ADDRESS_BOOK_FILE\n", os.Args[0])
|
||||
}
|
||||
fname := os.Args[1]
|
||||
|
||||
// [START unmarshal_proto]
|
||||
// Read the existing address book.
|
||||
in, err := ioutil.ReadFile(fname)
|
||||
if err != nil {
|
||||
log.Fatalln("Error reading file:", err)
|
||||
}
|
||||
book := &pb.AddressBook{}
|
||||
if err := proto.Unmarshal(in, book); err != nil {
|
||||
log.Fatalln("Failed to parse address book:", err)
|
||||
}
|
||||
// [END unmarshal_proto]
|
||||
|
||||
listPeople(os.Stdout, book)
|
||||
*/
|
||||
}
|
||||
|
||||
func TestWriteEvent() {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
// [START populate_proto]
|
||||
e := pb.Event{
|
||||
Id: 1234,
|
||||
Name: "jcarrEvent1",
|
||||
Email: "jcarr@wit.com",
|
||||
Result: []*pb.Response{
|
||||
{Name: "555-4321", Type: pb.EventType_ADD},
|
||||
},
|
||||
}
|
||||
// [END populate_proto]
|
||||
|
||||
writePerson(buf, &e)
|
||||
|
||||
got := buf.String()
|
||||
log.Println(got)
|
||||
|
||||
/*
|
||||
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)
|
||||
}
|
||||
*/
|
||||
}
|
Loading…
Reference in New Issue