contains_key
Check if map contains key
* Boolean contains_key(Map[P, Y], P)
* Boolean contains_key(Object, String)
* Boolean contains_key(Map[String, Y]|Struct|Object, Array[String])
Given a key-value type collection (Map, Struct, or Object) and a key, tests whether the collection contains an entry with the given key.
This function has three variants:
Boolean contains_key(Map[P, Y], P): Tests whether theMaphas an entry with the given key. IfPis an optional type (e.g.,String?), then the second argument may beNone.Boolean contains_key(Object, String): Tests whether theObjecthas an entry with the given name.Boolean contains_key(Map[String, Y]|Struct|Object, Array[String]): Tests recursively for the presence of a compound key within a nested collection.
For the third variant, the first argument is a collection that may be nested to any level, i.e., contain values that are collections, which themselves may contain collections, and so on. The second argument is an array of keys that are resolved recursively. If the value associated with any except the last key in the array is None or not a collection type, this function returns false.
For example, if the first argument is a Map[String, Map[String, Int]] and the second argument is ["foo", "bar"], then the outer Map is tested for the presence of key "foo", and if it is present, then its value is tested for the presence of key "bar". This only tests for the presence of the named element, not whether or not it is defined.
Parameters
Map[P, Y]|Struct|Object: Collection to search for the key.P|Array[String]: The key to search. If the first argument is aMap, then the key must be of the same type as theMap's key type. If theMap's key type is optional then the key may also be optional. If the first argument is aMap[String, Y],Struct, orObject, then the key may be either aStringorArray[String].
Returns: true if the collection contains the key, otherwise false.
Example
Example: test_contains_key.wdl
version 1.2
struct Person {
String name
Map[String, String] details
}
workflow test_contains_key {
input {
Map[String, Int] m
String key1
String key2
Person p1
Person p2
}
output {
Int? i1 = if contains_key(m, key1) then m[key1] else None
Int? i2 = if contains_key(m, key2) then m[key2] else None
String? phone1 = if contains_key(p1.details, "phone") then p1.details["phone"] else None
String? phone2 = if contains_key(p2.details, "phone") then p2.details["phone"] else None
}
}Example input:
{
"test_contains_key.m": {"a": 1, "b": 2},
"test_contains_key.key1": "a",
"test_contains_key.key2": "c",
"test_contains_key.p1": {
"name": "John",
"details": {
"phone": "123-456-7890"
}
},
"test_contains_key.p2": {
"name": "Agent X",
"details": {
}
}
}
Example output:
{
"test_contains_key.i1": 1,
"test_contains_key.i2": null,
"test_contains_key.phone1": "123-456-7890",
"test_contains_key.phone2": null
}