Skip to content

Commit

Permalink
CLI: Added a placeholder to cli deps node_modules folder to make sure…
Browse files Browse the repository at this point in the history
… node can load from it
  • Loading branch information
dcodeIO committed Mar 2, 2017
1 parent 8eeffcb commit 7a94453
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Empty file added cli/node_modules/placeholder
Empty file.
32 changes: 17 additions & 15 deletions examples/custom-get-set.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
// this example demonstrates a way to keep field casing (as defined within .proto files)
// while still having virtual getters and setters for the camel cased counterparts.

/*eslint-disable strict, no-console*/
var protobuf = require("..");

var proto = "syntax=\"proto3\";\
message MyMessage {\
string some_field = 1;\
}";

var root = protobuf.parse(proto, { keepCase: true }).root;
var root = protobuf.parse(proto, { keepCase: true }).root; // or use Root#load

function camelCase(str) {
function toCamelCase(str) {
return str.substring(0,1) + str.substring(1).replace(/_([a-z])(?=[a-z]|$)/g, function($0, $1) { return $1.toUpperCase(); });
}

function addAliasProperty(type, name, aliasName) {
if (aliasName !== name)
Object.defineProperty(type.ctor.prototype, aliasName, {
get: function() { return this[name]; },
set: function(value) { this[name] = value; }
});
}

// this function adds alternative getters and setters for the camel cased counterparts
// to the runtime message's prototype (i.e. without having to register a custom class):
function addVirtualCamelcaseFields(type) {
type.fieldsArray.forEach(function(field) {
var altName = camelCase(field.name);
if (altName !== field.name)
Object.defineProperty(type.ctor.prototype, altName, {
get: function() {
return this[field.name];
},
set: function(value) {
this[field.name] = value;
}
});
addAliasProperty(type, field.name, toCamelCase(field.name));
});
type.oneofsArray.forEach(function(oneof) {
addAliasProperty(type, oneof.name, toCamelCase(oneof.name));
});
return type;
}

var MyMessage = root.lookup("MyMessage");

addVirtualCamelcaseFields(MyMessage);
var MyMessage = addVirtualCamelcaseFields(root.lookup("MyMessage"));

var myMessage = MyMessage.create({
some_field /* or someField */: "hello world"
Expand Down

0 comments on commit 7a94453

Please sign in to comment.