Skip to content

Commit ab95bf3

Browse files
Jose Vitor SchneidSteven Yuan
authored andcommitted
Update docId resolution method (smithy-lang#1882)
This commit changes the resolution of the default docId property of the service trait by removing the reference to the target from the trait and passing the targeted shape itself as a parameter.
1 parent 1e43dde commit ab95bf3

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ServiceTrait.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.Objects;
2020
import java.util.Optional;
2121
import java.util.logging.Logger;
22-
import software.amazon.smithy.model.Model;
2322
import software.amazon.smithy.model.SourceException;
23+
import software.amazon.smithy.model.node.ExpectationNotMetException;
2424
import software.amazon.smithy.model.node.Node;
2525
import software.amazon.smithy.model.node.ObjectNode;
2626
import software.amazon.smithy.model.node.StringNode;
@@ -40,7 +40,6 @@ public final class ServiceTrait extends AbstractTrait implements ToSmithyBuilder
4040
public static final ShapeId ID = ShapeId.from("aws.api#service");
4141
private static final Logger LOGGER = Logger.getLogger(ServiceTrait.class.getName());
4242

43-
private final ShapeId target;
4443
private final String cloudFormationName;
4544
private final String arnNamespace;
4645
private final String sdkId;
@@ -50,7 +49,6 @@ public final class ServiceTrait extends AbstractTrait implements ToSmithyBuilder
5049

5150
private ServiceTrait(Builder builder) {
5251
super(ID, builder.getSourceLocation());
53-
this.target = SmithyBuilder.requiredState("target", builder.target);
5452
this.sdkId = SmithyBuilder.requiredState("sdkId", builder.sdkId);
5553
this.arnNamespace = SmithyBuilder.requiredState("arnNamespace", builder.arnNamespace);
5654
this.cloudFormationName = SmithyBuilder.requiredState("cloudFormationName", builder.cloudFormationName);
@@ -149,25 +147,31 @@ public String getCloudTrailEventSource() {
149147
}
150148

151149
/**
152-
* Returns the documentation identifier value for the service.
150+
* Resolves the doc id value for the service.
153151
*
154152
* <p> When value on trait is not set, this method defaults to the lower
155153
* cased value of the sdkId followed by the service version, separated by
156154
* dashes.
157155
*
156+
* @param serviceShape the shape which this trait targets
158157
* @return Returns the documentation identifier value for the service name.
158+
* @throws ExpectationNotMetException if the shape is not the target of this trait.
159159
*/
160-
public String getDocId(Model model) {
161-
return getDocId().orElseGet(() -> buildDefaultDocId(model));
160+
public String resolveDocId(ServiceShape serviceShape) {
161+
return getDocId().orElseGet(() -> buildDefaultDocId(serviceShape));
162162
}
163163

164164
protected Optional<String> getDocId() {
165165
return Optional.ofNullable(docId);
166166
}
167167

168-
private String buildDefaultDocId(Model model) {
169-
String version = model.expectShape(target, ServiceShape.class).getVersion();
170-
return sdkId.replace(" ", "-").toLowerCase(Locale.US) + "-" + version;
168+
private String buildDefaultDocId(ServiceShape serviceShape) {
169+
if (!serviceShape.expectTrait(ServiceTrait.class).equals(this)) {
170+
throw new ExpectationNotMetException(String.format(
171+
"Provided service shape `%s` is not the target of this trait.", serviceShape.getId()), this);
172+
}
173+
174+
return sdkId.replace(" ", "-").toLowerCase(Locale.US) + "-" + serviceShape.getVersion();
171175
}
172176

173177
/**
@@ -192,7 +196,6 @@ public Optional<String> getAbbreviation() {
192196
@Override
193197
public Builder toBuilder() {
194198
return new Builder()
195-
.target(target)
196199
.sdkId(sdkId)
197200
.sourceLocation(getSourceLocation())
198201
.cloudFormationName(cloudFormationName)
@@ -206,7 +209,6 @@ public Builder toBuilder() {
206209
protected Node createNode() {
207210
return Node.objectNodeBuilder()
208211
.sourceLocation(getSourceLocation())
209-
.withMember("target", Node.from(target.toString()))
210212
.withMember("sdkId", Node.from(sdkId))
211213
.withMember("arnNamespace", Node.from(getArnNamespace()))
212214
.withMember("cloudFormationName", Node.from(getCloudFormationName()))
@@ -227,7 +229,6 @@ public boolean equals(Object other) {
227229
} else {
228230
ServiceTrait os = (ServiceTrait) other;
229231
return sdkId.equals(os.sdkId)
230-
&& target.equals(os.target)
231232
&& arnNamespace.equals(os.arnNamespace)
232233
&& cloudFormationName.equals(os.cloudFormationName)
233234
&& cloudTrailEventSource.equals(os.cloudTrailEventSource)
@@ -240,13 +241,12 @@ public boolean equals(Object other) {
240241

241242
@Override
242243
public int hashCode() {
243-
return Objects.hash(toShapeId(), target, sdkId, arnNamespace, cloudFormationName,
244+
return Objects.hash(toShapeId(), sdkId, arnNamespace, cloudFormationName,
244245
cloudTrailEventSource, docId, endpointPrefix);
245246
}
246247

247248
/** Builder for {@link ServiceTrait}. */
248249
public static final class Builder extends AbstractTraitBuilder<ServiceTrait, Builder> {
249-
private ShapeId target;
250250
private String sdkId;
251251
private String cloudFormationName;
252252
private String arnNamespace;
@@ -266,8 +266,6 @@ public ServiceTrait build() {
266266
}
267267

268268
public ServiceTrait build(ShapeId target) {
269-
this.target = target;
270-
271269
// Fill in default values if they weren't set.
272270
if (arnNamespace == null) {
273271
arnNamespace(target.getName().toLowerCase(Locale.US));
@@ -288,17 +286,6 @@ public ServiceTrait build(ShapeId target) {
288286
return new ServiceTrait(this);
289287
}
290288

291-
/**
292-
* Sets the target shape to which the trait is applied.
293-
*
294-
* @param target the ShapeId targeted by the trait.
295-
* @return Returns the builder.
296-
*/
297-
private Builder target(ShapeId target) {
298-
this.target = target;
299-
return this;
300-
}
301-
302289
/**
303290
* Sets the AWS CloudFormation resource type service name.
304291
*

smithy-aws-traits/src/test/java/software/amazon/smithy/aws/traits/EventSourceValidatorTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@
1010
import org.junit.jupiter.api.Test;
1111
import software.amazon.smithy.model.Model;
1212
import software.amazon.smithy.model.shapes.ServiceShape;
13-
import software.amazon.smithy.model.shapes.ShapeId;
1413
import software.amazon.smithy.model.validation.Severity;
1514
import software.amazon.smithy.model.validation.ValidationEvent;
1615

1716
public class EventSourceValidatorTest {
1817
@Test
1918
public void detectsWhenEventSourceIsUnexpected() {
20-
ShapeId id = ShapeId.from("smithy.example#Foo");
2119
ServiceTrait trait = ServiceTrait.builder()
2220
.sdkId("Foo")
2321
.arnNamespace("foo")
2422
.cloudTrailEventSource("REPLACE_ME_LATER")
2523
.cloudFormationName("AWS::Foo")
26-
.build(id);
24+
.build();
2725
ServiceShape service = ServiceShape.builder()
28-
.id(id)
26+
.id("smithy.example#Foo")
2927
.version("123")
3028
.addTrait(trait)
3129
.build();
@@ -41,15 +39,14 @@ public void detectsWhenEventSourceIsUnexpected() {
4139

4240
@Test
4341
public void detectsWhenEventSourceIsPlaceholder() {
44-
ShapeId id = ShapeId.from("smithy.example#Foo");
4542
ServiceTrait trait = ServiceTrait.builder()
4643
.sdkId("Foo")
4744
.arnNamespace("foo")
4845
.cloudTrailEventSource("notfoo.amazonaws.com")
4946
.cloudFormationName("AWS::Foo")
50-
.build(id);
47+
.build();
5148
ServiceShape service = ServiceShape.builder()
52-
.id(id)
49+
.id("smithy.example#Foo")
5350
.version("123")
5451
.addTrait(trait)
5552
.build();
@@ -66,15 +63,14 @@ public void detectsWhenEventSourceIsPlaceholder() {
6663

6764
@Test
6865
public void ignoresKnownExceptions() {
69-
ShapeId id = ShapeId.from("smithy.example#Foo");
7066
ServiceTrait trait = ServiceTrait.builder()
7167
.sdkId("Foo")
7268
.arnNamespace("cloudwatch")
7369
.cloudTrailEventSource("monitoring.amazonaws.com")
7470
.cloudFormationName("AWS::Foo")
75-
.build(id);
71+
.build();
7672
ServiceShape service = ServiceShape.builder()
77-
.id(id)
73+
.id("smithy.example#Foo")
7874
.version("123")
7975
.addTrait(trait)
8076
.build();

smithy-aws-traits/src/test/java/software/amazon/smithy/aws/traits/ServiceTraitTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package software.amazon.smithy.aws.traits;
1717

1818
import static org.hamcrest.MatcherAssert.assertThat;
19-
import static org.hamcrest.Matchers.containsString;
2019
import static org.hamcrest.Matchers.equalTo;
2120
import static org.hamcrest.Matchers.instanceOf;
2221
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -26,6 +25,7 @@
2625
import java.util.Optional;
2726
import org.junit.jupiter.api.Test;
2827
import software.amazon.smithy.model.Model;
28+
import software.amazon.smithy.model.node.ExpectationNotMetException;
2929
import software.amazon.smithy.model.node.Node;
3030
import software.amazon.smithy.model.shapes.ServiceShape;
3131
import software.amazon.smithy.model.shapes.ShapeId;
@@ -50,8 +50,6 @@ public void loadsTraitWithString() {
5050
assertThat(serviceTrait.getEndpointPrefix(), equalTo("foo"));
5151
assertThat(serviceTrait.toBuilder().build(), equalTo(serviceTrait));
5252
assertFalse(serviceTrait.getDocId().isPresent());
53-
assertThat(Node.prettyPrintJson(serviceTrait.createNode()),
54-
containsString("\"target\": \"ns.foo#Foo\","));
5553
}
5654

5755
@Test
@@ -89,6 +87,27 @@ public void requiresSdkServiceId() {
8987
assertThrows(IllegalStateException.class, () -> ServiceTrait.builder().build());
9088
}
9189

90+
@Test
91+
public void requiresProperServiceShapeToResolveDocId() {
92+
ServiceTrait trait = ServiceTrait.builder()
93+
.sdkId("Foo SDK")
94+
.arnNamespace("foo")
95+
.cloudTrailEventSource("cloudTrailEventSource")
96+
.cloudFormationName("AWS::Foo")
97+
.build();
98+
ServiceShape service = ServiceShape.builder()
99+
.id("smithy.example#Foo")
100+
.version("123")
101+
.addTrait(trait)
102+
.build();
103+
ServiceShape anotherService = ServiceShape.builder()
104+
.id("smithy.example#Bar")
105+
.build();
106+
107+
assertThat(trait.resolveDocId(service), equalTo("foo-sdk-123"));
108+
assertThrows(ExpectationNotMetException.class, () -> trait.resolveDocId(anotherService));
109+
}
110+
92111
@Test
93112
public void loadsFromModel() {
94113
Model result = Model.assembler()
@@ -105,6 +124,6 @@ public void loadsFromModel() {
105124
assertThat(trait.getArnNamespace(), equalTo("service"));
106125
assertThat(trait.getEndpointPrefix(), equalTo("some-service"));
107126
assertFalse(trait.getDocId().isPresent());
108-
assertThat(trait.getDocId(result), equalTo("some-value-2018-03-17"));
127+
assertThat(trait.resolveDocId(service), equalTo("some-value-2018-03-17"));
109128
}
110129
}

0 commit comments

Comments
 (0)