From 9eb1102f985a569c97093f654c784bf5334964da Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 24 Feb 2025 00:42:38 +0100 Subject: [PATCH] catalog-react: rename $and to $all and $or to $any Signed-off-by: Patrik Oldsberg --- .../createEntityPredicateSchema.test.ts | 22 +++++++++---------- .../predicates/createEntityPredicateSchema.ts | 4 ++-- .../evaluateEntityPredicate.test.ts | 17 ++++++++------ .../predicates/evaluateEntityPredicate.ts | 8 +++---- .../src/alpha/predicates/types.ts | 4 ++-- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.test.ts b/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.test.ts index 3334343e8b..4fdf921c1a 100644 --- a/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.test.ts +++ b/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.test.ts @@ -28,7 +28,7 @@ describe('createEntityPredicateSchema', () => { { kind: 'component', 'spec.type': 'service' }, { 'metadata.tags': { $in: ['java'] } }, { - $and: [ + $all: [ { 'metadata.tags': { $contains: 'java' } }, { 'metadata.tags': { $contains: 'spring' } }, ], @@ -38,7 +38,7 @@ describe('createEntityPredicateSchema', () => { { 'metadata.tags.0': 'java' }, { $not: { 'metadata.tags': { $in: ['java'] } } }, { - $or: [{ kind: 'component', 'spec.type': 'service' }, { kind: 'group' }], + $any: [{ kind: 'component', 'spec.type': 'service' }, { kind: 'group' }], }, { relations: { @@ -50,16 +50,16 @@ describe('createEntityPredicateSchema', () => { }, { kind: 'component', 'spec.type': { $in: ['service', 'website'] } }, { - $or: [ + $any: [ { - $and: [ + $all: [ { kind: 'component', 'spec.type': { $in: ['service', 'website'] }, }, ], }, - { $and: [{ kind: 'api', 'spec.type': 'grpc' }] }, + { $all: [{ kind: 'api', 'spec.type': 'grpc' }] }, ], }, { kind: 'component', 'spec.type': { $in: ['service'] } }, @@ -71,10 +71,10 @@ describe('createEntityPredicateSchema', () => { kind: 'component', 'metadata.annotations.github.com/repo': { $exists: true }, }, - { $and: [{ x: { $exists: true } }] }, - { $or: [{ x: { $exists: true } }] }, + { $all: [{ x: { $exists: true } }] }, + { $any: [{ x: { $exists: true } }] }, { $not: { x: { $exists: true } } }, - { $not: { $and: [{ x: { $exists: true } }] } }, + { $not: { $all: [{ x: { $exists: true } }] } }, ])('should accept valid predicate %j', predicate => { expect(schema.parse(predicate)).toEqual(predicate); }); @@ -87,10 +87,10 @@ describe('createEntityPredicateSchema', () => { { kind: { $in: [{ x: 'foo' }] } }, { kind: { $in: [{ x: 'foo' }] } }, { 'spec.type': null }, - { $and: [{ x: { $unknown: true } }] }, - { $or: [{ x: { $unknown: true } }] }, + { $all: [{ x: { $unknown: true } }] }, + { $any: [{ x: { $unknown: true } }] }, { $not: { x: { $unknown: true } } }, - { $not: { $and: [{ x: { $unknown: true } }] } }, + { $not: { $all: [{ x: { $unknown: true } }] } }, { $unknown: 'foo' }, ])('should reject invalid predicate %j', predicate => { const result = schema.safeParse(predicate); diff --git a/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.ts b/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.ts index 147769c937..9e5f44b327 100644 --- a/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.ts +++ b/plugins/catalog-react/src/alpha/predicates/createEntityPredicateSchema.ts @@ -32,8 +32,8 @@ export function createEntityPredicateSchema(z: typeof zImpl) { const predicateSchema = z.lazy(() => z.union([ comparableValueSchema, - z.object({ $and: z.array(predicateSchema) }), - z.object({ $or: z.array(predicateSchema) }), + z.object({ $all: z.array(predicateSchema) }), + z.object({ $any: z.array(predicateSchema) }), z.object({ $not: predicateSchema }), z.record(z.string().regex(/^(?!\$).*$/), valuePredicateSchema), ]), diff --git a/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.test.ts b/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.test.ts index 841056d01e..6ce23b1c74 100644 --- a/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.test.ts +++ b/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.test.ts @@ -132,7 +132,7 @@ describe('evaluateEntityPredicate', () => { [ 's', { - $and: [ + $all: [ { 'metadata.tags': { $contains: 'java' } }, { 'metadata.tags': { $contains: 'spring' } }, ], @@ -150,14 +150,17 @@ describe('evaluateEntityPredicate', () => { [ 's,g', { - $or: [{ kind: 'component', 'spec.type': 'service' }, { kind: 'group' }], + $any: [ + { kind: 'component', 'spec.type': 'service' }, + { kind: 'group' }, + ], }, ], [ 'w,a', { $not: { - $or: [ + $any: [ { kind: 'component', 'spec.type': 'service' }, { kind: 'group' }, ], @@ -186,16 +189,16 @@ describe('evaluateEntityPredicate', () => { [ 's,w,a', { - $or: [ + $any: [ { - $and: [ + $all: [ { kind: 'component', 'spec.type': { $in: ['service', 'website'] }, }, ], }, - { $and: [{ kind: 'api', 'spec.type': 'grpc' }] }, + { $all: [{ kind: 'api', 'spec.type': 'grpc' }] }, ], }, ], @@ -203,7 +206,7 @@ describe('evaluateEntityPredicate', () => { [ 'w', { - $and: [ + $all: [ { kind: 'component' }, { $not: { 'spec.type': { $in: ['service'] } } }, ], diff --git a/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.ts b/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.ts index 7bd301a889..cf33457376 100644 --- a/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.ts +++ b/plugins/catalog-react/src/alpha/predicates/evaluateEntityPredicate.ts @@ -44,11 +44,11 @@ export function evaluateEntityPredicate( return valuesAreEqual(value, filter); } - if ('$and' in filter) { - return filter.$and.every(f => evaluateEntityPredicate(f, value)); + if ('$all' in filter) { + return filter.$all.every(f => evaluateEntityPredicate(f, value)); } - if ('$or' in filter) { - return filter.$or.some(f => evaluateEntityPredicate(f, value)); + if ('$any' in filter) { + return filter.$any.some(f => evaluateEntityPredicate(f, value)); } if ('$not' in filter) { return !evaluateEntityPredicate(filter.$not, value); diff --git a/plugins/catalog-react/src/alpha/predicates/types.ts b/plugins/catalog-react/src/alpha/predicates/types.ts index b20d90c4e5..a4a2e9199f 100644 --- a/plugins/catalog-react/src/alpha/predicates/types.ts +++ b/plugins/catalog-react/src/alpha/predicates/types.ts @@ -18,8 +18,8 @@ export type EntityPredicate = | EntityPredicateExpression | EntityPredicatePrimitive - | { $and: EntityPredicate[] } - | { $or: EntityPredicate[] } + | { $all: EntityPredicate[] } + | { $any: EntityPredicate[] } | { $not: EntityPredicate }; /** @alpha */