-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Other: Added a custom getters/setters example for gRPC
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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. | ||
|
||
var protobuf = require(".."); | ||
|
||
var proto = "syntax=\"proto3\";\ | ||
message MyMessage {\ | ||
string some_field = 1;\ | ||
}"; | ||
|
||
var root = protobuf.parse(proto, { keepCase: true }).root; | ||
|
||
function camelCase(str) { | ||
return str.substring(0,1) + str.substring(1).replace(/_([a-z])(?=[a-z]|$)/g, function($0, $1) { return $1.toUpperCase(); }); | ||
} | ||
|
||
// 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; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
var MyMessage = root.lookup("MyMessage"); | ||
|
||
addVirtualCamelcaseFields(MyMessage); | ||
|
||
var myMessage = MyMessage.create({ | ||
some_field /* or someField */: "hello world" | ||
}); | ||
|
||
console.log( | ||
"someField:", myMessage.someField, | ||
"\nsome_field:", myMessage.some_field, | ||
"\nJSON:", JSON.stringify(myMessage) | ||
); |