Skip to content

Commit 850e26b

Browse files
feat: Remove all optional parameters with null value
* feat: Remove all optional parameters with null value * test: Add unit tests to verify that the transport removes all optional parameters with null value
1 parent c81507a commit 850e26b

File tree

6 files changed

+438
-21
lines changed

6 files changed

+438
-21
lines changed

.github/workflows/check-pr.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ jobs:
150150
REPO_NAME="firebolt-apis"
151151
RUN_ID="${{ env.CPP_RUN_ID }}"
152152
MAX_POLLS=30 # Max polls before timeout
153-
POLL_INTERVAL=50 # In seconds
153+
POLL_INTERVAL=180 # In seconds
154154
155155
156156
# Poll the specific run ID for status
@@ -173,7 +173,7 @@ jobs:
173173
done
174174
175175
if [ "$i" -eq "$MAX_POLLS" ]; then
176-
echo "Timeout reached while waiting for JavaScript SDK generation."
176+
echo "Timeout reached while waiting for CPP SDK generation."
177177
exit 1
178178
fi
179179

languages/javascript/src/shared/Transport/index.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ export default class Transport {
103103
return Transport.get()._sendAndGetId(module, method, params, transforms)
104104
}
105105

106+
static removeNullOptionalParams(params, numOfOptionalParams) {
107+
108+
// Iterate over all params starting backwrods and if the param is null remove it from the params object.
109+
// If it is undefined that means the param is not provided, which is Ok.
110+
// We should not get a call with numOfOptionalParams === 0, but if we do, just return the params as are.
111+
const keys = Object.keys(params)
112+
let paramsIndex = keys.length - 1
113+
while (paramsIndex >= 0 && numOfOptionalParams > 0) {
114+
const key = keys[paramsIndex]
115+
if (params[key] === null) {
116+
delete params[key]
117+
console.warn('WARNING: null values for optional params will be disallowed in a future Firebolt version. Parameter: ' + key)
118+
} else if (params[key] === undefined) {
119+
// undefined means the param is not provided, we should continue.
120+
} else {
121+
// if an optional param is provided we should stop removing params as if we continue we will change the order of the params
122+
break
123+
}
124+
paramsIndex--
125+
numOfOptionalParams--
126+
}
127+
return params
128+
}
106129
_send (module, method, params, transforms) {
107130
if (Array.isArray(module) && !method && !params) {
108131
return this._batch(module)

languages/javascript/templates/methods/default.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@ function ${method.name}(${method.params.list}) {
33

44
const transforms = ${method.transforms}
55

6-
return Transport.send('${info.title}', '${method.name}', { ${method.params.list} }, transforms)
6+
let params = { ${method.params.list} }${if.optionalParams}
7+
8+
// remove the null params if they are optional
9+
params = Transport.removeNullOptionalParams(params, ${optionalParams})${end.if.optionalParams}
10+
11+
return Transport.send('${info.title}', '${method.name}', params, transforms)
712
}

src/macrofier/engine.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,18 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
14271427
signature = ''
14281428
}
14291429

1430+
//callculate the number of optional params
1431+
let optionalParams = 0
1432+
if (methodObj.params && methodObj.params.length) {
1433+
// Count the number of optional parameters starting from the last and stop when a required parameter is found
1434+
for (let i = methodObj.params.length - 1; i >= 0; i--) {
1435+
if (methodObj.params[i].required) {
1436+
break; // Stop counting when a required parameter is found
1437+
}
1438+
optionalParams++;
1439+
}
1440+
}
1441+
14301442
template = insertExampleMacros(template, examples[methodObj.name] || [], methodObj, json, templates)
14311443
template = template.replace(/\$\{method\.name\}/g, method.name)
14321444
.replace(/\$\{method\.rpc\.name\}/g, methodObj.rpc_name || methodObj.name)
@@ -1441,6 +1453,8 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
14411453
.replace(/\$\{method\.params\.list\}/g, method.params)
14421454
.replace(/\$\{method\.params\.array\}/g, JSON.stringify(methodObj.params.map(p => p.name)))
14431455
.replace(/\$\{method\.params\.count}/g, methodObj.params ? methodObj.params.length : 0)
1456+
.replace(/\$\{if\.optionalParams\}(.*?)\$\{end\.if\.optionalParams\}/gms, optionalParams > 0 ? '$1' : '')
1457+
.replace(/\$\{optionalParams\}/g, optionalParams)
14441458
.replace(/\$\{if\.params\}(.*?)\$\{end\.if\.params\}/gms, method.params.length ? '$1' : '')
14451459
.replace(/\$\{if\.result\}(.*?)\$\{end\.if\.result\}/gms, resultType ? '$1' : '')
14461460
.replace(/\$\{if\.result.nonvoid\}(.*?)\$\{end\.if\.result.nonvoid\}/gms, resultType && resultType !== 'void' ? '$1' : '')

0 commit comments

Comments
 (0)