Skip to content

Commit c60ccec

Browse files
committed
fix filter in list functions
1 parent 928593e commit c60ccec

File tree

3 files changed

+74
-29
lines changed

3 files changed

+74
-29
lines changed

governance/third-generation/common-functions/tfconfig-functions/docs/filter_attribute_in_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This function returns a map with two maps, `items` and `messages`. The `items` m
2525
This function prints the violation messages if the parameter, `prtmsg`, was set to `true`. Otherwise, it does not print anything.
2626

2727
## Examples
28-
Here are some examples of calling this function, assuming that the tfconfig-functions.sentinel file that contains it has been imported with the alias `plan`:
28+
Here are some examples of calling this function, assuming that the tfconfig-functions.sentinel file that contains it has been imported with the alias `config`:
2929
```
3030
violatingProviders = config.filter_attribute_in_list(allProviders,
3131
"name", prohibited_list, false)

governance/third-generation/common-functions/tfconfig-functions/docs/filter_attribute_not_in_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This function returns a map with two maps, `items` and `messages`. The `items` m
2525
This function prints the violation messages if the parameter, `prtmsg`, was set to `true`. Otherwise, it does not print anything.
2626

2727
## Examples
28-
Here are some examples of calling this function, assuming that the tfconfig-functions.sentinel file that contains it has been imported with the alias `plan`:
28+
Here are some examples of calling this function, assuming that the tfconfig-functions.sentinel file that contains it has been imported with the alias `config`:
2929
```
3030
violatingProviders = config.filter_attribute_not_in_list(allProviders,
3131
"name", allowed_list, false)

governance/third-generation/common-functions/tfconfig-functions/tfconfig-functions.sentinel

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -330,25 +330,47 @@ filter_attribute_not_in_list = func(items, attr, allowed, prtmsg) {
330330
# Iterate over items
331331
for items as index, item {
332332
val = evaluate_attribute(item, attr) else null
333-
#val = item[attr] else null
333+
print("val:", val)
334334
# Check if the value is null
335335
if val is null {
336336
val = "null"
337337
}
338-
# Check if the value is in the forbidden list
339-
if val not in allowed {
340-
# Add the item and a warning message to the violators list
341-
message = to_string(index) + " has " + to_string(attr) + " with value " +
342-
to_string(val) + " that is not in the allowed list: " +
343-
to_string(allowed)
344-
violators[index] = item
345-
messages[index] = message
346-
if prtmsg {
347-
print(message)
348-
} // end if prtmsg
349-
} // end if forbidden
338+
# Process lists and maps
339+
if types.type_of(val) in ["list", "map"] {
340+
message = ""
341+
# Check each item of list or map
342+
for val as i, v {
343+
if v not in allowed {
344+
# Add the item and a warning message to the violators list
345+
message = to_string(index) + " has " + to_string(attr) + " with value " +
346+
to_string(v) + " that is not in the allowed list: " +
347+
to_string(allowed)
348+
}
349+
if message is not "" {
350+
# Add the item and warning message to the violators list
351+
violators[index] = item
352+
messages[index] = message
353+
if prtmsg {
354+
print(message)
355+
}
356+
} // end message not ""
357+
} // end for
358+
} else {
359+
# Process single item
360+
if val not in allowed {
361+
# Add the item and a warning message to the violators list
362+
message = to_string(index) + " has " + to_string(attr) +
363+
" with value " + to_string(val) +
364+
" that is not in the allowed list: " +
365+
to_string(allowed)
366+
violators[index] = item
367+
messages[index] = message
368+
if prtmsg {
369+
print(message)
370+
}
371+
} // end if single item not matches
372+
} // end single item
350373
} // end for items
351-
352374
return {"items":violators,"messages":messages}
353375
}
354376

@@ -365,23 +387,46 @@ filter_attribute_in_list = func(items, attr, forbidden, prtmsg) {
365387
# Iterate over items
366388
for items as index, item {
367389
val = evaluate_attribute(item, attr) else null
368-
#val = item[attr] else null
390+
print("val:", val)
369391
# Check if the value is null
370392
if val is null {
371393
val = "null"
372394
}
373-
# Check if the value is in the forbidden list
374-
if val in forbidden {
375-
# Add the item and a warning message to the violators list
376-
message = to_string(index) + " has " + to_string(attr) + " with value " +
377-
to_string(val) + " that is in the forbidden list: " +
378-
to_string(forbidden)
379-
violators[index] = item
380-
messages[index] = message
381-
if prtmsg {
382-
print(message)
383-
} // end if prtmsg
384-
} // end if forbidden
395+
# Process lists and maps
396+
if types.type_of(val) in ["list", "map"] {
397+
message = ""
398+
# Check each item of list or map
399+
for val as i, v {
400+
if v in forbidden {
401+
# Add the item and a warning message to the violators list
402+
message = to_string(index) + " has " + to_string(attr) + " with value " +
403+
to_string(v) + " that is in the forbidden list: " +
404+
to_string(forbidden)
405+
}
406+
if message is not "" {
407+
# Add the item and warning message to the violators list
408+
violators[index] = item
409+
messages[index] = message
410+
if prtmsg {
411+
print(message)
412+
}
413+
} // end message not ""
414+
} // end for
415+
} else {
416+
# Process single item
417+
if val in forbidden {
418+
# Add the item and a warning message to the violators list
419+
message = to_string(index) + " has " + to_string(attr) +
420+
" with value " + to_string(val) +
421+
" that is in the forbidden list: " +
422+
to_string(forbidden)
423+
violators[index] = item
424+
messages[index] = message
425+
if prtmsg {
426+
print(message)
427+
}
428+
} // end if single item not matches
429+
} // end single item
385430
} // end for items
386431

387432
return {"items":violators,"messages":messages}

0 commit comments

Comments
 (0)