Skip to content

Commit 21937e6

Browse files
committed
python: address review comments
- rename `normalise` to `normalizePath` - factor out `lookupValueOrEmpty`
1 parent c0407ae commit 21937e6

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

shared/yaml/codeql/serverless/ServerLess.qll

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ module ServerLess<Input I> {
5858
result = mapping.lookup(property).(YamlScalar).getValue()
5959
}
6060

61+
/**
62+
* Gets the looked up value if it exists or
63+
* the empty string if it does not.
64+
*/
65+
pragma[inline]
66+
private string lookupValueOrEmpty(YamlMapping mapping, string property) {
67+
if exists(mapping.lookup(property))
68+
then result = mapping.lookup(property).(YamlScalar).getValue()
69+
else result = ""
70+
}
71+
6172
/**
6273
* Gets a string where an ending "/." is simplified to "/" (if it exists).
6374
*/
@@ -78,9 +89,13 @@ module ServerLess<Input I> {
7889

7990
/**
8091
* Gets a string suitable as part of a file path.
92+
*
93+
* Maps the empty string to the empty string.
8194
*/
8295
bindingset[base]
83-
private string normalise(string base) { result = removeLeadingDotSlash(removeTrailingDot(base)) }
96+
private string normalizePath(string base) {
97+
result = removeLeadingDotSlash(removeTrailingDot(base))
98+
}
8499

85100
/**
86101
* Holds if the `.yml` file `ymlFile` contains a serverless configuration from `framework` with
@@ -99,16 +114,8 @@ module ServerLess<Input I> {
99114
exists(YamlMapping properties | properties = resource.lookup("Properties") |
100115
(
101116
handler = lookupValue(properties, "Handler") and
102-
(
103-
if exists(properties.lookup("CodeUri"))
104-
then codeUri = normalise(lookupValue(properties, "CodeUri"))
105-
else codeUri = ""
106-
) and
107-
(
108-
if exists(properties.lookup("Runtime"))
109-
then runtime = lookupValue(properties, "Runtime")
110-
else runtime = ""
111-
)
117+
codeUri = normalizePath(lookupValueOrEmpty(properties, "CodeUri")) and
118+
runtime = lookupValueOrEmpty(properties, "Runtime")
112119
)
113120
)
114121
or
@@ -119,11 +126,7 @@ module ServerLess<Input I> {
119126
not exists(resource.getParentNode()) and
120127
handler = lookupValue(functions.getValue(_), "handler") and
121128
codeUri = "" and
122-
(
123-
if exists(functions.lookup("Runtime"))
124-
then runtime = lookupValue(functions, "Runtime")
125-
else runtime = ""
126-
)
129+
runtime = lookupValueOrEmpty(functions, "Runtime")
127130
)
128131
)
129132
}

0 commit comments

Comments
 (0)