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

fix: Fix the problem of null judgment error #21

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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
128 changes: 120 additions & 8 deletions __tests__/utils/filter-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,122 @@ describe('filterData function', () => {
});

it('should return unfiltered data if no filters are provided', () => {
const filters = [];
const filters: CrudFilters = [];
const filteredData = filterData(filters, unstructuredData);
expect(filteredData).toEqual(unstructuredData);
});

it('filters with nested structure', () => {
const filters: CrudFilters = [
{
field: 'status.state', operator: 'eq', value: 'Normal',
},
{
field: 'status.state', operator: 'in', value: [
'InUpdate',
'InRollback',
],
},
{
operator: 'and', value: [
{
field: 'metadata.deletionTimestamp', operator: 'nnull', value: true,
},
{
field: 'status.state', operator: 'ne', value: 'DeleteFailed',
},
],
},
];
const data: (Unstructured & { status: { state: string; } })[] = [
{
apiVersion: 'v1',
kind: 'Pod',
metadata: { name: 'Pod A', namespace: 'Namespace A' },
status: {
state: 'Normal',
},
},
{
apiVersion: 'v1',
kind: 'Pod',
metadata: { name: 'Pod B', namespace: 'Namespace B' },
status: {
state: 'InRollback',
},
},
{
apiVersion: 'v1',
kind: 'Pod',
metadata: { name: 'Pod C', namespace: 'Namespace A', deletionTimestamp: 'mock time' },
status: {
state: 'InUpgrade',
},
},
];

expect(filterData([
{
operator: 'and',
value: [
filters[0],
],
},
], data)).toEqual([{
apiVersion: 'v1',
kind: 'Pod',
metadata: { name: 'Pod A', namespace: 'Namespace A' },
status: {
state: 'Normal',
},
}]);

expect(filterData([
{
operator: 'and',
value: [
filters[1],
],
},
], data)).toEqual([{
apiVersion: 'v1',
kind: 'Pod',
metadata: { name: 'Pod B', namespace: 'Namespace B' },
status: {
state: 'InRollback',
},
}]);

expect(filterData([
{
operator: 'and',
value: [
filters[2],
],
},
], data)).toEqual([{
apiVersion: 'v1',
kind: 'Pod',
metadata: { name: 'Pod C', namespace: 'Namespace A', deletionTimestamp: 'mock time' },
status: {
state: 'InUpgrade',
},
},]);

expect(filterData([
{
operator: 'and',
value: filters,
},
], data)).toEqual([]);

expect(filterData([
{
operator: 'or',
value: filters,
},
], data)).toEqual(data);
});
});

describe('evaluateFilter function', () => {
Expand All @@ -107,8 +219,8 @@ describe('evaluateFilter function', () => {
total: 10,
labels: ['label-1', 'label-2'],
description: null,
type: 'type-1'
}
type: 'type-1',
},
} as Unstructured;

test('handles "eq" operator', () => {
Expand Down Expand Up @@ -190,13 +302,13 @@ describe('evaluateFilter function', () => {
});

test('handles "null" operator', () => {
const result = evaluateFilter(mockItem, 'spec.description', 'null', null);
expect(result).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.description', 'null', null)).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.non_exist_path', 'null', null)).toBeFalsy();
});

test('handles "nnull" operator', () => {
const result = evaluateFilter(mockItem, 'spec.total', 'nnull', null);
expect(result).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.total', 'nnull', null)).toBeTruthy();
expect(evaluateFilter(mockItem, 'spec.non_exist_path', 'null', null)).toBeFalsy();
});

test('handles "startswith" operator', () => {
Expand Down Expand Up @@ -238,4 +350,4 @@ describe('evaluateFilter function', () => {
const result = evaluateFilter(mockItem, 'metadata.name', 'nendswiths', 'SERVICE');
expect(result).toBeTruthy();
});
})
});
66 changes: 33 additions & 33 deletions src/utils/filter-data.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
import { CrudFilters, CrudOperators, LogicalFilter } from '@refinedev/core';
import { CrudFilter, CrudFilters, CrudOperators } from '@refinedev/core';
import _ from 'lodash';
import { Unstructured } from '../kube-api';

function deepFilter(item: Unstructured, filter: CrudFilter): boolean {
if ('field' in filter) {
// Logical filter
const { field, operator, value } = filter;
return evaluateFilter(item, field, operator, value);
} else {
// Conditional filter
const { operator, value } = filter;
if (operator === 'or') {
return value.some(subFilter => deepFilter(item, subFilter));
} else if (operator === 'and') {
return value.every(subFilter => deepFilter(item, subFilter));
}
}
return true;
}

export const filterData = (
filters: CrudFilters,
data: Unstructured[]
data: Unstructured[],
): Unstructured[] => {
if (!filters || filters.length === 0) {
return data;
}

return data.filter(item => {
return filters.every(filter => {
if ('field' in filter) {
// Logical filter
const { field, operator, value } = filter;
return evaluateFilter(item, field, operator, value);
} else {
// Conditional filter
const { operator, value } = filter;
if (operator === 'or') {
return value.some(subFilter => {
const { field, operator, value } = subFilter as LogicalFilter;
return evaluateFilter(item, field, operator, value);
});
} else if (operator === 'and') {
return value.every(subFilter => {
const { field, operator, value } = subFilter as LogicalFilter;
return evaluateFilter(item, field, operator, value);
});
}
}
return true;
});
});
return data.filter(item => filters.every(filter => deepFilter(item, filter)));
};

export function evaluateFilter(
item: Unstructured,
field: string,
operator: Exclude<CrudOperators, 'or' | 'and'>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any
value: any,
): boolean {
if (!_.has(item, field)) {
return false;
}

const fieldValue = _.get(item, field);

switch (operator) {
Expand All @@ -66,7 +64,7 @@ export function evaluateFilter(
if (Array.isArray(fieldValue)) {
return _.includes(fieldValue, item);
}
return item === fieldValue
return item === fieldValue;
});
}
case 'nin': {
Expand All @@ -77,7 +75,7 @@ export function evaluateFilter(
if (Array.isArray(fieldValue)) {
return !_.includes(fieldValue, item);
}
return item !== fieldValue
return item !== fieldValue;
});
}
case 'contains':
Expand All @@ -92,10 +90,12 @@ export function evaluateFilter(
return value[0] <= fieldValue && fieldValue <= value[1];
case 'nbetween':
return value[0] > fieldValue || fieldValue > value[1];
case 'null':
return fieldValue === null;
case 'nnull':
return fieldValue !== null;
case 'null': {
Copy link
Member

@self-transition self-transition Oct 19, 2023

Choose a reason for hiding this comment

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

Is it possible to put this judgment at the beginning of the function, so that when path is not a direct property of object, it doesn't do anything and returns the original value?

Copy link
Author

Choose a reason for hiding this comment

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

you're right! I think it should return false when path isn't direct property of object, because it mean that data is not valid.

return _.isNil(fieldValue);
}
case 'nnull': {
return !_.isNil(fieldValue);
}
case 'startswith':
return fieldValue.startsWith(value);
case 'nstartswith':
Expand Down