diff options
author | Feng Xiao <xiaofeng@google.com> | 2015-12-17 11:42:59 -0800 |
---|---|---|
committer | Feng Xiao <xiaofeng@google.com> | 2015-12-17 11:42:59 -0800 |
commit | 9e1777f4b05da6a65e2a60680e5924a802ae5c94 (patch) | |
tree | 752ad968c2c9074b5d5fbd7802a089132094e7fa /examples | |
parent | afbc89a263d57bb2e36ce5f07b979c739bb2e8cd (diff) | |
parent | 7df1d773b7752fc51460fa0f116ed5a74d9d514b (diff) |
Merge pull request #1052 from tswast/master
Add region tags to the Go protobuf examples.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/add_person.go | 5 | ||||
-rw-r--r-- | examples/list_people.go | 46 | ||||
-rw-r--r-- | examples/list_people_test.go | 24 |
3 files changed, 53 insertions, 22 deletions
diff --git a/examples/add_person.go b/examples/add_person.go index 6b2d3d69..4f2e7f74 100644 --- a/examples/add_person.go +++ b/examples/add_person.go @@ -105,7 +105,10 @@ func main() { log.Fatalln("Error reading file:", err) } } + + // [START marshal_proto] book := &pb.AddressBook{} + // [START_EXCLUDE] if err := proto.Unmarshal(in, book); err != nil { log.Fatalln("Failed to parse address book:", err) } @@ -116,6 +119,7 @@ func main() { log.Fatalln("Error with address:", err) } book.People = append(book.People, addr) + // [END_EXCLUDE] // Write the new address book back to disk. out, err := proto.Marshal(book) @@ -125,4 +129,5 @@ func main() { if err := ioutil.WriteFile(fname, out, 0644); err != nil { log.Fatalln("Failed to write address book:", err) } + // [END marshal_proto] } diff --git a/examples/list_people.go b/examples/list_people.go index 48b1fbfa..70bc589e 100644 --- a/examples/list_people.go +++ b/examples/list_people.go @@ -11,25 +11,29 @@ import ( pb "github.com/google/protobuf/examples/tutorial" ) -func listPeople(w io.Writer, book *pb.AddressBook) { - for _, p := range book.People { - fmt.Fprintln(w, "Person ID:", p.Id) - fmt.Fprintln(w, " Name:", p.Name) - if p.Email != "" { - fmt.Fprintln(w, " E-mail address:", p.Email) - } +func writePerson(w io.Writer, p *pb.Person) { + fmt.Fprintln(w, "Person ID:", p.Id) + fmt.Fprintln(w, " Name:", p.Name) + if p.Email != "" { + fmt.Fprintln(w, " E-mail address:", p.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) + 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) } } @@ -41,19 +45,17 @@ func main() { } fname := os.Args[1] + // [START unmarshal_proto] // Read the existing address book. in, err := ioutil.ReadFile(fname) if err != nil { - if os.IsNotExist(err) { - fmt.Printf("%s: File not found. Creating new file.\n", fname) - } else { - log.Fatalln("Error reading file:", err) - } + 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) } diff --git a/examples/list_people_test.go b/examples/list_people_test.go index 721d3555..87d6ad6e 100644 --- a/examples/list_people_test.go +++ b/examples/list_people_test.go @@ -8,6 +8,30 @@ import ( pb "github.com/google/protobuf/examples/tutorial" ) +func TestWritePersonWritesPerson(t *testing.T) { + buf := new(bytes.Buffer) + // [START populate_proto] + p := pb.Person{ + Id: 1234, + Name: "John Doe", + Email: "jdoe@example.com", + Phones: []*pb.Person_PhoneNumber{ + {Number: "555-4321", Type: pb.Person_HOME}, + }, + } + // [END populate_proto] + writePerson(buf, &p) + got := buf.String() + 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 TestListPeopleWritesList(t *testing.T) { buf := new(bytes.Buffer) in := pb.AddressBook{[]*pb.Person{ |