-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Medium/nick-extend
Add extends to pbnj. This will let us break cycles by letting us add fields later.
- Loading branch information
Showing
7 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
/** | ||
* @fileoverview Extend class for extending a pb message. | ||
*/ | ||
|
||
var util = require('util') | ||
var Descriptor = require('./Descriptor') | ||
var helper = require('../helper') | ||
|
||
|
||
/** | ||
* @param {string} name The message name | ||
* @constructor | ||
* @extends {Descriptor} | ||
*/ | ||
function ExtendDescriptor(name) { | ||
Descriptor.call(this) | ||
|
||
this._name = name | ||
this._fields = {} | ||
this._fieldNames = {} | ||
} | ||
util.inherits(ExtendDescriptor, Descriptor) | ||
module.exports = ExtendDescriptor | ||
|
||
|
||
/** | ||
* Merge this Extend into the original message. | ||
*/ | ||
ExtendDescriptor.prototype.mergeInto = function (message) { | ||
for (var key in this._fields) { | ||
var field = this._fields[key] | ||
if (message.getFieldByTag(field.getTag())) { | ||
throw new Error( | ||
'duplicate tag name in extend "' + this._name + '" at field ' + field.getName()) | ||
} | ||
|
||
message.addField(field) | ||
} | ||
} | ||
|
||
|
||
/** @override */ | ||
ExtendDescriptor.prototype.inspect = function (depth) { | ||
return util.inspect(this.toTemplateObject(), false, null) | ||
} | ||
|
||
|
||
/** @override */ | ||
ExtendDescriptor.prototype.toTemplateObject = function () { | ||
return { | ||
name: this._name, | ||
fields: helper.values(this._fields, helper.toTemplateObject) | ||
} | ||
} | ||
|
||
|
||
ExtendDescriptor.prototype.getName = function () { | ||
return this._name | ||
} | ||
|
||
|
||
ExtendDescriptor.prototype.addField = function (field) { | ||
this._fields[field.getTag()] = field | ||
this._fieldNames[field.getName()] = field | ||
field.setParent(this) | ||
return this | ||
} | ||
|
||
|
||
ExtendDescriptor.prototype.getField = function (name) { | ||
return this._fieldNames[name] || this._fieldNames[helper.toProtoCase(name)] || null | ||
} | ||
|
||
|
||
ExtendDescriptor.prototype.getFieldByTag = function (tag) { | ||
return this._fields[tag] || null | ||
} | ||
|
||
|
||
ExtendDescriptor.prototype.getFields = function () { | ||
return helper.values(this._fields) | ||
} | ||
|
||
|
||
ExtendDescriptor.prototype.getFieldNames = function () { | ||
return Object.keys(this._fieldNames) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
/** | ||
* @fileoverview Tests that extensions work. | ||
*/ | ||
|
||
var fs = require('fs') | ||
var parser = require('../lib/parser') | ||
var path = require('path') | ||
var Project = require('../lib/Project') | ||
var baseDir = __dirname | ||
|
||
exports.testExtendsParsing = function (test) { | ||
var proto = parseFile('super-person.proto') | ||
test.equal(1, proto.getExtends().length) | ||
|
||
var e = proto.getExtend('Person') | ||
test.ok(e) | ||
|
||
test.equal('number', e.getField('flying_speed').getType()) | ||
test.done() | ||
} | ||
|
||
exports.testExtendsResolution = function (test) { | ||
var project = new Project(baseDir) | ||
.addProto('protos/person.proto') | ||
.addProto('protos/super-person.proto') | ||
|
||
project._resolveExtensions() | ||
var person = project.getProtos('protos/person.proto')[0] | ||
var personMsg = person.getMessage('Person') | ||
test.equal('number', personMsg.getField('flying_speed').getType()) | ||
test.done() | ||
} | ||
|
||
function parseFile(file) { | ||
return parser(file, fs.readFileSync(path.join(__dirname, 'protos', file), 'utf8')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Extension of person | ||
*/ | ||
|
||
import "protos/person.proto"; | ||
|
||
package examples; | ||
|
||
extend Person { | ||
optional int32 flying_speed = 101; | ||
} |