Skip to content

Commit

Permalink
chore: Remove set and get as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lordrip committed Dec 16, 2024
1 parent a68039a commit ee781f4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe('Tests for Design page', () => {
cy.checkCodeSpanLine('json-deserialize-action', 0);
cy.checkCodeSpanLine('kafka-source', 0);
cy.checkCodeSpanLine('kafka-sink', 0);
cy.checkCodeSpanLine('source: {}', 1);
cy.checkCodeSpanLine('sink: {}', 1);
cy.checkCodeSpanLine('source: {}', 0);
cy.checkCodeSpanLine('sink: {}', 0);
});

it('In an integration with at least two steps, user can not delete the from step', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/src/models/camel/kamelet-resource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { set } from 'lodash';
import { TileFilter } from '../../components/Catalog/Catalog.models';
import { setValue } from '../../utils';
import { IKameletDefinition } from '../kamelets-catalog';
import { AddStepMode } from '../visualization/base-visual-entity';
import { CamelComponentFilterService } from '../visualization/flows/support/camel-component-filter.service';
Expand Down Expand Up @@ -61,9 +61,9 @@ export class KameletResource extends CamelKResource implements RouteTemplateBean
* and this way the kamelet definition is updated when the user interacts with
* the CamelRouteVisualEntity.
*/
set(this.resource, 'metadata.name', this.flow.getId());
set(this.resource, 'spec.template.from', this.flow.entityDef.template.from);
set(this.resource, 'spec.template.beans', this.beans?.parent.beans);
setValue(this.resource, 'metadata.name', this.flow.getId());
setValue(this.resource, 'spec.template.from', this.flow.entityDef.template.from);
setValue(this.resource, 'spec.template.beans', this.beans?.parent.beans);
return this.resource as IKameletDefinition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ describe('Pipe', () => {
pipeVisualEntity.removeStep('source');

expect(pipeVisualEntity.toJSON()).not.toEqual(pipeJson.spec);
expect(pipeVisualEntity.pipe.spec?.source).toEqual({});
expect(pipeVisualEntity.pipe.spec?.source).toBeUndefined();
});

it('should remove the `sink` step', () => {
pipeVisualEntity.removeStep('sink');

expect(pipeVisualEntity.toJSON()).not.toEqual(pipeJson.spec);
expect(pipeVisualEntity.pipe.spec?.sink).toEqual({});
expect(pipeVisualEntity.pipe.spec?.sink).toBeUndefined();
});

it('should remove the `steps.0` step', () => {
Expand Down
10 changes: 2 additions & 8 deletions packages/ui/src/models/visualization/flows/pipe-visual-entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Pipe } from '@kaoto/camel-catalog/types';
import { set } from 'lodash';
import { getCamelRandomId } from '../../../camel-utils/camel-random-id';
import { SchemaService } from '../../../components/Form/schema.service';
import {
Expand Down Expand Up @@ -152,12 +151,7 @@ export class PipeVisualEntity implements BaseVisualCamelEntity {

/** Replace an existing Kamelet */
if (options.mode === AddStepMode.ReplaceStep) {
if (path === 'source' || path === 'sink') {
set(this.pipe.spec!, path, step);
} else {
set(this.pipe.spec!, path, step);
}

setValue(this.pipe.spec, path, step);

Check warning on line 154 in packages/ui/src/models/visualization/flows/pipe-visual-entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/visualization/flows/pipe-visual-entity.ts#L154

Added line #L154 was not covered by tests
return;
}

Expand All @@ -180,7 +174,7 @@ export class PipeVisualEntity implements BaseVisualCamelEntity {
* If the path is `source` or `sink`, we can remove it directly
*/
if (path === 'source' || path === 'sink') {
set(this.pipe.spec!, path, {});
setValue(this.pipe.spec, path, {});
return;
}
const pathArray = path.split('.');
Expand Down
9 changes: 5 additions & 4 deletions packages/ui/src/utils/get-array-property.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { get, set } from 'lodash';
import { getValue } from './get-value';
import { setValue } from './set-value';

export const getArrayProperty = <T>(model: object, path: string): T[] => {
let stepsArray: T[] | undefined = get(model, path);
let stepsArray: T[] | undefined = getValue(model, path);

if (!Array.isArray(stepsArray)) {
set(model, path, []);
stepsArray = get(model, path) as T[];
setValue(model, path, []);
stepsArray = getValue(model, path) as T[];
}

return stepsArray;
Expand Down
9 changes: 4 additions & 5 deletions packages/ui/src/utils/pipe-custom-schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Pipe } from '@kaoto/camel-catalog/types';
import { getValue } from './get-value';
import { setValue } from './set-value';
import { set } from 'lodash';

export const getCustomSchemaFromPipe = (pipe: Pipe) => {
const name: string = getValue(pipe, 'metadata.name', '');
Expand All @@ -19,11 +18,11 @@ export const getCustomSchemaFromPipe = (pipe: Pipe) => {

export const updatePipeFromCustomSchema = (pipe: Pipe, value: Record<string, unknown>): void => {
// Ensure 'labels' and 'annotations' are defined in 'value'
if (getValue(value, 'labels') === undefined) {
set(value, 'labels', {});
if (value && getValue(value, 'labels') === undefined) {
value.labels = {};

Check warning on line 22 in packages/ui/src/utils/pipe-custom-schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/utils/pipe-custom-schema.ts#L22

Added line #L22 was not covered by tests
}
if (getValue(value, 'annotations') === undefined) {
set(value, 'annotations', {});
if (value && getValue(value, 'annotations') === undefined) {
value.annotations = {};

Check warning on line 25 in packages/ui/src/utils/pipe-custom-schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/utils/pipe-custom-schema.ts#L25

Added line #L25 was not covered by tests
}
const previousName: string = getValue(pipe, 'metadata.name');
const newName: string = getValue(value, 'name');
Expand Down

0 comments on commit ee781f4

Please sign in to comment.