1919import java .util .Objects ;
2020import java .util .Optional ;
2121import java .util .logging .Logger ;
22- import software .amazon .smithy .model .Model ;
2322import software .amazon .smithy .model .SourceException ;
23+ import software .amazon .smithy .model .node .ExpectationNotMetException ;
2424import software .amazon .smithy .model .node .Node ;
2525import software .amazon .smithy .model .node .ObjectNode ;
2626import 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 *
0 commit comments