catalog-react: rename $and to $all and $or to $any
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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),
|
||||
]),
|
||||
|
||||
@@ -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'] } } },
|
||||
],
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
export type EntityPredicate =
|
||||
| EntityPredicateExpression
|
||||
| EntityPredicatePrimitive
|
||||
| { $and: EntityPredicate[] }
|
||||
| { $or: EntityPredicate[] }
|
||||
| { $all: EntityPredicate[] }
|
||||
| { $any: EntityPredicate[] }
|
||||
| { $not: EntityPredicate };
|
||||
|
||||
/** @alpha */
|
||||
|
||||
Reference in New Issue
Block a user