Skip to content

Commit 7a94453

Browse files
committed
CLI: Added a placeholder to cli deps node_modules folder to make sure node can load from it
1 parent 8eeffcb commit 7a94453

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

cli/node_modules/placeholder

Whitespace-only changes.

examples/custom-get-set.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
// this example demonstrates a way to keep field casing (as defined within .proto files)
22
// while still having virtual getters and setters for the camel cased counterparts.
33

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

67
var proto = "syntax=\"proto3\";\
78
message MyMessage {\
89
string some_field = 1;\
910
}";
1011

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

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

18+
function addAliasProperty(type, name, aliasName) {
19+
if (aliasName !== name)
20+
Object.defineProperty(type.ctor.prototype, aliasName, {
21+
get: function() { return this[name]; },
22+
set: function(value) { this[name] = value; }
23+
});
24+
}
25+
1726
// this function adds alternative getters and setters for the camel cased counterparts
1827
// to the runtime message's prototype (i.e. without having to register a custom class):
1928
function addVirtualCamelcaseFields(type) {
2029
type.fieldsArray.forEach(function(field) {
21-
var altName = camelCase(field.name);
22-
if (altName !== field.name)
23-
Object.defineProperty(type.ctor.prototype, altName, {
24-
get: function() {
25-
return this[field.name];
26-
},
27-
set: function(value) {
28-
this[field.name] = value;
29-
}
30-
});
30+
addAliasProperty(type, field.name, toCamelCase(field.name));
3131
});
32+
type.oneofsArray.forEach(function(oneof) {
33+
addAliasProperty(type, oneof.name, toCamelCase(oneof.name));
34+
});
35+
return type;
3236
}
3337

34-
var MyMessage = root.lookup("MyMessage");
35-
36-
addVirtualCamelcaseFields(MyMessage);
38+
var MyMessage = addVirtualCamelcaseFields(root.lookup("MyMessage"));
3739

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

0 commit comments

Comments
 (0)