Commit eb70bd0b authored by Jon Skeet's avatar Jon Skeet

Update the AddressBook tutorial to reflect the mutable design.

parent 96ddf01a
...@@ -42,52 +42,14 @@ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers/DescriptorProtos \ ...@@ -42,52 +42,14 @@ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers/DescriptorProtos \
src/google/protobuf/descriptor_proto_file.proto src/google/protobuf/descriptor_proto_file.proto
rm src/google/protobuf/descriptor_proto_file.proto rm src/google/protobuf/descriptor_proto_file.proto
# ProtocolBuffers.Test protos
$PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers.Test/TestProtos \ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers.Test/TestProtos \
src/google/protobuf/unittest.proto \ src/google/protobuf/unittest_proto3.proto \
src/google/protobuf/unittest_custom_options.proto \ src/google/protobuf/unittest_import_proto3.proto \
src/google/protobuf/unittest_drop_unknown_fields.proto \ src/google/protobuf/unittest_import_public_proto3.proto
src/google/protobuf/unittest_enormous_descriptor.proto \
src/google/protobuf/unittest_import.proto \
src/google/protobuf/unittest_import_public.proto \
src/google/protobuf/unittest_mset.proto \
src/google/protobuf/unittest_optimize_for.proto \
src/google/protobuf/unittest_no_field_presence.proto \
src/google/protobuf/unknown_enum_test.proto
$PROTOC -Icsharp/protos/extest --csharp_out=csharp/src/ProtocolBuffers.Test/TestProtos \ $PROTOC -Icsharp/protos/extest --csharp_out=csharp/src/ProtocolBuffers.Test/TestProtos \
csharp/protos/extest/unittest_extras_xmltest.proto \
csharp/protos/extest/unittest_issues.proto csharp/protos/extest/unittest_issues.proto
$PROTOC -Ibenchmarks --csharp_out=csharp/src/ProtocolBuffers.Test/TestProtos \
benchmarks/google_size.proto \
benchmarks/google_speed.proto
# ProtocolBuffersLite.Test protos
$PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffersLite.Test/TestProtos \
src/google/protobuf/unittest.proto \
src/google/protobuf/unittest_import.proto \
src/google/protobuf/unittest_import_lite.proto \
src/google/protobuf/unittest_import_public.proto \
src/google/protobuf/unittest_import_public_lite.proto \
src/google/protobuf/unittest_lite.proto \
src/google/protobuf/unittest_lite_imports_nonlite.proto
$PROTOC -Icsharp/protos/extest --csharp_out=csharp/src/ProtocolBuffersLite.Test/TestProtos \
csharp/protos/extest/unittest_extras_full.proto \
csharp/protos/extest/unittest_extras_lite.proto
# TODO(jonskeet): Remove fixup; see issue #307
sed -i -e 's/RepeatedFieldsGenerator\.Group/RepeatedFieldsGenerator.Types.Group/g' \
csharp/src/ProtocolBuffers.Test/TestProtos/Unittest.cs \
csharp/src/ProtocolBuffersLite.Test/TestProtos/Unittest.cs \
csharp/src/ProtocolBuffersLite.Test/TestProtos/UnittestLite.cs
# TODO(jonskeet): Remove fixup
sed -i -e 's/DescriptorProtos\.Descriptor\./DescriptorProtos.DescriptorProtoFile./g' \
csharp/src/ProtocolBuffers.Test/TestProtos/UnittestCustomOptions.cs
# AddressBook sample protos # AddressBook sample protos
$PROTOC -Iexamples --csharp_out=csharp/src/AddressBook \ $PROTOC -Iexamples --csharp_out=csharp/src/AddressBook \
examples/addressbook.proto examples/addressbook.proto
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
using System; using System;
using System.IO; using System.IO;
using Google.Protobuf;
namespace Google.ProtocolBuffers.Examples.AddressBook namespace Google.ProtocolBuffers.Examples.AddressBook
{ {
...@@ -46,7 +47,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook ...@@ -46,7 +47,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
/// </summary> /// </summary>
private static Person PromptForAddress(TextReader input, TextWriter output) private static Person PromptForAddress(TextReader input, TextWriter output)
{ {
Person.Builder person = Person.CreateBuilder(); Person person = new Person();
output.Write("Enter person ID: "); output.Write("Enter person ID: ");
person.Id = int.Parse(input.ReadLine()); person.Id = int.Parse(input.ReadLine());
...@@ -70,8 +71,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook ...@@ -70,8 +71,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
break; break;
} }
Person.Types.PhoneNumber.Builder phoneNumber = Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber { Number = number };
Person.Types.PhoneNumber.CreateBuilder().SetNumber(number);
output.Write("Is this a mobile, home, or work phone? "); output.Write("Is this a mobile, home, or work phone? ");
String type = input.ReadLine(); String type = input.ReadLine();
...@@ -91,9 +91,9 @@ namespace Google.ProtocolBuffers.Examples.AddressBook ...@@ -91,9 +91,9 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
break; break;
} }
person.AddPhone(phoneNumber); person.Phone.Add(phoneNumber);
} }
return person.Build(); return person;
} }
/// <summary> /// <summary>
...@@ -108,27 +108,28 @@ namespace Google.ProtocolBuffers.Examples.AddressBook ...@@ -108,27 +108,28 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
return -1; return -1;
} }
AddressBook.Builder addressBook = AddressBook.CreateBuilder(); AddressBook addressBook;
if (File.Exists(args[0])) if (File.Exists(args[0]))
{ {
using (Stream file = File.OpenRead(args[0])) using (Stream file = File.OpenRead(args[0]))
{ {
addressBook.MergeFrom(file); addressBook = AddressBook.Parser.ParseFrom(file);
} }
} }
else else
{ {
Console.WriteLine("{0}: File not found. Creating a new file.", args[0]); Console.WriteLine("{0}: File not found. Creating a new file.", args[0]);
addressBook = new AddressBook();
} }
// Add an address. // Add an address.
addressBook.AddPerson(PromptForAddress(Console.In, Console.Out)); addressBook.Person.Add(PromptForAddress(Console.In, Console.Out));
// Write the new address book back to disk. // Write the new address book back to disk.
using (Stream output = File.OpenWrite(args[0])) using (Stream output = File.OpenWrite(args[0]))
{ {
addressBook.Build().WriteTo(output); addressBook.WriteTo(output);
} }
return 0; return 0;
} }
......
This diff is collapsed.
...@@ -46,16 +46,16 @@ namespace Google.ProtocolBuffers.Examples.AddressBook ...@@ -46,16 +46,16 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
/// </summary> /// </summary>
private static void Print(AddressBook addressBook) private static void Print(AddressBook addressBook)
{ {
foreach (Person person in addressBook.PersonList) foreach (Person person in addressBook.Person)
{ {
Console.WriteLine("Person ID: {0}", person.Id); Console.WriteLine("Person ID: {0}", person.Id);
Console.WriteLine(" Name: {0}", person.Name); Console.WriteLine(" Name: {0}", person.Name);
if (person.HasEmail) if (person.Email != "")
{ {
Console.WriteLine(" E-mail address: {0}", person.Email); Console.WriteLine(" E-mail address: {0}", person.Email);
} }
foreach (Person.Types.PhoneNumber phoneNumber in person.PhoneList) foreach (Person.Types.PhoneNumber phoneNumber in person.Phone)
{ {
switch (phoneNumber.Type) switch (phoneNumber.Type)
{ {
...@@ -94,7 +94,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook ...@@ -94,7 +94,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
// Read the existing address book. // Read the existing address book.
using (Stream stream = File.OpenRead(args[0])) using (Stream stream = File.OpenRead(args[0]))
{ {
AddressBook addressBook = AddressBook.ParseFrom(stream); AddressBook addressBook = AddressBook.Parser.ParseFrom(stream);
Print(addressBook); Print(addressBook);
} }
return 0; return 0;
......
using System; using Google.Protobuf;
using System;
using System.IO; using System.IO;
namespace Google.ProtocolBuffers.Examples.AddressBook namespace Google.ProtocolBuffers.Examples.AddressBook
...@@ -8,37 +9,31 @@ namespace Google.ProtocolBuffers.Examples.AddressBook ...@@ -8,37 +9,31 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
private static void Main() private static void Main()
{ {
byte[] bytes; byte[] bytes;
//Create a builder to start building a message // Create a new person
Person.Builder newContact = Person.CreateBuilder(); Person person = new Person
//Set the primitive properties {
newContact.SetId(1) Id = 1,
.SetName("Foo") Name = "Foo",
.SetEmail("foo@bar"); Email = "foo@bar",
//Now add an item to a list (repeating) field Phone = { new Person.Types.PhoneNumber { Number = "555-1212" } }
newContact.AddPhone( };
//Create the child message inline
Person.Types.PhoneNumber.CreateBuilder().SetNumber("555-1212").Build()
);
//Now build the final message:
Person person = newContact.Build();
//The builder is no longer valid (at least not now, scheduled for 2.4):
newContact = null;
using (MemoryStream stream = new MemoryStream()) using (MemoryStream stream = new MemoryStream())
{ {
//Save the person to a stream // Save the person to a stream
person.WriteTo(stream); person.WriteTo(stream);
bytes = stream.ToArray(); bytes = stream.ToArray();
} }
//Create another builder, merge the byte[], and build the message: Person copy = Person.Parser.ParseFrom(bytes);
Person copy = Person.CreateBuilder().MergeFrom(bytes).Build();
//A more streamlined approach might look like this: // A more streamlined approach might look like this:
bytes = AddressBook.CreateBuilder().AddPerson(copy).Build().ToByteArray(); bytes = copy.ToByteArray();
//And read the address book back again // And read the address book back again
AddressBook restored = AddressBook.CreateBuilder().MergeFrom(bytes).Build(); AddressBook restored = AddressBook.Parser.ParseFrom(bytes);
//The message performs a deep-comparison on equality: // The message performs a deep-comparison on equality:
if (restored.PersonCount != 1 || !person.Equals(restored.PersonList[0])) if (restored.Person.Count != 1 || !person.Equals(restored.Person[0]))
{
throw new ApplicationException("There is a bad person in here!"); throw new ApplicationException("There is a bad person in here!");
}
} }
} }
} }
\ No newline at end of file
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