|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.codegen.customization.processors; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | +import software.amazon.awssdk.codegen.customization.CodegenCustomizationProcessor; |
| 25 | +import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; |
| 26 | +import software.amazon.awssdk.codegen.model.service.ServiceModel; |
| 27 | + |
| 28 | +/** |
| 29 | + * Services that have "legacy" streams, Kinesis and Transcribe Streaming builds should fail if there is a new |
| 30 | + * evenstream added, that codegen doesn't know about. This is so we can decide if we want to generate the new stream in |
| 31 | + * the legacy style so they look like the existing streams, or if we use the new style (e.g. because it won't work with |
| 32 | + * the old style). |
| 33 | + */ |
| 34 | +public final class NewAndLegacyEventStreamProcessor implements CodegenCustomizationProcessor { |
| 35 | + // Map from service ID -> list of event streams we know about and approve |
| 36 | + private static final Map<String, Set<String>> APPROVED_EVENT_STREAMS; |
| 37 | + |
| 38 | + static { |
| 39 | + Map<String, Set<String>> approvedEventStreams = new HashMap<>(); |
| 40 | + |
| 41 | + approvedEventStreams.put("Kinesis", new HashSet<>(Arrays.asList("SubscribeToShardEventStream"))); |
| 42 | + approvedEventStreams.put("Transcribe Streaming", |
| 43 | + new HashSet<>(Arrays.asList("AudioStream", "TranscriptResultStream", "MedicalTranscriptResultStream"))); |
| 44 | + |
| 45 | + APPROVED_EVENT_STREAMS = Collections.unmodifiableMap(approvedEventStreams); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + @Override |
| 50 | + public void preprocess(ServiceModel serviceModel) { |
| 51 | + String serviceId = serviceModel.getMetadata().getServiceId(); |
| 52 | + if (!APPROVED_EVENT_STREAMS.containsKey(serviceId)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + Set<String> approvedStreams = APPROVED_EVENT_STREAMS.get(serviceId); |
| 57 | + |
| 58 | + serviceModel.getShapes().entrySet().stream() |
| 59 | + .filter(e -> e.getValue().isEventstream()) |
| 60 | + .forEach(e -> { |
| 61 | + String name = e.getKey(); |
| 62 | + if (!approvedStreams.contains(name)) { |
| 63 | + throw unknownStreamError(serviceId, name); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void postprocess(IntermediateModel intermediateModel) { |
| 70 | + // no-op |
| 71 | + } |
| 72 | + |
| 73 | + private static RuntimeException unknownStreamError(String serviceId, String evenstreamName) { |
| 74 | + String msg = String.format("Encountered a new eventstream for service %s: %s. This service contains " + |
| 75 | + "evenstreams that are code generated using an older style that requires a customization. Please " + |
| 76 | + "contact the Java SDK maintainers for assistance.", serviceId, evenstreamName); |
| 77 | + |
| 78 | + return new RuntimeException(msg); |
| 79 | + } |
| 80 | +} |
0 commit comments