Skip to content

Commit 99c191e

Browse files
Add range trait information
1 parent e9db7d6 commit 99c191e

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-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
@@ -20,6 +20,7 @@
2020
import software.amazon.smithy.docgen.core.interceptors.NoReplaceOperationInterceptor;
2121
import software.amazon.smithy.docgen.core.interceptors.NullabilityInterceptor;
2222
import software.amazon.smithy.docgen.core.interceptors.PatternInterceptor;
23+
import software.amazon.smithy.docgen.core.interceptors.RangeInterceptor;
2324
import software.amazon.smithy.docgen.core.interceptors.RecommendedInterceptor;
2425
import software.amazon.smithy.docgen.core.interceptors.SensitiveInterceptor;
2526
import software.amazon.smithy.docgen.core.interceptors.SinceInterceptor;
@@ -68,6 +69,7 @@ public List<? extends CodeInterceptor<? extends CodeSection, DocWriter>> interce
6869
new NoReplaceBindingInterceptor(),
6970
new NoReplaceOperationInterceptor(),
7071
new PatternInterceptor(),
72+
new RangeInterceptor(),
7173
new LengthInterceptor(),
7274
new ExternalDocsInterceptor(),
7375
new ErrorFaultInterceptor(),
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.ShapeSubheadingSection;
9+
import software.amazon.smithy.docgen.core.writers.DocWriter;
10+
import software.amazon.smithy.model.traits.RangeTrait;
11+
import software.amazon.smithy.utils.CodeInterceptor;
12+
import software.amazon.smithy.utils.SmithyInternalApi;
13+
14+
/**
15+
* Adds information to shapes if they have the
16+
* <a href="https://smithy.io/2.0/spec/constraint-traits.html#range-trait">
17+
* range trait</a>.
18+
*/
19+
@SmithyInternalApi
20+
public final class RangeInterceptor 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(), RangeTrait.class).isPresent();
29+
}
30+
31+
@Override
32+
public void write(DocWriter writer, String previousText, ShapeSubheadingSection section) {
33+
var trait = section.shape().getMemberTrait(section.context().model(), RangeTrait.class).get();
34+
writer.putContext("min", trait.getMin());
35+
writer.putContext("max", trait.getMax());
36+
37+
writer.write("""
38+
${?min}
39+
$1B ${min:L}
40+
41+
${/min}
42+
${?max}
43+
$2B ${max:L}
44+
45+
${/max}
46+
$3L""",
47+
"Minimum:",
48+
"Maximum:",
49+
previousText);
50+
}
51+
}

smithy-docgen-test/model/main.smithy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ operation DocumentedOperation {
4949
input := {
5050
structure: DocumentedStructure
5151
lengthExamples: LengthTraitExamples
52+
rangeExamples: RangeTraitExamples
5253
}
5354
output := {
5455
structure: DocumentedStructure
@@ -136,6 +137,18 @@ map StringMap {
136137
value: String
137138
}
138139

140+
/// This shows how the range trait is applied to various types.
141+
structure RangeTraitExamples {
142+
@range(min: 0)
143+
positive: Integer
144+
145+
@range(max: 0)
146+
negative: Long
147+
148+
@range(min: 0, max: 255)
149+
unsignedByte: Short
150+
}
151+
139152
/// This in an enum that can have one of the following values:
140153
///
141154
/// - `SPAM`: `1`

0 commit comments

Comments
 (0)