feat(cdp): allow substring without length param (#28746)

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Marcus Hof
2025-03-03 12:37:14 +01:00
committed by GitHub
parent 3525d9688a
commit eee73a9ec8
8 changed files with 33 additions and 25 deletions

1
.cursorignore Normal file
View File

@@ -0,0 +1 @@
.env

View File

@@ -44,22 +44,23 @@
"print", 1, 35, 32, "-- string/array --", 2, "print", 1, 35, 32, "a", 32, "a", 32, "b", 32, "c", 2, "tuple", 3, 2, "in",
2, 32, "z", 32, "a", 32, "b", 32, "c", 2, "tuple", 3, 2, "in", 2, 2, "print", 2, 35, 32, "a", 32, "a", 32, "b", 32, "c",
43, 3, 2, "in", 2, 32, "z", 32, "a", 32, "b", 32, "c", 43, 3, 2, "in", 2, 2, "print", 2, 35, 32, "hello", 32, "he", 2,
"startsWith", 2, 32, "abcdef", 33, 2, 33, 3, 2, "substring", 3, 2, "print", 2, 35, 31, 31, 32, "firstNonNull", 2,
"coalesce", 3, 32, "notNull", 2, "assumeNotNull", 1, 2, "print", 2, 35, 32, "", 2, "print", 1, 35, 32, "-- date --", 2,
"print", 1, 35, 32, "2024-12-18T00:00:00Z", 2, "toDateTime", 1, 2, "toYear", 1, 32, "2024-12-18T00:00:00Z", 2,
"toDateTime", 1, 2, "toMonth", 1, 2, "print", 2, 35, 2, "now", 0, 2, "typeof", 1, 2, "print", 1, 35, 32,
"2024-12-18T11:11:11Z", 2, "toDateTime", 1, 2, "toStartOfDay", 1, 32, "2024-12-18T11:11:11Z", 2, "toDateTime", 1, 2,
"toStartOfWeek", 1, 2, "print", 2, 35, 32, "2024-12-18T00:00:00Z", 2, "toDateTime", 1, 2, "toYYYYMM", 1, 2, "print", 1,
35, 32, "day", 33, 1, 32, "2024-12-18", 2, "toDate", 1, 2, "dateAdd", 3, 32, "day", 32, "2024-12-18", 2, "toDate", 1,
32, "day", 33, 5, 32, "2024-12-18", 2, "toDate", 1, 2, "dateAdd", 3, 2, "dateDiff", 3, 2, "print", 2, 35, 32, "day", 32,
"2024-12-18T12:34:56Z", 2, "toDateTime", 1, 2, "dateTrunc", 2, 2, "print", 1, 35, 32, "2024-12-18", 2, "toDate", 1, 33,
3, 2, "addDays", 2, 2, "print", 1, 35, 33, 5, 2, "toIntervalDay", 1, 33, 2, 2, "toIntervalMonth", 1, 2, "print", 2, 35,
2, "today", 0, 2, "typeof", 1, 2, "print", 1, 35, 32, "", 2, "print", 1, 35, 32, "-- json --", 2, "print", 1, 35, 32,
"{\"a\":123.1}", 32, "a", 2, "JSONExtractInt", 2, 2, "jsonStringify", 1, 32, "{\"a\":\"hello\"}", 32, "a", 2,
"JSONExtractInt", 2, 2, "jsonStringify", 1, 2, "print", 2, 35, 32, "{\"a\":123.1}", 32, "a", 2, "JSONExtractFloat", 2,
2, "jsonStringify", 1, 32, "{\"a\":\"hello\"}", 32, "a", 2, "JSONExtractFloat", 2, 2, "jsonStringify", 1, 2, "print", 2,
35, 32, "{\"a\":123.1}", 32, "a", 2, "JSONExtractString", 2, 2, "jsonStringify", 1, 32, "{\"a\":\"hello\"}", 32, "a", 2,
"JSONExtractString", 2, 2, "jsonStringify", 1, 2, "print", 2, 35, 32, "{\"a\":123}", 32, "a", 2, "JSONExtractArrayRaw",
2, 2, "jsonStringify", 1, 32, "{\"a\":\"hello\"}", 32, "a", 2, "JSONExtractArrayRaw", 2, 2, "jsonStringify", 1, 2,
"print", 2, 35, 32, "{\"a\":[]}", 32, "a", 2, "JSONExtractArrayRaw", 2, 2, "jsonStringify", 1, 32,
"{\"a\":[\"hello\"]}", 32, "a", 2, "JSONExtractArrayRaw", 2, 2, "jsonStringify", 1, 2, "print", 2, 35]
"startsWith", 2, 32, "abcdef", 33, 2, 33, 3, 2, "substring", 3, 2, "print", 2, 35, 32, "abcdef", 33, 2, 2, "substring",
2, 2, "print", 1, 35, 31, 31, 32, "firstNonNull", 2, "coalesce", 3, 32, "notNull", 2, "assumeNotNull", 1, 2, "print", 2,
35, 32, "", 2, "print", 1, 35, 32, "-- date --", 2, "print", 1, 35, 32, "2024-12-18T00:00:00Z", 2, "toDateTime", 1, 2,
"toYear", 1, 32, "2024-12-18T00:00:00Z", 2, "toDateTime", 1, 2, "toMonth", 1, 2, "print", 2, 35, 2, "now", 0, 2,
"typeof", 1, 2, "print", 1, 35, 32, "2024-12-18T11:11:11Z", 2, "toDateTime", 1, 2, "toStartOfDay", 1, 32,
"2024-12-18T11:11:11Z", 2, "toDateTime", 1, 2, "toStartOfWeek", 1, 2, "print", 2, 35, 32, "2024-12-18T00:00:00Z", 2,
"toDateTime", 1, 2, "toYYYYMM", 1, 2, "print", 1, 35, 32, "day", 33, 1, 32, "2024-12-18", 2, "toDate", 1, 2, "dateAdd",
3, 32, "day", 32, "2024-12-18", 2, "toDate", 1, 32, "day", 33, 5, 32, "2024-12-18", 2, "toDate", 1, 2, "dateAdd", 3, 2,
"dateDiff", 3, 2, "print", 2, 35, 32, "day", 32, "2024-12-18T12:34:56Z", 2, "toDateTime", 1, 2, "dateTrunc", 2, 2,
"print", 1, 35, 32, "2024-12-18", 2, "toDate", 1, 33, 3, 2, "addDays", 2, 2, "print", 1, 35, 33, 5, 2, "toIntervalDay",
1, 33, 2, 2, "toIntervalMonth", 1, 2, "print", 2, 35, 2, "today", 0, 2, "typeof", 1, 2, "print", 1, 35, 32, "", 2,
"print", 1, 35, 32, "-- json --", 2, "print", 1, 35, 32, "{\"a\":123.1}", 32, "a", 2, "JSONExtractInt", 2, 2,
"jsonStringify", 1, 32, "{\"a\":\"hello\"}", 32, "a", 2, "JSONExtractInt", 2, 2, "jsonStringify", 1, 2, "print", 2, 35,
32, "{\"a\":123.1}", 32, "a", 2, "JSONExtractFloat", 2, 2, "jsonStringify", 1, 32, "{\"a\":\"hello\"}", 32, "a", 2,
"JSONExtractFloat", 2, 2, "jsonStringify", 1, 2, "print", 2, 35, 32, "{\"a\":123.1}", 32, "a", 2, "JSONExtractString",
2, 2, "jsonStringify", 1, 32, "{\"a\":\"hello\"}", 32, "a", 2, "JSONExtractString", 2, 2, "jsonStringify", 1, 2,
"print", 2, 35, 32, "{\"a\":123}", 32, "a", 2, "JSONExtractArrayRaw", 2, 2, "jsonStringify", 1, 32, "{\"a\":\"hello\"}",
32, "a", 2, "JSONExtractArrayRaw", 2, 2, "jsonStringify", 1, 2, "print", 2, 35, 32, "{\"a\":[]}", 32, "a", 2,
"JSONExtractArrayRaw", 2, 2, "jsonStringify", 1, 32, "{\"a\":[\"hello\"]}", 32, "a", 2, "JSONExtractArrayRaw", 2, 2,
"jsonStringify", 1, 2, "print", 2, 35]

