|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package software.amazon.smithy.aws.iam.traits; |
| 6 | + |
| 7 | +import static java.lang.String.format; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import software.amazon.smithy.aws.traits.ServiceTrait; |
| 14 | +import software.amazon.smithy.model.Model; |
| 15 | +import software.amazon.smithy.model.knowledge.OperationIndex; |
| 16 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 17 | +import software.amazon.smithy.model.shapes.MemberShape; |
| 18 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 19 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 20 | +import software.amazon.smithy.model.validation.AbstractValidator; |
| 21 | +import software.amazon.smithy.model.validation.ValidationEvent; |
| 22 | +import software.amazon.smithy.model.validation.ValidationUtils; |
| 23 | + |
| 24 | +/** |
| 25 | + * Validates that members match 1:1 with supplying a value for a specific condition key. |
| 26 | + */ |
| 27 | +public class ConditionKeyValueTraitValidator extends AbstractValidator { |
| 28 | + @Override |
| 29 | + public List<ValidationEvent> validate(Model model) { |
| 30 | + List<ValidationEvent> events = new ArrayList<>(); |
| 31 | + if (!model.getAppliedTraits().contains(ConditionKeyValueTrait.ID)) { |
| 32 | + return events; |
| 33 | + } |
| 34 | + |
| 35 | + TopDownIndex topDownIndex = TopDownIndex.of(model); |
| 36 | + OperationIndex operationIndex = OperationIndex.of(model); |
| 37 | + for (ServiceShape service : model.getServiceShapesWithTrait(ServiceTrait.class)) { |
| 38 | + for (OperationShape operation : topDownIndex.getContainedOperations(service)) { |
| 39 | + Map<String, List<String>> conditionKeyMembers = new HashMap<>(); |
| 40 | + |
| 41 | + // Store each member name where the same condition key value is derived. |
| 42 | + for (MemberShape memberShape : operationIndex.getInputMembers(operation) |
| 43 | + .values()) { |
| 44 | + if (memberShape.hasTrait(ConditionKeyValueTrait.ID)) { |
| 45 | + ConditionKeyValueTrait trait = memberShape.expectTrait(ConditionKeyValueTrait.class); |
| 46 | + String conditionKey = trait.resolveConditionKey(service); |
| 47 | + conditionKeyMembers.computeIfAbsent(conditionKey, k -> new ArrayList<>()) |
| 48 | + .add(memberShape.getMemberName()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Emit events if more than one value source member is found for the same key. |
| 53 | + for (Map.Entry<String, List<String>> entry : conditionKeyMembers.entrySet()) { |
| 54 | + if (entry.getValue().size() > 1) { |
| 55 | + events.add(error(operationIndex.expectInputShape(operation), |
| 56 | + format("The `%s` condition key has its value supplied in the `%s` operation's input " |
| 57 | + + "shape by multiple members [%s]. This value must only be supplied by one " |
| 58 | + + "member.", |
| 59 | + entry.getKey(), |
| 60 | + operation.getId(), |
| 61 | + ValidationUtils.tickedList(entry.getValue())))); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return events; |
| 67 | + } |
| 68 | +} |
0 commit comments