|
| 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.sections.ProtocolSection; |
| 9 | +import software.amazon.smithy.docgen.core.writers.DocWriter; |
| 10 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 11 | +import software.amazon.smithy.model.traits.TimestampFormatTrait; |
| 12 | +import software.amazon.smithy.utils.Pair; |
| 13 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 14 | + |
| 15 | +/** |
| 16 | + * Adds a member's <a href="https://smithy.io/2.0/spec/protocol-traits.html#timestampformat-trait"> |
| 17 | + * timestamp format</a> to the {@link ProtocolSection} if the protocol supports it. |
| 18 | + */ |
| 19 | +@SmithyInternalApi |
| 20 | +public final class TimestampFormatInterceptor extends ProtocolTraitInterceptor<TimestampFormatTrait> { |
| 21 | + private static final Pair<String, String> DATE_TIME_REF = Pair.of( |
| 22 | + "RFC3339 date-time", "https://datatracker.ietf.org/doc/html/rfc3339.html#section-5.6" |
| 23 | + ); |
| 24 | + private static final Pair<String, String> HTTP_DATE_REF = Pair.of( |
| 25 | + "RFC7231 IMF-fixdate", "https://tools.ietf.org/html/rfc7231.html#section-7.1.1.1" |
| 26 | + ); |
| 27 | + |
| 28 | + @Override |
| 29 | + protected Class<TimestampFormatTrait> getTraitClass() { |
| 30 | + return TimestampFormatTrait.class; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected ShapeId getTraitId() { |
| 35 | + return TimestampFormatTrait.ID; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + void write(DocWriter writer, String previousText, ProtocolSection section, TimestampFormatTrait trait) { |
| 40 | + writer.writeInline("$B ", "TimestampFormat:"); |
| 41 | + switch (trait.getFormat()) { |
| 42 | + case DATE_TIME -> writer.write("$R", DATE_TIME_REF); |
| 43 | + case HTTP_DATE -> writer.write("$R", HTTP_DATE_REF); |
| 44 | + case EPOCH_SECONDS -> writer.write("epoch seconds"); |
| 45 | + default -> { |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + writer.writeWithNoFormatting("\n" + previousText); |
| 50 | + } |
| 51 | +} |
0 commit comments