Skip to content

Commit de29b69

Browse files
Add uniqueItems trait information
1 parent 99c191e commit de29b69

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
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
@@ -24,6 +24,7 @@
2424
import software.amazon.smithy.docgen.core.interceptors.RecommendedInterceptor;
2525
import software.amazon.smithy.docgen.core.interceptors.SensitiveInterceptor;
2626
import software.amazon.smithy.docgen.core.interceptors.SinceInterceptor;
27+
import software.amazon.smithy.docgen.core.interceptors.UniqueItemsInterceptor;
2728
import software.amazon.smithy.docgen.core.interceptors.UnstableInterceptor;
2829
import software.amazon.smithy.docgen.core.writers.DocWriter;
2930
import software.amazon.smithy.docgen.core.writers.MarkdownWriter;
@@ -68,6 +69,7 @@ public List<? extends CodeInterceptor<? extends CodeSection, DocWriter>> interce
6869
return List.of(
6970
new NoReplaceBindingInterceptor(),
7071
new NoReplaceOperationInterceptor(),
72+
new UniqueItemsInterceptor(),
7173
new PatternInterceptor(),
7274
new RangeInterceptor(),
7375
new LengthInterceptor(),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.ShapeSubheadingSection;
9+
import software.amazon.smithy.docgen.core.writers.DocWriter;
10+
import software.amazon.smithy.model.traits.UniqueItemsTrait;
11+
import software.amazon.smithy.utils.CodeInterceptor;
12+
import software.amazon.smithy.utils.SmithyInternalApi;
13+
14+
/**
15+
* Adds an annotation to docs for lists with the
16+
* <a href="https://smithy.io/2.0/spec/constraint-traits.html#uniqueitems-trait">
17+
* uniqueItems trait</a>.
18+
*/
19+
@SmithyInternalApi
20+
public final class UniqueItemsInterceptor implements CodeInterceptor<ShapeSubheadingSection, DocWriter> {
21+
@Override
22+
public Class<ShapeSubheadingSection> sectionType() {
23+
return ShapeSubheadingSection.class;
24+
}
25+
26+
@Override
27+
public boolean isIntercepted(ShapeSubheadingSection section) {
28+
return section.shape().getMemberTrait(section.context().model(), UniqueItemsTrait.class).isPresent();
29+
}
30+
31+
@Override
32+
public void write(DocWriter writer, String previousText, ShapeSubheadingSection section) {
33+
writer.write("""
34+
Items in this list MUST be unique.
35+
36+
$L""", previousText);
37+
}
38+
}

smithy-docgen-test/model/main.smithy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,14 @@ structure LengthTraitExamples {
122122
blob: Blob
123123

124124
@length(min: 2, max: 4)
125-
list: StringList
125+
list: StringSet
126126

127127
map: StringMap
128128
}
129129

130-
list StringList {
130+
/// A set of strings.
131+
@uniqueItems
132+
list StringSet {
131133
member: String
132134
}
133135

0 commit comments

Comments
 (0)