88import java .util .HashMap ;
99import java .util .HashSet ;
1010import java .util .Map ;
11+ import java .util .Optional ;
1112import java .util .Set ;
1213import software .amazon .smithy .aws .traits .ArnReferenceTrait ;
1314import software .amazon .smithy .aws .traits .ServiceTrait ;
2122import software .amazon .smithy .model .shapes .ToShapeId ;
2223import software .amazon .smithy .model .traits .DocumentationTrait ;
2324import software .amazon .smithy .utils .MapUtils ;
24- import software .amazon .smithy .utils .OptionalUtils ;
2525import software .amazon .smithy .utils .SetUtils ;
2626import software .amazon .smithy .utils .StringUtils ;
2727
@@ -47,13 +47,7 @@ public ConditionKeysIndex(Model model) {
4747 if (service .hasTrait (DefineConditionKeysTrait .ID )) {
4848 DefineConditionKeysTrait trait = service .expectTrait (DefineConditionKeysTrait .class );
4949 for (Map .Entry <String , ConditionKeyDefinition > entry : trait .getConditionKeys ().entrySet ()) {
50- // If no colon is present, we infer that this condition key is for the
51- // current service and apply its ARN namespace.
52- String key = entry .getKey ();
53- if (!key .contains (":" )) {
54- key = arnNamespace + ":" + key ;
55- }
56- serviceKeys .put (key , entry .getValue ());
50+ serviceKeys .put (resolveFullConditionKey (service , entry .getKey ()), entry .getValue ());
5751 }
5852 }
5953 serviceConditionKeys .put (service .getId (), serviceKeys );
@@ -75,11 +69,20 @@ public static ConditionKeysIndex of(Model model) {
7569 return model .getKnowledge (ConditionKeysIndex .class , ConditionKeysIndex ::new );
7670 }
7771
72+ static String resolveFullConditionKey (ServiceShape service , String conditionKey ) {
73+ // If no colon is present, we infer that this condition key is for the
74+ // current service and apply its ARN namespace.
75+ if (conditionKey .contains (":" )) {
76+ return conditionKey ;
77+ }
78+ return service .expectTrait (ServiceTrait .class ).getArnNamespace () + ":" + conditionKey ;
79+ }
80+
7881 /**
79- * Get all of the explicit and inferred condition keys used in the entire service.
82+ * Get all the explicit and inferred condition keys used in the entire service.
8083 *
8184 * <p>The result does not include global condition keys like "aws:accountId".
82- * Use {@link #getConditionKeyNames} to find all of the condition keys used
85+ * Use {@link #getConditionKeyNames} to find all the condition keys used
8386 * but not necessarily defined for a service.
8487 *
8588 * @param service Service shape/shapeId to get.
@@ -90,21 +93,25 @@ public Map<String, ConditionKeyDefinition> getDefinedConditionKeys(ToShapeId ser
9093 }
9194
9295 /**
93- * Get all of the condition key names used in a service.
96+ * Get all the condition key names used in a service.
9497 *
9598 * @param service Service shape/shapeId use to scope the result.
9699 * @return Returns the conditions keys of the service or an empty map when not found.
97100 */
98101 public Set <String > getConditionKeyNames (ToShapeId service ) {
99- return resourceConditionKeys .getOrDefault (service .toShapeId (), MapUtils .of ())
100- .values ()
101- .stream ()
102- .flatMap (Set ::stream )
103- .collect (SetUtils .toUnmodifiableSet ());
102+ if (!resourceConditionKeys .containsKey (service .toShapeId ())) {
103+ return SetUtils .of ();
104+ }
105+
106+ Set <String > names = new HashSet <>();
107+ for (Set <String > resourceKeyNames : resourceConditionKeys .get (service .toShapeId ()).values ()) {
108+ names .addAll (resourceKeyNames );
109+ }
110+ return names ;
104111 }
105112
106113 /**
107- * Get all of the defined condition keys used in an operation or resource, including
114+ * Get all the defined condition keys used in an operation or resource, including
108115 * any inferred keys and keys inherited by parent resource bindings.
109116 *
110117 * @param service Service shape/shapeId use to scope the result.
@@ -119,11 +126,11 @@ public Set<String> getConditionKeyNames(ToShapeId service, ToShapeId resourceOrO
119126 }
120127
121128 /**
122- * Get all of the defined condition keys used in an operation or resource, including
129+ * Get all the defined condition keys used in an operation or resource, including
123130 * any inferred keys and keys inherited by parent resource bindings.
124131 *
125132 * <p>The result does not include global condition keys like "aws:accountId".
126- * Use {@link #getConditionKeyNames} to find all of the condition keys used
133+ * Use {@link #getConditionKeyNames} to find all the condition keys used
127134 * but not necessarily defined for a resource or operation.
128135 *
129136 * @param service Service shape/shapeId use to scope the result.
@@ -170,7 +177,9 @@ private void compute(
170177 definitions .addAll (parentDefinitions );
171178 }
172179 resourceConditionKeys .get (service .getId ()).put (subject .getId (), definitions );
173- subject .getTrait (ConditionKeysTrait .class ).ifPresent (trait -> definitions .addAll (trait .getValues ()));
180+ if (subject .hasTrait (ConditionKeysTrait .ID )) {
181+ definitions .addAll (subject .expectTrait (ConditionKeysTrait .class ).resolveConditionKeys (service ));
182+ }
174183
175184 // Continue recursing into resources and computing keys.
176185 subject .asResourceShape ().ifPresent (resource -> {
@@ -183,18 +192,23 @@ private void compute(
183192 : MapUtils .of ();
184193
185194 // Compute the keys of each child operation, passing no keys.
186- resource .getAllOperations ()
187- .stream ()
188- .flatMap (id -> OptionalUtils .stream (model .getShape (id )))
189- .forEach (child -> compute (model , service , arnRoot , child , resource ));
195+ for (ShapeId operationId : resource .getOperations ()) {
196+ Optional <Shape > operationOptional = model .getShape (operationId );
197+ if (operationOptional .isPresent ()) {
198+ compute (model , service , arnRoot , operationOptional .get (), resource );
199+ }
200+ }
190201
191202 // Child resources always inherit the identifiers of the parent.
192203 definitions .addAll (childIdentifiers .values ());
193204
194205 // Compute the keys of each child resource.
195- resource .getResources ().stream ().flatMap (id -> OptionalUtils .stream (model .getShape (id ))).forEach (child -> {
196- compute (model , service , arnRoot , child , resource , definitions );
197- });
206+ for (ShapeId resourceId : resource .getResources ()) {
207+ Optional <Shape > resourceOptional = model .getShape (resourceId );
208+ if (resourceOptional .isPresent ()) {
209+ compute (model , service , arnRoot , resourceOptional .get (), resource , definitions );
210+ }
211+ }
198212 });
199213 }
200214
@@ -230,7 +244,7 @@ private Map<String, String> inferChildResourceIdentifiers(
230244 builder .documentation (shape .getTrait (DocumentationTrait .class )
231245 .map (DocumentationTrait ::getValue )
232246 .orElse (computeIdentifierDocs (resource , childId )));
233- // The identifier name is comprised of "[arn service]:[Resource name][uppercase identifier name]
247+ // The identifier name consists of "[arn service]:[Resource name][uppercase identifier name]".
234248 String computeIdentifierName = computeIdentifierName (arnRoot , resource , childId );
235249 // Add the computed identifier binding and resolved context key to the result map.
236250 result .put (childId , computeIdentifierName );
0 commit comments