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

Adjust function Ros.prototype.decodeTypeDefs #463

Open
peach96 opened this issue Aug 1, 2021 · 0 comments
Open

Adjust function Ros.prototype.decodeTypeDefs #463

peach96 opened this issue Aug 1, 2021 · 0 comments

Comments

@peach96
Copy link

peach96 commented Aug 1, 2021

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);
    } 
k-aguete pushed a commit to k-aguete/roslibjs that referenced this issue Oct 21, 2022
Bumps [karma](https://github.com/karma-runner/karma) from 6.3.6 to 6.3.8.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](karma-runner/karma@v6.3.6...v6.3.8)

---
updated-dependencies:
- dependency-name: karma
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant