Skip to content

Commit d3fdb2e

Browse files
committed
[Streams] [Streamlang] Add base / core types (#226668)
## Summary Implements #224421. Adds various base / core types for Streamlang. As mentioned in the issue this will not (yet) be a perfect representation of the end product (e.g. processor types will eventually be reduced down to a schema representation that fits the support matrix, types will change with the transpilation code / extension of the conditions code etc), however this is a best effort at keeping PR sizes down. (cherry picked from commit b20ba3e)
1 parent 9b8729d commit d3fdb2e

6 files changed

Lines changed: 595 additions & 2 deletions

File tree

x-pack/platform/packages/shared/kbn-streamlang/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313
"exclude": [
1414
"target/**/*"
1515
],
16-
"kbn_references": []
16+
"kbn_references": [
17+
"@kbn/zod",
18+
"@kbn/zod-helpers",
19+
"@kbn/streams-schema",
20+
]
1721
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { z } from '@kbn/zod';
9+
import { NonEmptyString } from '@kbn/zod-helpers';
10+
import { createIsNarrowSchema } from '@kbn/streams-schema';
11+
12+
const stringOrNumberOrBoolean = z.union([z.string(), z.number(), z.boolean()]);
13+
14+
export interface ShorthandBinaryFilterCondition {
15+
field: string;
16+
eq?: string | number | boolean;
17+
neq?: string | number | boolean;
18+
lt?: string | number | boolean;
19+
lte?: string | number | boolean;
20+
gt?: string | number | boolean;
21+
gte?: string | number | boolean;
22+
contains?: string | number | boolean;
23+
startsWith?: string | number | boolean;
24+
endsWith?: string | number | boolean;
25+
}
26+
27+
// Shorthand binary: field + one of the operator keys
28+
export const shorthandBinaryFilterConditionSchema = z
29+
.object({
30+
field: NonEmptyString,
31+
eq: stringOrNumberOrBoolean.optional(),
32+
neq: stringOrNumberOrBoolean.optional(),
33+
lt: stringOrNumberOrBoolean.optional(),
34+
lte: stringOrNumberOrBoolean.optional(),
35+
gt: stringOrNumberOrBoolean.optional(),
36+
gte: stringOrNumberOrBoolean.optional(),
37+
contains: stringOrNumberOrBoolean.optional(),
38+
startsWith: stringOrNumberOrBoolean.optional(),
39+
endsWith: stringOrNumberOrBoolean.optional(),
40+
})
41+
.refine(
42+
(obj) =>
43+
// At least one operator must be present
44+
Object.keys(obj).some((key) =>
45+
['eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'contains', 'startsWith', 'endsWith'].includes(key)
46+
),
47+
{ message: 'At least one operator must be specified' }
48+
);
49+
50+
export interface ShorthandUnaryFilterCondition {
51+
field: string;
52+
exists?: true;
53+
notExists?: true;
54+
}
55+
56+
// Shorthand unary: field + exists or notExists
57+
export const shorthandUnaryFilterConditionSchema = z
58+
.object({
59+
field: NonEmptyString,
60+
exists: z.literal(true).optional(),
61+
notExists: z.literal(true).optional(),
62+
})
63+
.refine((obj) => obj.exists === true || obj.notExists === true, {
64+
message: 'Must specify exists: true or notExists: true',
65+
});
66+
67+
export type FilterCondition = ShorthandBinaryFilterCondition | ShorthandUnaryFilterCondition;
68+
69+
export const filterConditionSchema = z.union([
70+
shorthandBinaryFilterConditionSchema,
71+
shorthandUnaryFilterConditionSchema,
72+
]);
73+
74+
export interface AndCondition {
75+
and: Condition[];
76+
}
77+
78+
export interface OrCondition {
79+
or: Condition[];
80+
}
81+
82+
export interface AlwaysCondition {
83+
always: {};
84+
}
85+
86+
export interface NeverCondition {
87+
never: {};
88+
}
89+
90+
export type Condition =
91+
| FilterCondition
92+
| AndCondition
93+
| OrCondition
94+
| NeverCondition
95+
| AlwaysCondition;
96+
97+
export const conditionSchema: z.Schema<Condition> = z.lazy(() =>
98+
z.union([
99+
filterConditionSchema,
100+
andConditionSchema,
101+
orConditionSchema,
102+
neverConditionSchema,
103+
alwaysConditionSchema,
104+
])
105+
);
106+
107+
export const andConditionSchema = z.object({ and: z.array(conditionSchema) });
108+
export const orConditionSchema = z.object({ or: z.array(conditionSchema) });
109+
export const neverConditionSchema = z.object({ never: z.strictObject({}) });
110+
export const alwaysConditionSchema = z.object({ always: z.strictObject({}) });
111+
112+
export const isBinaryFilterCondition = createIsNarrowSchema(
113+
conditionSchema,
114+
shorthandBinaryFilterConditionSchema
115+
);
116+
export const isUnaryFilterCondition = createIsNarrowSchema(
117+
conditionSchema,
118+
shorthandUnaryFilterConditionSchema
119+
);
120+
export const isFilterCondition = createIsNarrowSchema(conditionSchema, filterConditionSchema);
121+
122+
export const isAndCondition = createIsNarrowSchema(conditionSchema, andConditionSchema);
123+
export const isOrCondition = createIsNarrowSchema(conditionSchema, orConditionSchema);
124+
export const isNeverCondition = createIsNarrowSchema(conditionSchema, neverConditionSchema);
125+
export const isAlwaysCondition = createIsNarrowSchema(conditionSchema, alwaysConditionSchema);
126+
127+
export const isCondition = createIsNarrowSchema(z.unknown(), conditionSchema);

0 commit comments

Comments
 (0)