ondc-code-generator 0.6.21 → 0.6.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generator/generators/python/py-generator.js +1 -1
- package/dist/generator/generators/python/templates/json-path-utils.mustache +16 -1
- package/dist/generator/generators/python/templates/storage-templates/api-save.mustache +1 -1
- package/dist/generator/generators/typescript/templates/json-path-utils.mustache +25 -7
- package/dist/generator/generators/typescript/templates/storage-templates/api-save.mustache +1 -1
- package/dist/generator/generators/typescript/ts-generator.js +1 -1
- package/package.json +2 -2
- package/test-python-session.js +0 -0
|
@@ -194,7 +194,7 @@ export class PythonGenerator extends CodeGenerator {
|
|
|
194
194
|
for (const name of varNames) {
|
|
195
195
|
const value = testObject[name];
|
|
196
196
|
const final = typeof value === "string"
|
|
197
|
-
? `payload_utils["get_json_path"](${testObject[TestObjectSyntax.Name]}_obj, "${value}")`
|
|
197
|
+
? `payload_utils["get_json_path"](${testObject[TestObjectSyntax.Name]}_obj, "${value}", True)`
|
|
198
198
|
: this.convertArrayToStringPython(value);
|
|
199
199
|
variables.push({
|
|
200
200
|
name: name,
|
|
@@ -8,13 +8,24 @@ def is_list_of_strings_or_none(value: Any) -> bool:
|
|
|
8
8
|
and all((v is None) or isinstance(v, str) for v in value)
|
|
9
9
|
)
|
|
10
10
|
|
|
11
|
-
def
|
|
11
|
+
def flatten_list(lst: List[Any]) -> List[Any]:
|
|
12
|
+
"""Recursively flatten a nested list"""
|
|
13
|
+
result = []
|
|
14
|
+
for item in lst:
|
|
15
|
+
if isinstance(item, list):
|
|
16
|
+
result.extend(flatten_list(item))
|
|
17
|
+
else:
|
|
18
|
+
result.append(item)
|
|
19
|
+
return result
|
|
20
|
+
|
|
21
|
+
def get_json_path(payload: Any, path: str, flatten_result: bool = False) -> List[Any]:
|
|
12
22
|
"""
|
|
13
23
|
Evaluate a JSONPath against `payload`.
|
|
14
24
|
|
|
15
25
|
- If the result is a list that contains only strings and/or None,
|
|
16
26
|
convert None -> "null".
|
|
17
27
|
- Return [] when there are no matches.
|
|
28
|
+
- If flatten_result is True, flatten nested arrays completely.
|
|
18
29
|
"""
|
|
19
30
|
expr = parse(path)
|
|
20
31
|
matches = [m.value for m in expr.find(payload)] # extract raw values
|
|
@@ -22,6 +33,10 @@ def get_json_path(payload: Any, path: str) -> List[Any]:
|
|
|
22
33
|
if is_list_of_strings_or_none(matches):
|
|
23
34
|
matches = ["null" if v is None else v for v in matches]
|
|
24
35
|
|
|
36
|
+
# Flatten only if flag is true
|
|
37
|
+
if flatten_result:
|
|
38
|
+
matches = flatten_list(matches)
|
|
39
|
+
|
|
25
40
|
# Explicitly mirror the TS return of [] for no matches
|
|
26
41
|
return matches if len(matches) > 0 else []
|
|
27
42
|
|
|
@@ -21,7 +21,7 @@ async def save_function(payload: dict, unique_prefix: str, key: str, path: str,
|
|
|
21
21
|
if path == "" or key == "_SELF":
|
|
22
22
|
return
|
|
23
23
|
|
|
24
|
-
value = get_json_path(payload, path)
|
|
24
|
+
value = get_json_path(payload, path, True)
|
|
25
25
|
data = {
|
|
26
26
|
"stored_from": "{{action}}_action",
|
|
27
27
|
"key_path": path,
|
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
import jsonpath from "jsonpath";
|
|
2
|
-
|
|
3
|
-
let output = jsonpath.query(payload, path);
|
|
4
|
-
if (isListOfStringsOrNull(output)) {
|
|
5
|
-
output = output.map((o) => (o === null ? "null" : o));
|
|
6
|
-
}
|
|
7
|
-
return output.length === 0 ? [] : output;
|
|
8
|
-
}
|
|
2
|
+
|
|
9
3
|
function isListOfStringsOrNull(variable: any): boolean {
|
|
10
4
|
return (
|
|
11
5
|
Array.isArray(variable) &&
|
|
12
6
|
variable.every((item) => item === null || typeof item === "string")
|
|
13
7
|
);
|
|
14
8
|
}
|
|
9
|
+
|
|
10
|
+
function getJsonPath(
|
|
11
|
+
payload: any,
|
|
12
|
+
path: string,
|
|
13
|
+
flattenResult: boolean = false
|
|
14
|
+
) {
|
|
15
|
+
let output = jsonpath.query(payload, path);
|
|
16
|
+
|
|
17
|
+
if (isListOfStringsOrNull(output)) {
|
|
18
|
+
output = output.map((o) => {
|
|
19
|
+
if (o === null) return "null";
|
|
20
|
+
if (o === null) return "null";
|
|
21
|
+
return o;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// flatten only if flag is true
|
|
26
|
+
if (flattenResult) {
|
|
27
|
+
output = output.flat(Infinity);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return output.length === 0 ? [] : output;
|
|
31
|
+
}
|
|
32
|
+
|
|
15
33
|
export default {
|
|
16
34
|
getJsonPath,
|
|
17
35
|
};
|
|
@@ -30,7 +30,7 @@ config: StorageConfig): Promise<void> {
|
|
|
30
30
|
if(path === "" || key === "_SELF"){
|
|
31
31
|
return;
|
|
32
32
|
}
|
|
33
|
-
const value = payloadUtils.getJsonPath(payload, path);
|
|
33
|
+
const value = payloadUtils.getJsonPath(payload, path,true);
|
|
34
34
|
const data = {
|
|
35
35
|
stored_from: "{{action}}_action",
|
|
36
36
|
key_path: path,
|
|
@@ -182,7 +182,7 @@ export class TypescriptGenerator extends CodeGenerator {
|
|
|
182
182
|
for (const name of varNames) {
|
|
183
183
|
const value = testObject[name];
|
|
184
184
|
const final = typeof value === "string"
|
|
185
|
-
? `payloadUtils.getJsonPath(testObj, "${value}")`
|
|
185
|
+
? `payloadUtils.getJsonPath(testObj, "${value}",true)`
|
|
186
186
|
: ConvertArrayToString(value);
|
|
187
187
|
variables.push({
|
|
188
188
|
name: name,
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ondc-code-generator",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.22",
|
|
4
4
|
"description": "generate code from build.yaml ",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc && copyfiles -u 1 \"src/**/*.{yaml,html,css,mustache}\" dist/",
|
|
9
9
|
"start": "node ./dist/index.js",
|
|
10
|
-
"test": "npm run build && node ./dist/test.js",
|
|
10
|
+
"test": "npm run build && node ./dist/index.test.js",
|
|
11
11
|
"dev": "nodemon",
|
|
12
12
|
"clean": "rm -rf dist",
|
|
13
13
|
"prepare": "npm run clean && npm run build"
|
|
File without changes
|