Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform identifiers to look like idiomatic JavaScript #81

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/client/proxy-interface.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const {
identifierToJs,
identifierFromJs
} = require('../conventions');
const EventEmitter = require('events');
const {
isInterfaceNameValid,
Expand All @@ -16,7 +20,7 @@ class ProxyListener {
console.error(`warning: got signature ${signature} for signal ${msg.interface}.${signal.name} (expected ${signal.signature})`);
return;
}
iface.emit.apply(iface, [signal.name].concat(body));
iface.emit.apply(iface, [identifierToJs(signal.name)].concat(body));
};
}
}
Expand Down Expand Up @@ -78,7 +82,8 @@ class ProxyInterface extends EventEmitter {
return [signal, detailedEvent];
};

this.on('removeListener', (eventName, listener) => {
this.on('removeListener', (eventNameJs, listener) => {
const eventName = identifierFromJs(eventNameJs);
const [signal, detailedEvent] = getEventDetails(eventName);

if (!signal) {
Expand All @@ -103,7 +108,8 @@ class ProxyInterface extends EventEmitter {
this.$object.bus._signals.removeListener(detailedEvent, proxyListener.fn);
});

this.on('newListener', (eventName, listener) => {
this.on('newListener', (eventNameJs, listener) => {
const eventName = identifierFromJs(eventNameJs);
const [signal, detailedEvent] = getEventDetails(eventName);

if (!signal) {
Expand Down Expand Up @@ -208,7 +214,7 @@ class ProxyInterface extends EventEmitter {
// TODO signature validation
iface.$methods.push(method);

iface[method.name] = function (...args) {
iface[identifierToJs(method.name)] = function (...args) {
const objArgs = [
name,
method.name,
Expand Down
12 changes: 12 additions & 0 deletions lib/conventions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function identifierToJs (identifier) {
return identifier.charAt(0).toLowerCase() + identifier.slice(1);
}

function identifierFromJs (identifier) {
return identifier.charAt(0).toUpperCase() + identifier.slice(1);
}

module.exports = {
identifierToJs,
identifierFromJs
};
52 changes: 27 additions & 25 deletions lib/service/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* @module interface
*/
const { identifierFromJs } = require('../conventions');
const { parseSignature, collapseSignature } = require('../signature');
const variant = require('../variant');
const Variant = variant.Variant;
Expand Down Expand Up @@ -56,10 +57,10 @@ const {
* class MyInterface extends Interface {
* // uncomment below to use the decorator (jsdoc bug)
* //@property({signature: 's'})
* get MyProp() {
* get myProp() {
* return this.myProp;
* }
* set MyProp(value) {
* set myProp(value) {
* this.myProp = value;
* }
* }
Expand All @@ -80,7 +81,7 @@ function property (options) {
}
options.signatureTree = parseSignature(options.signature);
return function (descriptor) {
options.name = options.name || descriptor.key;
options.name = options.name || identifierFromJs(descriptor.key);
assertMemberNameValid(options.name);
descriptor.finisher = function (klass) {
klass.prototype.$properties = klass.prototype.$properties || [];
Expand Down Expand Up @@ -114,20 +115,20 @@ function property (options) {
*
* class MyInterface extends Interface {
* //@method({inSignature: 's', outSignature: 's'})
* async Echo(what) {
* async echo(what) {
* return what;
* }
*
* //@method({inSignature: 'ss', outSignature: 'vv'})
* ReturnsMultiple(what, what2) {
* returnsMultiple(what, what2) {
* return [
* new Variant('s', what),
* new Variant('s', what2)
* ];
* }
*
* //@method({inSignature: '', outSignature: ''})
* ThrowsError() {
* throwsError() {
* // the error is returned to the client
* throw new DBusError('org.test.iface.Error', 'something went wrong');
* }
Expand All @@ -153,7 +154,7 @@ function method (options) {
options.inSignatureTree = parseSignature(options.inSignature);
options.outSignatureTree = parseSignature(options.outSignature);
return function (descriptor) {
options.name = options.name || descriptor.key;
options.name = options.name || identifierFromJs(descriptor.key);
assertMemberNameValid(options.name);
options.fn = descriptor.descriptor.value;
descriptor.finisher = function (klass) {
Expand All @@ -179,12 +180,12 @@ function method (options) {
* // uncomment the decorators to use them (jsdoc bug)
* class MyInterface extends Interface {
* //@signal({signature: 's'})
* HelloWorld(value) {
* helloWorld(value) {
* return value;
* }
*
* //@signal({signature: 'ss'})
* SignalMultiple(x) {
* signalMultiple(x) {
* return [
* 'hello',
* 'world'
Expand All @@ -205,7 +206,7 @@ function signal (options) {
options.signature = options.signature || '';
options.signatureTree = parseSignature(options.signature);
return function (descriptor) {
options.name = options.name || descriptor.key;
options.name = options.name || identifierFromJs(descriptor.key);
assertMemberNameValid(options.name);
options.fn = descriptor.descriptor.value;
descriptor.descriptor.value = function () {
Expand Down Expand Up @@ -271,18 +272,18 @@ class Interface {
* @example
* ConfiguredInterface.configureMembers({
* properties: {
* SomeProperty: {
* someProperty: {
* signature: 's'
* }
* },
* methods: {
* Echo: {
* echo: {
* inSignature: 'v',
* outSignature: 'v'
* }
* },
* signals: {
* HelloWorld: {
* helloWorld: {
* signature: 'ss'
* }
* }
Expand Down Expand Up @@ -312,7 +313,7 @@ class Interface {

for (const k of Object.keys(properties)) {
const options = properties[k];
options.name = options.name || k;
options.name = options.name || identifierFromJs(k);
options.access = options.access || ACCESS_READWRITE;
if (!options.signature) {
throw new Error('missing signature for property');
Expand All @@ -324,7 +325,7 @@ class Interface {

for (const k of Object.keys(methods)) {
const options = methods[k];
options.name = options.name || k;
options.name = options.name || identifierFromJs(k);
assertMemberNameValid(options.name);
options.disabled = !!options.disabled;
options.inSignature = options.inSignature || '';
Expand All @@ -337,7 +338,7 @@ class Interface {

for (const k of Object.keys(signals)) {
const options = signals[k];
options.name = options.name || k;
options.name = options.name || identifierFromJs(k);
assertMemberNameValid(options.name);
options.fn = this.prototype[k];
options.signature = options.signature || '';
Expand All @@ -361,7 +362,7 @@ class Interface {
*
* @static
* @example
* Interface.emitPropertiesChanged({ SomeProperty: 'bar' }, ['InvalidedProperty']);
* Interface.emitPropertiesChanged({ someProperty: 'bar' }, ['invalidatedProperty']);
*
* @param {module:interface~Interface} - the `Interface` to emit the `PropertiesChanged` signal on
* @param {Object} - A map of property names and new property values that are changed.
Expand All @@ -377,13 +378,14 @@ class Interface {
// don't have to
const properties = iface.$properties || {};
const changedPropertiesVariants = {};
for (const p of Object.keys(changedProperties)) {
if (properties[p] === undefined) {
throw new Error(`got properties changed with unknown property: ${p}`);
for (const propNameJs of Object.keys(changedProperties)) {
const propName = identifierFromJs(propNameJs);
if (properties[propName] === undefined) {
throw new Error(`got properties changed with unknown property: ${propNameJs}`);
}
changedPropertiesVariants[p] = new Variant(properties[p].signature, changedProperties[p]);
changedPropertiesVariants[propName] = new Variant(properties[propName].signature, changedProperties[propNameJs]);
}
iface.$emitter.emit('properties-changed', changedPropertiesVariants, invalidatedProperties);
iface.$emitter.emit('properties-changed', changedPropertiesVariants, invalidatedProperties.map(identifierFromJs));
}

$introspect () {
Expand All @@ -403,7 +405,7 @@ class Interface {
xml.property = xml.property || [];
xml.property.push({
$: {
name: property.name,
name: identifierFromJs(property.name),
type: property.signature,
access: property.access
}
Expand All @@ -420,7 +422,7 @@ class Interface {
xml.method = xml.method || [];
const methodXml = {
$: {
name: method.name
name: identifierFromJs(method.name)
},
arg: [],
annotation: []
Expand Down Expand Up @@ -465,7 +467,7 @@ class Interface {
xml.signal = xml.signal || [];
const signalXml = {
$: {
name: signal.name
name: identifierFromJs(signal.name)
},
arg: []
};
Expand Down