diff --git a/examples/Makefile b/examples/Makefile index 1ff7fa7fc5..4ad605641d 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -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 -I ../src/:. --dart_out=dart_tutorial addressbook.proto ../src/google/protobuf/timestamp.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 diff --git a/examples/README.md b/examples/README.md index 20f285cdd7..3eb63527b7 100644 --- a/examples/README.md +++ b/examples/README.md @@ -119,6 +119,26 @@ is created if it does not exist. To view the data, run: ./list_people_go addressbook.data -Observe that the C++, Python, and Java examples in this directory run in a +Observe that the C++, Python, Java, and Dart 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: + +```sh +$ 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. diff --git a/examples/add_person.dart b/examples/add_person.dart new file mode 100644 index 0000000000..a93ec60540 --- /dev/null +++ b/examples/add_person.dart @@ -0,0 +1,70 @@ +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 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()); +} diff --git a/examples/list_people.dart b/examples/list_people.dart new file mode 100644 index 0000000000..dd5bb41005 --- /dev/null +++ b/examples/list_people.dart @@ -0,0 +1,47 @@ +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 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); +} diff --git a/examples/pubspec.yaml b/examples/pubspec.yaml new file mode 100644 index 0000000000..0c36dbdcd3 --- /dev/null +++ b/examples/pubspec.yaml @@ -0,0 +1,5 @@ +name: addressbook +description: dartlang.org example code. + +dependencies: + protobuf: