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
969397b7
Commit
969397b7
authored
Oct 15, 2018
by
Sarah Zakarias
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Dart example.
parent
4426cb57
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
156 additions
and
0 deletions
+156
-0
Makefile
examples/Makefile
+13
-0
README.md
examples/README.md
+19
-0
add_person.dart
examples/add_person.dart
+72
-0
list_people.dart
examples/list_people.dart
+47
-0
pubspec.yaml
examples/pubspec.yaml
+5
-0
No files found.
examples/Makefile
View file @
969397b7
...
...
@@ -5,6 +5,7 @@
all
:
cpp java python
cpp
:
add_person_cpp list_people_cpp
dart
:
add_person_dart list_people_dart
go
:
add_person_go list_people_go
gotest
:
add_person_gotest list_people_gotest
java
:
add_person_java list_people_java
...
...
@@ -16,6 +17,8 @@ clean:
rm
-f
protoc_middleman addressbook.pb.cc addressbook.pb.h addressbook_pb2.py com/example/tutorial/AddressBookProtos.java
rm
-f
*
.pyc
rm
-f
protoc_middleman_go tutorial/
*
.pb.go add_person_go list_people_go
rm
-f
protoc_middleman_dart dart_tutorial/
*
.pb
*
.dart
rmdir
dart_tutorial 2>/dev/null
||
true
rmdir
tutorial 2>/dev/null
||
true
rmdir
com/example/tutorial 2>/dev/null
||
true
rmdir
com/example 2>/dev/null
||
true
...
...
@@ -30,6 +33,12 @@ protoc_middleman_go: addressbook.proto
protoc
$$
PROTO_PATH
--go_out
=
tutorial addressbook.proto
@
touch
protoc_middleman_go
protoc_middleman_dart
:
addressbook.proto
mkdir
-p
dart_tutorial
# make directory for the dart package
protoc
$$
PROTO_PATH
--dart_out
=
dart_tutorial addressbook.proto
pub get
@
touch
protoc_middleman_dart
add_person_cpp
:
add_person.cc protoc_middleman
pkg-config
--cflags
protobuf
# fails if protobuf is not installed
c++ add_person.cc addressbook.pb.cc
-o
add_person_cpp
`
pkg-config
--cflags
--libs
protobuf
`
...
...
@@ -38,6 +47,10 @@ list_people_cpp: list_people.cc protoc_middleman
pkg-config
--cflags
protobuf
# fails if protobuf is not installed
c++ list_people.cc addressbook.pb.cc
-o
list_people_cpp
`
pkg-config
--cflags
--libs
protobuf
`
add_person_dart
:
add_person.dart protoc_middleman_dart
list_people_dart
:
list_people.dart protoc_middleman_dart
add_person_go
:
add_person.go protoc_middleman_go
go build
-o
add_person_go add_person.go
...
...
examples/README.md
View file @
969397b7
...
...
@@ -122,3 +122,22 @@ is created if it does not exist. To view the data, run:
Observe that the C++, Python, and Java examples in this directory run in a
similar way and can view/modify files created by the Go example and vice
versa.
### Dart
First, follow the instructions in
[
../README.md
](
../README.md
)
to install the Protocol Buffer Compiler (protoc).
Then, install the Dart Protocol Buffer plugin as described
[
here
](
https://github.com/dart-lang/dart-protoc-plugin#how-to-build-and-use
)
.
Note, the executable
`bin/protoc-gen-dart`
must be in your
`PATH`
for
`protoc`
to find it.
Build the Dart samples in this directory with
`make dart`
.
To run the examples:
$ dart add_person.dart addessbook.data
$ dart list_people.dart addressbook.data
The two programs take a protocol buffer encoded file as their parameter.
The first can be used to add a person to the file. The file is created
if it does not exist. The second displays the data in the file.
examples/add_person.dart
0 → 100644
View file @
969397b7
import
'dart:io'
;
import
'dart_tutorial/addressbook.pb.dart'
;
// This function fills in a Person message based on user input.
Person
promtForAddress
(
)
{
Person
person
=
Person
();
print
(
'Enter person ID: '
);
String
input
=
stdin
.
readLineSync
();
person
.
id
=
int
.
parse
(
input
);
print
(
'Enter name'
);
person
.
name
=
stdin
.
readLineSync
();
print
(
'Enter email address (blank for none) : '
);
String
email
=
stdin
.
readLineSync
();
if
(
email
.
isNotEmpty
)
{
person
.
email
=
email
;
}
while
(
true
)
{
print
(
'Enter a phone number (or leave blank to finish): '
);
String
number
=
stdin
.
readLineSync
();
if
(
number
.
isEmpty
)
break
;
Person_PhoneNumber
phoneNumber
=
Person_PhoneNumber
();
phoneNumber
.
number
=
number
;
print
(
'Is this a mobile, home, or work phone? '
);
String
type
=
stdin
.
readLineSync
();
switch
(
type
)
{
case
'mobile'
:
phoneNumber
.
type
=
Person_PhoneType
.
MOBILE
;
break
;
case
'home'
:
phoneNumber
.
type
=
Person_PhoneType
.
HOME
;
break
;
case
'work'
:
phoneNumber
.
type
=
Person_PhoneType
.
WORK
;
break
;
default
:
print
(
'Unknown phone type. Using default.'
);
}
person
.
phones
.
add
(
phoneNumber
);
}
return
person
;
}
// Reads the entire address book from a file, adds one person based
// on user input, then writes it back out to the same file.
main
(
List
<
String
>
arguments
)
{
if
(
arguments
.
length
!=
1
)
{
print
(
'Usage: add_person ADDRESS_BOOK_FILE'
);
exit
(-
1
);
}
File
file
=
File
(
arguments
.
first
);
AddressBook
addressBook
;
if
(!
file
.
existsSync
())
{
print
(
'File not found. Creating new file.'
);
addressBook
=
AddressBook
();
}
else
{
addressBook
=
AddressBook
.
fromBuffer
(
file
.
readAsBytesSync
());
}
addressBook
.
people
.
add
(
promtForAddress
());
file
.
writeAsBytes
(
addressBook
.
writeToBuffer
());
}
\ No newline at end of file
examples/list_people.dart
0 → 100644
View file @
969397b7
import
'dart:io'
;
import
'dart_tutorial/addressbook.pb.dart'
;
import
'dart_tutorial/addressbook.pbenum.dart'
;
// Iterates though all people in the AddressBook and prints info about them.
void
printAddressBook
(
AddressBook
addressBook
)
{
for
(
Person
person
in
addressBook
.
people
)
{
print
(
'Person ID:
${person.id}
'
);
print
(
' Name:
${person.name}
'
);
if
(
person
.
hasEmail
())
{
print
(
' E-mail address:
${person.email}
'
);
}
for
(
Person_PhoneNumber
phoneNumber
in
person
.
phones
)
{
switch
(
phoneNumber
.
type
)
{
case
Person_PhoneType
.
MOBILE
:
print
(
' Mobile phone #: '
);
break
;
case
Person_PhoneType
.
HOME
:
print
(
' Home phone #: '
);
break
;
case
Person_PhoneType
.
WORK
:
print
(
' Work phone #: '
);
break
;
default
:
print
(
' Unknown phone #: '
);
break
;
}
print
(
phoneNumber
.
number
);
}
}
}
// Reads the entire address book from a file and prints all
// the information inside.
main
(
List
<
String
>
arguments
)
{
if
(
arguments
.
length
!=
1
)
{
print
(
'Usage: list_person ADDRESS_BOOK_FILE'
);
exit
(-
1
);
}
// Read the existing address book.
File
file
=
new
File
(
arguments
.
first
);
AddressBook
addressBook
=
new
AddressBook
.
fromBuffer
(
file
.
readAsBytesSync
());
printAddressBook
(
addressBook
);
}
examples/pubspec.yaml
0 → 100644
View file @
969397b7
name
:
addressbook
description
:
dartlang.org example code.
dependencies
:
protobuf
:
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