Adjust function Ros.prototype.decodeTypeDefs

This issue has been tracked since 2021-08-01.

I wanted to use the function Ros.prototype.decodeTypeDefs in my project to decode the results I got from the functions Ros.prototype.getMessageDetails and Ros.prototype.getServiceRequestDetails, but it only worked for simple message/request types.

Therefore I adjusted the existing function, so that it would also work for complex types, for example 'geometry_msgs/twist'.
I thought I'd post my solution here, if anyone has the same problem:

 decodeTypeDefs(defs) {
        var that = this;

        // calls itself recursively to resolve type definition using hints.
        var decodeTypeDefsRec = function (theType, hints) {
            var typeDefDict = {};
            for (var i = 0; i < theType.fieldnames.length; i++) {
                var arrayLen = theType.fieldarraylen[i];
                var fieldName = theType.fieldnames[i];
                var fieldType = theType.fieldtypes[i];

                // check if fieldType can be found in fieldnames 
                var complexFieldType = (defs.findIndex(def => def.type === fieldType) !== -1) ? true : false;

                //if fieldType complex --> then change fieldarraylen to 0
                arrayLen = complexFieldType ? 0 : arrayLen;

                if (!complexFieldType) { // check if fieldType is complex
                    if (arrayLen === -1) {
                        typeDefDict[fieldName] = fieldType;
                    } else {
                        typeDefDict[fieldName] = [fieldType];
                    }
                } else {
                    // lookup the name
                    var sub = false;
                    for (var j = 0; j < hints.length; j++) {
                        if (hints[j].type.toString() === fieldType.toString()) {
                            sub = hints[j];
                            break;
                        }
                    }
                    if (sub) {
                        console.log(true);
                        var subResult = decodeTypeDefsRec(sub, hints);
                        if (arrayLen === -1) {
                        } else {
                            typeDefDict[fieldName] = [subResult];
                        }
                    } else {
                        that.emit('error', 'Cannot find ' + fieldType + ' in decodeTypeDefs');
                    }
                }
            }
            return typeDefDict;
        };
        return decodeTypeDefsRec(defs[0], defs);
    } 
More Details About Repo
Owner Name RobotWebTools
Repo Name roslibjs
Full Name RobotWebTools/roslibjs
Language JavaScript
Created Date 2013-03-15
Updated Date 2022-11-22
Star Count 549
Watcher Count 54
Fork Count 340
Issue Count 66

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date