Skip to content

Add getParamType function to handle ROS message type mapping #25

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 26 additions & 13 deletions src/lib/generateFromRosMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { camelCase, compact, partition, snakeCase, upperFirst } from 'lodash';

import { IConfig } from '../types/config';

import { primitives1, primitives2 } from './primitives';
import { primitiveArrayTypes, primitives1, primitives2 } from './primitives';

const SUPPORTED_ROS_VERSIONS = [1, 2];

Expand Down Expand Up @@ -127,6 +127,27 @@ export const generateFromRosMsg = (

const fullMessageDefinitions = [...messageDefinitions, ...serviceDefinitions];

const getParamType = (param: MessageDefinitionField, pkgName: string) => {
if (param.type in primitiveArrayTypes && param.isArray) {
return primitiveArrayTypes[
param.type as keyof typeof primitiveArrayTypes
];
} else if (param.type in primitives) {
return primitives[param.type as keyof typeof primitives];
} else if (useNamespaces) {
if (param.type.split('/')[0] === pkgName) {
return pascalCase(param.type.split('/')[1]);
} else {
return param.type.split('/').map(pascalCase).join('.');
}
} else {
return rosNameToTypeName(
param.type,
(param as any)['typeAdjusted'] === true ? '' : typePrefix
);
}
};

const interfacesByPackage = fullMessageDefinitions
.map((definition) => {
if (!definition) return '\n';
Expand Down Expand Up @@ -235,18 +256,10 @@ ${enumEntriesFromFields(candidates_without_prefix)}
const tsTypes = defTypes
.filter((defType) => isOfNoneEmptyType(defType))
.map((param) => {
const paramType: string =
param.type in primitives
? primitives[param.type as keyof typeof primitives]
: useNamespaces
? param.type.split('/')[0] === pkgName
? pascalCase(param.type.split('/')[1])
: param.type.split('/').map(pascalCase).join('.')
: rosNameToTypeName(
param.type,
(param as any)['typeAdjusted'] === true ? '' : typePrefix
);
const arrayMarker = param.isArray ? '[]' : '';
const paramType: string = getParamType(param, pkgName);
const arrayMarker =
param.isArray && !paramType.includes('Array') ? '[]' : '';

return ` ${param.name}: ${paramType.replace(
'.',
`.${typePrefix}`
Expand Down
13 changes: 13 additions & 0 deletions src/lib/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ export const primitives2 = {
duration: '{ sec: number, nanosec: number }',
time: '{ sec: number, nanosec: number }',
};

export const primitiveArrayTypes = {
float32: 'Float32Array',
uint8: 'Uint8Array',
uint16: 'Uint16Array',
int8: 'Int8Array',
int16: 'Int16Array',
int32: 'Int32Array',
uint32: 'Uint32Array',
float64: 'Float64Array',
bigint64: 'BigInt64Array',
biguint64: 'BigUint64Array',
};