Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
eb70bd0b
Commit
eb70bd0b
authored
Jun 12, 2015
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update the AddressBook tutorial to reflect the mutable design.
parent
96ddf01a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
79 deletions
+38
-79
generate_protos.sh
csharp/generate_protos.sh
+3
-41
AddPerson.cs
csharp/src/AddressBook/AddPerson.cs
+10
-9
Addressbook.cs
csharp/src/AddressBook/Addressbook.cs
+0
-0
ListPeople.cs
csharp/src/AddressBook/ListPeople.cs
+4
-4
SampleUsage.cs
csharp/src/AddressBook/SampleUsage.cs
+21
-25
No files found.
csharp/generate_protos.sh
View file @
eb70bd0b
...
...
@@ -42,52 +42,14 @@ $PROTOC -Isrc --csharp_out=csharp/src/ProtocolBuffers/DescriptorProtos \
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
\
src/google/protobuf/unittest.proto
\
src/google/protobuf/unittest_custom_options.proto
\
src/google/protobuf/unittest_drop_unknown_fields.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
src/google/protobuf/unittest_proto3.proto
\
src/google/protobuf/unittest_import_proto3.proto
\
src/google/protobuf/unittest_import_public_proto3.proto
$PROTOC
-Icsharp
/protos/extest
--csharp_out
=
csharp/src/ProtocolBuffers.Test/TestProtos
\
csharp/protos/extest/unittest_extras_xmltest.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
$PROTOC
-Iexamples
--csharp_out
=
csharp/src/AddressBook
\
examples/addressbook.proto
csharp/src/AddressBook/AddPerson.cs
View file @
eb70bd0b
...
...
@@ -36,6 +36,7 @@
using
System
;
using
System.IO
;
using
Google.Protobuf
;
namespace
Google.ProtocolBuffers.Examples.AddressBook
{
...
...
@@ -46,7 +47,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
/// </summary>
private
static
Person
PromptForAddress
(
TextReader
input
,
TextWriter
output
)
{
Person
.
Builder
person
=
Person
.
CreateBuilder
();
Person
person
=
new
Person
();
output
.
Write
(
"Enter person ID: "
);
person
.
Id
=
int
.
Parse
(
input
.
ReadLine
());
...
...
@@ -70,8 +71,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
break
;
}
Person
.
Types
.
PhoneNumber
.
Builder
phoneNumber
=
Person
.
Types
.
PhoneNumber
.
CreateBuilder
().
SetNumber
(
number
);
Person
.
Types
.
PhoneNumber
phoneNumber
=
new
Person
.
Types
.
PhoneNumber
{
Number
=
number
};
output
.
Write
(
"Is this a mobile, home, or work phone? "
);
String
type
=
input
.
ReadLine
();
...
...
@@ -91,9 +91,9 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
break
;
}
person
.
AddPhone
(
phoneNumber
);
person
.
Phone
.
Add
(
phoneNumber
);
}
return
person
.
Build
()
;
return
person
;
}
/// <summary>
...
...
@@ -108,27 +108,28 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
return
-
1
;
}
AddressBook
.
Builder
addressBook
=
AddressBook
.
CreateBuilder
()
;
AddressBook
addressBook
;
if
(
File
.
Exists
(
args
[
0
]))
{
using
(
Stream
file
=
File
.
OpenRead
(
args
[
0
]))
{
addressBook
.
Merg
eFrom
(
file
);
addressBook
=
AddressBook
.
Parser
.
Pars
eFrom
(
file
);
}
}
else
{
Console
.
WriteLine
(
"{0}: File not found. Creating a new file."
,
args
[
0
]);
addressBook
=
new
AddressBook
();
}
// 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.
using
(
Stream
output
=
File
.
OpenWrite
(
args
[
0
]))
{
addressBook
.
Build
().
WriteTo
(
output
);
addressBook
.
WriteTo
(
output
);
}
return
0
;
}
...
...
csharp/src/AddressBook/Addressbook.cs
View file @
eb70bd0b
This diff is collapsed.
Click to expand it.
csharp/src/AddressBook/ListPeople.cs
View file @
eb70bd0b
...
...
@@ -46,16 +46,16 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
/// </summary>
private
static
void
Print
(
AddressBook
addressBook
)
{
foreach
(
Person
person
in
addressBook
.
Person
List
)
foreach
(
Person
person
in
addressBook
.
Person
)
{
Console
.
WriteLine
(
"Person ID: {0}"
,
person
.
Id
);
Console
.
WriteLine
(
" Name: {0}"
,
person
.
Name
);
if
(
person
.
HasEmail
)
if
(
person
.
Email
!=
""
)
{
Console
.
WriteLine
(
" E-mail address: {0}"
,
person
.
Email
);
}
foreach
(
Person
.
Types
.
PhoneNumber
phoneNumber
in
person
.
Phone
List
)
foreach
(
Person
.
Types
.
PhoneNumber
phoneNumber
in
person
.
Phone
)
{
switch
(
phoneNumber
.
Type
)
{
...
...
@@ -94,7 +94,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
// Read the existing address book.
using
(
Stream
stream
=
File
.
OpenRead
(
args
[
0
]))
{
AddressBook
addressBook
=
AddressBook
.
ParseFrom
(
stream
);
AddressBook
addressBook
=
AddressBook
.
Parse
r
.
Parse
From
(
stream
);
Print
(
addressBook
);
}
return
0
;
...
...
csharp/src/AddressBook/SampleUsage.cs
View file @
eb70bd0b
using
System
;
using
Google.Protobuf
;
using
System
;
using
System.IO
;
namespace
Google.ProtocolBuffers.Examples.AddressBook
...
...
@@ -8,37 +9,31 @@ namespace Google.ProtocolBuffers.Examples.AddressBook
private
static
void
Main
()
{
byte
[]
bytes
;
//Create a builder to start building a message
Person
.
Builder
newContact
=
Person
.
CreateBuilder
();
//Set the primitive properties
newContact
.
SetId
(
1
)
.
SetName
(
"Foo"
)
.
SetEmail
(
"foo@bar"
);
//Now add an item to a list (repeating) field
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
;
// Create a new person
Person
person
=
new
Person
{
Id
=
1
,
Name
=
"Foo"
,
Email
=
"foo@bar"
,
Phone
=
{
new
Person
.
Types
.
PhoneNumber
{
Number
=
"555-1212"
}
}
};
using
(
MemoryStream
stream
=
new
MemoryStream
())
{
//Save the person to a stream
//
Save the person to a stream
person
.
WriteTo
(
stream
);
bytes
=
stream
.
ToArray
();
}
//Create another builder, merge the byte[], and build the message:
Person
copy
=
Person
.
CreateBuilder
().
MergeFrom
(
bytes
).
Build
();
Person
copy
=
Person
.
Parser
.
ParseFrom
(
bytes
);
//A more streamlined approach might look like this:
bytes
=
AddressBook
.
CreateBuilder
().
AddPerson
(
copy
).
Build
().
ToByteArray
();
//And read the address book back again
AddressBook
restored
=
AddressBook
.
CreateBuilder
().
MergeFrom
(
bytes
).
Build
();
//The message performs a deep-comparison on equality:
if
(
restored
.
PersonCount
!=
1
||
!
person
.
Equals
(
restored
.
PersonList
[
0
]))
// A more streamlined approach might look like this:
bytes
=
copy
.
ToByteArray
();
// And read the address book back again
AddressBook
restored
=
AddressBook
.
Parser
.
ParseFrom
(
bytes
);
// The message performs a deep-comparison on equality:
if
(
restored
.
Person
.
Count
!=
1
||
!
person
.
Equals
(
restored
.
Person
[
0
]))
{
throw
new
ApplicationException
(
"There is a bad person in here!"
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment