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

RSDK-6632 - Typescript SDK throws if MotionConfiguration.obstacleDetectorsList is not specified #249

Merged
merged 8 commits into from
Mar 1, 2024
25 changes: 18 additions & 7 deletions src/services/motion/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type ObstacleDetector = pb.ObstacleDetector.AsObject;
export type LinearConstraint = pb.LinearConstraint.AsObject;
export type OrientationConstraint = pb.OrientationConstraint.AsObject;
export type CollisionSpecification = pb.CollisionSpecification.AsObject;
export type MotionConfiguration = pb.MotionConfiguration.AsObject;
export type MotionConfiguration = Partial<pb.MotionConfiguration.AsObject>;
export type GetPlanResponse = pb.GetPlanResponse.AsObject;
export type ListPlanStatusesResponse = pb.ListPlanStatusesResponse.AsObject;
type ValueOf<T> = T[keyof T];
Expand Down Expand Up @@ -71,8 +71,9 @@ export const encodeMotionConfiguration = (
): pb.MotionConfiguration => {
const result = new pb.MotionConfiguration();

const { obstacleDetectorsList = [] } = obj;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this behave:

  1. if obj is undefined?
  2. if obstacleDetectorsList is not a member of obj?

Copy link
Member

@micheal-parks micheal-parks Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The parameter type for obj for this function states that it will never be undefined. If that's not the case, it should be an optional param, and the param should recieve a default value:
export const encodeMotionConfiguration = (
  obj?: MotionConfiguration = {}
): pb.MotionConfiguration =>
  1. If it's not a member of this object, it will be assigned a default value of [].

Copy link
Member Author

@nfranczak nfranczak Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if obj is undefined?

The parameter type for obj for this function states that it will never be undefined. If that's not the case, it should be an optional param, and the param should recieve a default value:

Within typescript-sdk/src/services/motion/client.ts for moveOnGlobe and moveOnMap we do

    if (motionConfig) {
      request.setMotionConfiguration(encodeMotionConfiguration(motionConfig));
    }

So I think it is safe to assume that obj will not be undefined entirely.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I'd actually recommend making this an optional param like I've done above. If all fields are optional then there's no reason the user should have to pass an empty object into this function.

result.setObstacleDetectorsList(
obj.obstacleDetectorsList.map((od: ObstacleDetector) => {
obstacleDetectorsList.map((od: ObstacleDetector) => {
const obstacleDetector = new pb.ObstacleDetector();
if (od.visionService) {
obstacleDetector.setVisionService(encodeResourceName(od.visionService));
Expand All @@ -83,11 +84,21 @@ export const encodeMotionConfiguration = (
return obstacleDetector;
})
);
result.setPositionPollingFrequencyHz(obj.positionPollingFrequencyHz);
result.setObstaclePollingFrequencyHz(obj.obstaclePollingFrequencyHz);
result.setPlanDeviationM(obj.planDeviationM);
result.setLinearMPerSec(obj.linearMPerSec);
result.setAngularDegsPerSec(obj.angularDegsPerSec);
if ('positionPollingFrequencyHz' in obj) {
result.setPositionPollingFrequencyHz(obj.positionPollingFrequencyHz);
}
if ('obstaclePollingFrequencyHz' in obj) {
result.setObstaclePollingFrequencyHz(obj.obstaclePollingFrequencyHz);
}
if ('planDeviationM' in obj) {
result.setPlanDeviationM(obj.planDeviationM);
}
if ('linearMPerSec' in obj) {
result.setLinearMPerSec(obj.linearMPerSec);
}
if ('angularDegsPerSec' in obj) {
result.setAngularDegsPerSec(obj.angularDegsPerSec);
}

return result;
};
Loading