Skip to content
v2.273.0

Capture Class ​

Capture values while matching templates.

Using an instance of this class within a Matcher will capture the matching value. The as*() APIs on the instance can be used to get the captured value.

Example ​

ts
// Given a template -
// {
//   "Resources": {
//     "MyBar": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Flob",
//       }
//     },
//     "MyBaz": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Quib",
//       }
//     }
//   }
// }

const fredCapture = new Capture();
template.hasResourceProperties('Foo::Bar', {
  Fred: fredCapture,
});

fredCapture.asString(); // returns "Flob"
fredCapture.next();     // returns true
fredCapture.asString(); // returns "Quib"
python
# Given a template -
# {
#   "Resources": {
#     "MyBar": {
#       "Type": "Foo::Bar",
#       "Properties": {
#         "Fred": "Flob",
#       }
#     },
#     "MyBaz": {
#       "Type": "Foo::Bar",
#       "Properties": {
#         "Fred": "Quib",
#       }
#     }
#   }
# }

fred_capture = Capture()
template.has_resource_properties("Foo::Bar", {
    "Fred": fred_capture
})

fred_capture.as_string() # returns "Flob"
fred_capture.next() # returns true
fred_capture.as_string()
java
// Given a template -
// {
//   "Resources": {
//     "MyBar": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Flob",
//       }
//     },
//     "MyBaz": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Quib",
//       }
//     }
//   }
// }

Capture fredCapture = new Capture();
template.hasResourceProperties("Foo::Bar", Map.of(
        "Fred", fredCapture));

fredCapture.asString(); // returns "Flob"
fredCapture.next(); // returns true
fredCapture.asString();
csharp
// Install: dotnet add package Amazon.CDK.Lib
// Given a template -
// {
//   "Resources": {
//     "MyBar": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Flob",
//       }
//     },
//     "MyBaz": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Quib",
//       }
//     }
//   }
// }

var fredCapture = new Capture();
template.HasResourceProperties("Foo::Bar", new Dictionary<string, Capture> {
    { "Fred", fredCapture }
});

fredCapture.AsString(); // returns "Flob"
fredCapture.Next(); // returns true
fredCapture.AsString();
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// Given a template -
// {
//   "Resources": {
//     "MyBar": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Flob",
//       }
//     },
//     "MyBaz": {
//       "Type": "Foo::Bar",
//       "Properties": {
//         "Fred": "Quib",
//       }
//     }
//   }
// }

fredCapture := awscdk.NewCapture()
template.HasResourceProperties(jsii.String("Foo::Bar"), map[string]Capture{
	"Fred": fredCapture,
})

fredCapture.AsString() // returns "Flob"
fredCapture.Next() // returns true
fredCapture.AsString()

Initializer ​

ts
new Capture(pattern?: any)
python
Capture(pattern: typing.Optional[Any] = None)
java
new Capture(Object pattern)
csharp
new Capture(object? pattern = null)
go
NewCapture(Pattern interface{})

Properties ​

namestringnamestrnameStringNamestringName*stringrequired#
A name for the matcher.

This is collected as part of the result and may be presented to the user.

Methods ​

asArray(): any[]def as_array(self, ) -> typing.List[Any]public List<Object> asArray()public object[] AsArray()func AsArray() *[]interface{}#
Retrieve the captured value as an array.

An error is generated if no value is captured or if the value is not an array.

asBoolean(): booleandef as_boolean(self, ) -> boolpublic Boolean asBoolean()public bool AsBoolean()func AsBoolean() *bool#
Retrieve the captured value as a boolean.

An error is generated if no value is captured or if the value is not a boolean.

asNumber(): numberdef as_number(self, ) -> int | floatpublic Number asNumber()public double AsNumber()func AsNumber() *float64#
Retrieve the captured value as a number.

An error is generated if no value is captured or if the value is not a number.

asObject(): { [key: string]: any }def as_object(self, ) -> typing.Mapping[str, Any]public Map<String, Object> asObject()public IDictionary<string, object> AsObject()func AsObject() *map[string]interface{}#
Retrieve the captured value as a JSON object.

An error is generated if no value is captured or if the value is not an object.

asString(): stringdef as_string(self, ) -> strpublic String asString()public string AsString()func AsString() *string#
Retrieve the captured value as a string.

An error is generated if no value is captured or if the value is not a string.

isMatcher(x: any): boolean@staticmethod def is_matcher(x: Any) -> boolpublic static Boolean isMatcher(Object x)public static bool IsMatcher(object x)func IsMatcher(X interface{}) *boolstatic#
Check whether the provided object is a subtype of the `IMatcher`.
test(actual: any): MatchResultdef test(self, actual: Any) -> MatchResultpublic MatchResult test(Object actual)public MatchResult Test(object actual)func Test(Actual interface{}) MatchResult#
Test whether a target matches the provided pattern.

Every Matcher must implement this method. This method will be invoked by the assertions framework. Do not call this method directly.