Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

Commit

Permalink
Generate a default package name and fix a bug with nested messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fungiboletus committed Nov 6, 2013
1 parent 7246f21 commit 80c7fff
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 8 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ Proto2TypeScript

This tool generate TypeScript definitions for your Protocol Buffers models, when you use the excellent [ProtoBuf.js](https://github.com/dcodeIO/ProtoBuf.js/) library.

It's a Node.js project, but a web demo is coming.

### Usage
```sh
# Install the dependencies
npm install
npm install --production

# Parse and convert the proto file to json using proto2js (from ProtoBuf.js)
node_modules/protobufjs/bin/proto2js model.proto > model.json
Expand All @@ -26,6 +24,10 @@ Options:
-p, --properties Generate properties [default: true]
```

### Requirements

It is a Node.js project. The sourcecode is written in TypeScript, but the JavaScript output is present in the repository. You need Bash if you want to run the tests.

### Acknowledgements

This code is developed in context of the [BRIDGE](http://www.bridgeproject.eu/en) project.
Expand Down
6 changes: 5 additions & 1 deletion command.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function generateNames(model, prefix, name) {
for (var key in model.messages) {
var message = model.messages[key];
newDefinitions[message.name] = true;
generateNames(message, prefix, "." + (model.name ? model.name : ""));
generateNames(message, model.fullPackageName, "." + (model.name ? model.name : ""));
}

for (var key in model.enums) {
Expand All @@ -105,6 +105,10 @@ loadDustTemplate("enum");
// Load the json file
var model = JSON.parse(fs.readFileSync(argv.file).toString());

if (!model.package) {
model.package = "Proto2TypeScript";
}

// Generates the names of the model
generateNames(model, model.package);

Expand Down
7 changes: 6 additions & 1 deletion command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function generateNames (model : any, prefix : string, name : string = "") : void
for (var key in model.messages) {
var message = model.messages[key];
newDefinitions[message.name] = true;
generateNames(message,prefix, "."+(model.name ? model.name : ""));
generateNames(message,model.fullPackageName, "."+(model.name ? model.name : ""));
}

// Generate names for enums
Expand Down Expand Up @@ -130,6 +130,11 @@ loadDustTemplate("enum");
// Load the json file
var model = JSON.parse(fs.readFileSync(argv.file).toString());

// If a packagename isn't present, use a default package name
if (!model.package) {
model.package = "Proto2TypeScript";
}

// Generates the names of the model
generateNames(model, model.package);

Expand Down
3 changes: 1 addition & 2 deletions templates/interface.dust
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ declare module {fullPackageName} {

export interface {name} extends AProtoBufJS {
{#fields}{#properties}{name}{rule|optionalFieldDeclaration}: {type|convertType}{rule|repeatedType};
{/properties}{#camelCaseGetSet}
get{name|firstLetterInUpperCase}() : {type|convertType}{rule|repeatedType};
{/properties}{#camelCaseGetSet}get{name|firstLetterInUpperCase}() : {type|convertType}{rule|repeatedType};
set{name|firstLetterInUpperCase}({name|firstLetterInLowerCase} : {type|convertType}{rule|repeatedType}): void;
{/camelCaseGetSet}{#underscoreGetSet}
get_{name}() : {type|convertType}{rule|repeatedType};
Expand Down
2 changes: 1 addition & 1 deletion templates/module.dust
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module {package} {
interface AProtoBufJS {
toArrayBuffer(): ArrayBuffer;
toBuffer(): NodeBuffer;
//toBuffer(): Buffer;
toBase64(): string;
toString(): string;
}
Expand Down
175 changes: 175 additions & 0 deletions tests/complete.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package LSTransmission;

// It's a WGS 84 location
message LatLng {
required double lat = 1;
required double lng = 2;
}

message AgeModel {
optional uint32 Value = 1;
optional string Unit = 2;
}

message BloodPressureModel {
optional int32 SystolicBloodPressure = 1;
optional int32 DiastolicBloodPressure = 2;
}

message VitalSignsModel {
optional BloodPressureModel BloodPressure = 1;
optional int32 PulseRate = 2;
optional int32 RespiratoryRate = 3;
optional double Temperature = 4;
optional uint64 DateTime = 5;
}

message TriageInfoModel {
optional string ReportProviderId = 1;
optional string ReportId = 2;
optional uint64 ReportDateTime = 3;
}


message HelpBeaconModel {
required string HelpBeaconID = 1;
optional string BasicID = 2;
optional string Message = 3;
optional uint64 LastScanDateTime = 4;
optional bool Notified = 5;
optional bool Rescued = 6;
optional bool Safe = 7;

optional uint64 AdvertisedDateTime = 8;
optional string Name = 9;
optional string Imei = 10;
optional string Source = 11;
optional uint64 Time = 12;

optional LatLng Location = 13;

message DictionaryEntry {
required string key = 1;
required string value = 2;
}

// repeated HelpBeaconModel.DictionaryEntry DistributionDictionay = 14;
repeated DictionaryEntry DistributionDictionay = 14;
}

message IncidentObjectModel {
required string GraphicID = 1;
optional string GraphicValue = 2;
optional string Description = 3;

optional LatLng GraphicMinPosition = 4;
optional LatLng GraphicMaxPosition = 5;

repeated LatLng LinearRingPointList = 6;
}

message MediaModel {
optional string Title = 1;
optional uint64 AvailableDateTime = 2;
repeated string Tags = 3;
optional string LocalFileUriString = 4;
optional string UriString = 5;
optional int32 Type = 6;
optional LatLng Location = 7;
optional string Description = 8;
optional string Author = 9;
optional string SubeventKeywords = 10;
optional string GroupName = 11;
}

message MessengerModel {
required string MessageID = 1;
optional string SenderID = 2;
optional string ActionPlan = 3;
optional uint64 SentDateTime = 4;
}

message PatientModel {
required string Id = 1;
optional TriageInfoModel TriageInfo = 2;
optional AgeModel Age = 3;
optional string Sex = 4;
optional TriageStatusEnum TriageStatus = 5;
optional string IncidentId = 6;
optional LatLng Location = 7;
optional string PersonalIdNumber = 8;
repeated VitalSignsModel VitalSigns = 9;
optional string TransferredToDestination = 10;
optional bool Transporting = 11;

enum TriageStatusEnum {
RED = 1;
YELLOW = 2;
GREEN = 3;
BLACK = 4;
WHITE = 5;
}
}


message ResourceMobilizationModel {
required string ResourceMobilizationID = 1;
optional LatLng Location = 2;
optional string Task = 3;
optional int32 NumberOfFireVehicles = 4;
optional int32 NumberOfFirePersonnel = 5;
optional int32 NumberOfMedicVehicles = 6;
optional int32 NumberOfMedicPersonnel = 7;
optional int32 NumberOfPoliceVehicles = 8;
optional int32 NumberOfPolicePersonnel = 9;
}

message ResourceStatusModel {
required string ResourceID = 1;
optional string type = 2;
optional string Assignment = 3;
optional string Name = 4;
optional string Status = 5;
optional LatLng Location = 6;
}



message Error {
required Status status = 1;
optional string message = 2;
enum Status {
BANANA = 1;
LINKSMART_ERROR = 2;
LSCOMM_ERROR = 3;
LSTRANSMISSION_ERROR = 4;
GENERIC_ERROR = 5;
CLIENT_ERROR = 6;
}
}

// The transaction is the same in both ways
// The client and the server use same messages
message Transaction {

message Content {
repeated HelpBeaconModel HelpBeaconList = 1;
repeated IncidentObjectModel IncidentObjectList = 2;
repeated MediaModel MediaList = 3;
repeated MessengerModel MessengerList = 4;
repeated PatientModel PatientList = 5;
repeated ResourceMobilizationModel ResourceMobilizationList = 6;
repeated ResourceStatusModel ResourceStatusList = 7;
}

// Elements to publish
optional Content PublishList = 1;

// Elements to remove
optional Content RemoveList = 2;

// This will be empty in general if everything
// works fine
repeated Error ErrorList = 3;
}

0 comments on commit 80c7fff

Please sign in to comment.