Commit 969397b7 authored by Sarah Zakarias's avatar Sarah Zakarias

Add Dart example.

parent 4426cb57
......@@ -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
......
......@@ -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.
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
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);
}
name: addressbook
description: dartlang.org example code.
dependencies:
protobuf:
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