Skip to content

Commit

Permalink
Merge pull request #47 from Medium/daniel-synth-field
Browse files Browse the repository at this point in the history
add method for adding synthetic fields to messages
  • Loading branch information
dmccartney committed Dec 7, 2015
2 parents 0444cad + 1cda2ff commit 669e9cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/descriptors/MessageDescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var util = require('util')
var Descriptor = require('./Descriptor')
var FieldDescriptor = require('./FieldDescriptor')
var OneofDescriptor = require('./OneofDescriptor')
var helper = require('../helper')

Expand Down Expand Up @@ -97,6 +98,18 @@ MessageDescriptor.prototype.addField = function (field) {
return this
}

/**
* Adds a synthetic field to this message
* e.g. for use by a post-processor to add another field to a message after parsing before compiling
*
* @param {string|FieldType} type The synthetic field type, e.g. string or int32
* @param {string} name The field name
* @param {number} tag The tag number for this field
* @return {MessageDescriptor}
*/
MessageDescriptor.prototype.addSyntheticField = function (type, name, tag) {
return this.addField(new FieldDescriptor(type, name, tag))
}

/**
* @param {string} name
Expand Down
12 changes: 12 additions & 0 deletions tests/project_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ builder.add(function testRemoveField(test) {
test.done()
})

builder.add(function testAddSyntheticField(test) {
var project = new Project(baseDir)
.addProto('protos/common.proto')

var color = project.getProtos('protos/common.proto')[0].getMessage('Color')
test.equal(3, color.toTemplateObject().fields.length)

color.addSyntheticField('int32', 'alpha', 4)
test.equal(4, color.toTemplateObject().fields.length)
test.done()
})

builder.add(function testTypeResolution(test) {
var project = new Project(baseDir)
.addProto('protos/person.proto')
Expand Down

0 comments on commit 669e9cf

Please sign in to comment.