Skip to content

Commit dc87b07

Browse files
Add timestampFormat trait support
1 parent a8a0259 commit dc87b07

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/integrations/BuiltinsIntegration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import software.amazon.smithy.docgen.core.interceptors.SensitiveInterceptor;
3232
import software.amazon.smithy.docgen.core.interceptors.SinceInterceptor;
3333
import software.amazon.smithy.docgen.core.interceptors.SparseInterceptor;
34+
import software.amazon.smithy.docgen.core.interceptors.TimestampFormatInterceptor;
3435
import software.amazon.smithy.docgen.core.interceptors.UniqueItemsInterceptor;
3536
import software.amazon.smithy.docgen.core.interceptors.UnstableInterceptor;
3637
import software.amazon.smithy.docgen.core.interceptors.XmlNameInterceptor;
@@ -77,6 +78,7 @@ public List<? extends CodeInterceptor<? extends CodeSection, DocWriter>> interce
7778
return List.of(
7879
new OperationAuthInterceptor(),
7980
new ApiKeyAuthInterceptor(),
81+
new TimestampFormatInterceptor(),
8082
new JsonNameInterceptor(),
8183
new XmlNameInterceptor(),
8284
new PaginationInterceptor(),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

smithy-docgen-test/model/main.smithy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ structure DocumentedStructure {
128128
@deprecated
129129
integer: Integer
130130

131+
/// This is a timestamp with a custom format
132+
timestamp: DateTime
133+
131134
// This doesn't have a doc string (this is just a normal comment), so it should
132135
// pull the docs from the target shape.
133136
@deprecated(message: "Please use intEnum instead")
@@ -146,6 +149,10 @@ structure DocumentedStructure {
146149
union: DocumentedUnion
147150
}
148151

152+
/// Timestamp in RFC3339 format
153+
@timestampFormat("date-time")
154+
timestamp DateTime
155+
149156
/// This in an enum that can have one of the following values:
150157
///
151158
/// - `FOO`

0 commit comments

Comments
 (0)