|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.docgen.core.interceptors; |
| 7 | + |
| 8 | +import software.amazon.smithy.docgen.core.DocGenerationContext; |
| 9 | +import software.amazon.smithy.docgen.core.sections.ProtocolSection; |
| 10 | +import software.amazon.smithy.docgen.core.writers.DocWriter; |
| 11 | +import software.amazon.smithy.model.shapes.Shape; |
| 12 | +import software.amazon.smithy.model.traits.ProtocolDefinitionTrait; |
| 13 | +import software.amazon.smithy.model.traits.XmlFlattenedTrait; |
| 14 | +import software.amazon.smithy.utils.CodeInterceptor; |
| 15 | +import software.amazon.smithy.utils.Pair; |
| 16 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 17 | + |
| 18 | +/** |
| 19 | + * Adds information to the protocol section for members indicating whether they target |
| 20 | + * a flat list/map or wrapped list/map depending on whether they have the |
| 21 | + * <a href="https://smithy.io/2.0/spec/protocol-traits.html#xmlflattened-trait"> |
| 22 | + * xmlFlattened</a> trait. |
| 23 | + */ |
| 24 | +@SmithyInternalApi |
| 25 | +public class XmlFlattenedInterceptor implements CodeInterceptor<ProtocolSection, DocWriter> { |
| 26 | + private static final Pair<String, String> WRAPPED_LIST_REF = Pair.of( |
| 27 | + "wrapped", "https://smithy.io/2.0/spec/protocol-traits.html#wrapped-list-serialization" |
| 28 | + ); |
| 29 | + private static final Pair<String, String> FLAT_LIST_REF = Pair.of( |
| 30 | + "flat", "https://smithy.io/2.0/spec/protocol-traits.html#flattened-list-serialization" |
| 31 | + ); |
| 32 | + private static final Pair<String, String> WRAPPED_MAP_REF = Pair.of( |
| 33 | + "wrapped", "https://smithy.io/2.0/spec/protocol-traits.html#wrapped-map-serialization" |
| 34 | + ); |
| 35 | + private static final Pair<String, String> FLAT_MAP_REF = Pair.of( |
| 36 | + "flat", "https://smithy.io/2.0/spec/protocol-traits.html#flattened-map-serialization" |
| 37 | + ); |
| 38 | + |
| 39 | + @Override |
| 40 | + public Class<ProtocolSection> sectionType() { |
| 41 | + return ProtocolSection.class; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean isIntercepted(ProtocolSection section) { |
| 46 | + if (!section.shape().isMemberShape()) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + var protocolShape = section.context().model().expectShape(section.protocol()); |
| 51 | + var protocolDefinition = protocolShape.expectTrait(ProtocolDefinitionTrait.class); |
| 52 | + if (!protocolDefinition.getTraits().contains(XmlFlattenedTrait.ID)) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + var target = section.context().model().expectShape(section.shape().asMemberShape().get().getTarget()); |
| 57 | + return target.isListShape() || target.isMapShape(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void write(DocWriter writer, String previousText, ProtocolSection section) { |
| 62 | + writer.write(""" |
| 63 | + Serialization type: $R |
| 64 | +
|
| 65 | + $L""", getRef(section.context(), section.shape()), previousText); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private Pair<String, String> getRef(DocGenerationContext context, Shape shape) { |
| 70 | + var target = context.model().expectShape(shape.asMemberShape().get().getTarget()); |
| 71 | + if (target.isMapShape()) { |
| 72 | + if (shape.hasTrait(XmlFlattenedTrait.class)) { |
| 73 | + return FLAT_MAP_REF; |
| 74 | + } |
| 75 | + return WRAPPED_MAP_REF; |
| 76 | + } |
| 77 | + |
| 78 | + if (shape.hasTrait(XmlFlattenedTrait.class)) { |
| 79 | + return FLAT_LIST_REF; |
| 80 | + } |
| 81 | + return WRAPPED_LIST_REF; |
| 82 | + } |
| 83 | +} |
0 commit comments