View File

@@ -61,9 +61,10 @@ function toIntervalMonth(val) { return __toHogInterval(val, 'month') }
function toIntervalDay(val) { return __toHogInterval(val, 'day') }
function toDateTime (input, zone) { return __toDateTime(input, zone) }
function toDate (input) { return __toDate(input) }
function substring(s, start, length) {
function substring(s, start, optionalLength) {
if (typeof s !== 'string') return '';
const startIdx = start - 1;
const length = typeof optionalLength === 'number' ? optionalLength : s.length - startIdx;
if (startIdx < 0 || length < 0) return '';
const endIdx = startIdx + length;
return startIdx < s.length ? s.slice(startIdx, endIdx) : '';
@@ -531,6 +532,7 @@ print("-- string/array --");
print(__x_in("a", tuple("a", "b", "c")), __x_in("z", tuple("a", "b", "c")));
print(__x_in("a", ["a", "b", "c"]), __x_in("z", ["a", "b", "c"]));
print(startsWith("hello", "he"), substring("abcdef", 2, 3));
print(substring("abcdef", 2));
print(coalesce(null, null, "firstNonNull"), assumeNotNull("notNull"));
print("");
print("-- date --");

View File

@@ -89,6 +89,7 @@ one
true false
true false
true bcd
bcdef
firstNonNull notNull
-- date --

View File

@@ -89,6 +89,7 @@ print('-- string/array --')
print(in('a', tuple('a','b','c')), in('z', tuple('a','b','c')))
print(in('a', ['a','b','c']), in('z', ['a','b','c']))
print(startsWith('hello','he'), substring('abcdef',2,3))
print(substring('abcdef',2))
print(coalesce(null, null, 'firstNonNull'), assumeNotNull('notNull'))
print('')
print('-- date --')

View File

@@ -718,10 +718,10 @@ def substring(args: list[Any], team: Optional["Team"], stdout: Optional[list[str
# start is 1-based.
s = args[0]
start = args[1]
length = args[2]
if not isinstance(s, str):
return ""
start_idx = start - 1
length = args[2] if len(args) > 2 else len(s) - start_idx
if start_idx < 0 or length < 0:
return ""
end_idx = start_idx + length
@@ -1060,7 +1060,7 @@ STL: dict[str, STLFunction] = {
"range": STLFunction(fn=range_fn, minArgs=1, maxArgs=2),
"round": STLFunction(fn=round_fn, minArgs=1, maxArgs=1),
"startsWith": STLFunction(fn=startsWith, minArgs=2, maxArgs=2),
"substring": STLFunction(fn=substring, minArgs=3, maxArgs=3),
"substring": STLFunction(fn=substring, minArgs=2, maxArgs=3),
"toIntervalDay": STLFunction(fn=toIntervalDay, minArgs=1, maxArgs=1),
"toIntervalHour": STLFunction(fn=toIntervalHour, minArgs=1, maxArgs=1),
"toIntervalMinute": STLFunction(fn=toIntervalMinute, minArgs=1, maxArgs=1),

View File

@@ -316,11 +316,12 @@ function startsWithFn([str, prefix]: any[]): boolean {
return typeof str === 'string' && typeof prefix === 'string' && str.startsWith(prefix)
}
function substringFn([s, start, length]: any[]): string {
function substringFn([s, start, optionalLength]: any[]): string {
if (typeof s !== 'string') {
return ''
}
const startIdx = start - 1
const length = typeof optionalLength === 'number' ? optionalLength : s.length - startIdx
if (startIdx < 0 || length < 0) {
return ''
}
@@ -1259,7 +1260,7 @@ export const STL: Record<string, STLFunction> = {
range: { fn: rangeFn, minArgs: 1, maxArgs: 2 },
round: { fn: roundFn, minArgs: 1, maxArgs: 1 },
startsWith: { fn: startsWithFn, minArgs: 2, maxArgs: 2 },
substring: { fn: substringFn, minArgs: 3, maxArgs: 3 },
substring: { fn: substringFn, minArgs: 2, maxArgs: 3 },
toIntervalDay: { fn: toIntervalDayFn, minArgs: 1, maxArgs: 1 },
toIntervalHour: { fn: toIntervalHourFn, minArgs: 1, maxArgs: 1 },
toIntervalMinute: { fn: toIntervalMinuteFn, minArgs: 1, maxArgs: 1 },

View File

@@ -1295,9 +1295,10 @@ function __setProperty(objectOrArray, key, value) {
[],
],
"substring": [
"""function substring(s, start, length) {
"""function substring(s, start, optionalLength) {
if (typeof s !== 'string') return '';
const startIdx = start - 1;
const length = typeof optionalLength === 'number' ? optionalLength : s.length - startIdx;
if (startIdx < 0 || length < 0) return '';
const endIdx = startIdx + length;
return startIdx < s.length ? s.slice(startIdx, endIdx) : '';