Commit 353b7a99 authored by Jan Tattermusch's avatar Jan Tattermusch

Merge pull request #627 from jtattermusch/addressbook_proto3

Update addressbook.proto and examples code to proto3
parents d119a275 b0e5ba69
...@@ -50,7 +50,7 @@ class AddPerson { ...@@ -50,7 +50,7 @@ class AddPerson {
stdout.println("Unknown phone type. Using default."); stdout.println("Unknown phone type. Using default.");
} }
person.addPhone(phoneNumber); person.addPhones(phoneNumber);
} }
return person.build(); return person.build();
...@@ -80,7 +80,7 @@ class AddPerson { ...@@ -80,7 +80,7 @@ class AddPerson {
} }
// Add an address. // Add an address.
addressBook.addPerson( addressBook.addPeople(
PromptForAddress(new BufferedReader(new InputStreamReader(System.in)), PromptForAddress(new BufferedReader(new InputStreamReader(System.in)),
System.out)); System.out));
......
...@@ -9,14 +9,14 @@ import java.io.PrintStream; ...@@ -9,14 +9,14 @@ import java.io.PrintStream;
class ListPeople { class ListPeople {
// Iterates though all people in the AddressBook and prints info about them. // Iterates though all people in the AddressBook and prints info about them.
static void Print(AddressBook addressBook) { static void Print(AddressBook addressBook) {
for (Person person: addressBook.getPersonList()) { for (Person person: addressBook.getPeopleList()) {
System.out.println("Person ID: " + person.getId()); System.out.println("Person ID: " + person.getId());
System.out.println(" Name: " + person.getName()); System.out.println(" Name: " + person.getName());
if (person.hasEmail()) { if (!person.getEmail().isEmpty()) {
System.out.println(" E-mail address: " + person.getEmail()); System.out.println(" E-mail address: " + person.getEmail());
} }
for (Person.PhoneNumber phoneNumber : person.getPhoneList()) { for (Person.PhoneNumber phoneNumber : person.getPhonesList()) {
switch (phoneNumber.getType()) { switch (phoneNumber.getType()) {
case MOBILE: case MOBILE:
System.out.print(" Mobile phone #: "); System.out.print(" Mobile phone #: ");
......
...@@ -32,7 +32,7 @@ void PromptForAddress(tutorial::Person* person) { ...@@ -32,7 +32,7 @@ void PromptForAddress(tutorial::Person* person) {
break; break;
} }
tutorial::Person::PhoneNumber* phone_number = person->add_phone(); tutorial::Person::PhoneNumber* phone_number = person->add_phones();
phone_number->set_number(number); phone_number->set_number(number);
cout << "Is this a mobile, home, or work phone? "; cout << "Is this a mobile, home, or work phone? ";
...@@ -77,7 +77,7 @@ int main(int argc, char* argv[]) { ...@@ -77,7 +77,7 @@ int main(int argc, char* argv[]) {
} }
// Add an address. // Add an address.
PromptForAddress(address_book.add_person()); PromptForAddress(address_book.add_people());
{ {
// Write the new address book back to disk. // Write the new address book back to disk.
......
...@@ -19,7 +19,7 @@ def PromptForAddress(person): ...@@ -19,7 +19,7 @@ def PromptForAddress(person):
if number == "": if number == "":
break break
phone_number = person.phone.add() phone_number = person.phones.add()
phone_number.number = number phone_number.number = number
type = raw_input("Is this a mobile, home, or work phone? ") type = raw_input("Is this a mobile, home, or work phone? ")
...@@ -50,7 +50,7 @@ except IOError: ...@@ -50,7 +50,7 @@ except IOError:
print sys.argv[1] + ": File not found. Creating a new file." print sys.argv[1] + ": File not found. Creating a new file."
# Add an address. # Add an address.
PromptForAddress(address_book.person.add()) PromptForAddress(address_book.people.add())
# Write the new address book back to disk. # Write the new address book back to disk.
f = open(sys.argv[1], "wb") f = open(sys.argv[1], "wb")
......
// See README.txt for information and build instructions. // See README.txt for information and build instructions.
syntax = "proto2"; syntax = "proto3";
package tutorial; package tutorial;
option java_package = "com.example.tutorial"; option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos"; option java_outer_classname = "AddressBookProtos";
option csharp_namespace = "Google.ProtocolBuffers.Examples.AddressBook"; option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
message Person { message Person {
required string name = 1; string name = 1;
required int32 id = 2; // Unique ID number for this person. int32 id = 2; // Unique ID number for this person.
optional string email = 3; string email = 3;
enum PhoneType { enum PhoneType {
MOBILE = 0; MOBILE = 0;
...@@ -20,14 +20,14 @@ message Person { ...@@ -20,14 +20,14 @@ message Person {
} }
message PhoneNumber { message PhoneNumber {
required string number = 1; string number = 1;
optional PhoneType type = 2 [default = HOME]; PhoneType type = 2;
} }
repeated PhoneNumber phone = 4; repeated PhoneNumber phones = 4;
} }
// Our address book file is just one of these. // Our address book file is just one of these.
message AddressBook { message AddressBook {
repeated Person person = 1; repeated Person people = 1;
} }
...@@ -8,17 +8,17 @@ using namespace std; ...@@ -8,17 +8,17 @@ using namespace std;
// Iterates though all people in the AddressBook and prints info about them. // Iterates though all people in the AddressBook and prints info about them.
void ListPeople(const tutorial::AddressBook& address_book) { void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = 0; i < address_book.person_size(); i++) { for (int i = 0; i < address_book.people_size(); i++) {
const tutorial::Person& person = address_book.person(i); const tutorial::Person& person = address_book.people(i);
cout << "Person ID: " << person.id() << endl; cout << "Person ID: " << person.id() << endl;
cout << " Name: " << person.name() << endl; cout << " Name: " << person.name() << endl;
if (person.has_email()) { if (person.email() != "") {
cout << " E-mail address: " << person.email() << endl; cout << " E-mail address: " << person.email() << endl;
} }
for (int j = 0; j < person.phone_size(); j++) { for (int j = 0; j < person.phones_size(); j++) {
const tutorial::Person::PhoneNumber& phone_number = person.phone(j); const tutorial::Person::PhoneNumber& phone_number = person.phones(j);
switch (phone_number.type()) { switch (phone_number.type()) {
case tutorial::Person::MOBILE: case tutorial::Person::MOBILE:
......
...@@ -7,13 +7,13 @@ import sys ...@@ -7,13 +7,13 @@ import sys
# Iterates though all people in the AddressBook and prints info about them. # Iterates though all people in the AddressBook and prints info about them.
def ListPeople(address_book): def ListPeople(address_book):
for person in address_book.person: for person in address_book.people:
print "Person ID:", person.id print "Person ID:", person.id
print " Name:", person.name print " Name:", person.name
if person.HasField('email'): if person.email != "":
print " E-mail address:", person.email print " E-mail address:", person.email
for phone_number in person.phone: for phone_number in person.phones:
if phone_number.type == addressbook_pb2.Person.MOBILE: if phone_number.type == addressbook_pb2.Person.MOBILE:
print " Mobile phone #:", print " Mobile phone #:",
elif phone_number.type == addressbook_pb2.Person.HOME: elif phone_number.type == addressbook_pb2.Person.HOME:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment