Skip to content

Commit

Permalink
Adding AWS CFN Trait: cfnDefaultValue (#1285)
Browse files Browse the repository at this point in the history
* cfnDefaultValue trait definition added
  • Loading branch information
rnihithp authored Jul 14, 2022
1 parent e8c82ab commit 2879601
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
72 changes: 68 additions & 4 deletions docs/source/1.0/spec/aws/aws-cloudformation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,68 @@ The following example defines a CloudFormation resource that has the
fooAlias: String
}
.. smithy-trait:: aws.cloudformation#cfnDefaultValue
.. _aws.cloudformation#cfnDefaultValue-trait:

----------------------------------------
``aws.cloudformation#cfnDefaultValue`` trait
----------------------------------------

Summary
Indicates that the member annotated has a default value for the resource.
Trait selector
``resource > operation -[output]-> structure > member``

*Only applicable to members of ``@output`` operations*
Value type
Annotation trait

Given the following example, because the ``fooAlias``
member is annotated with ``cfnDefaultValue``, it can be derived
that the ``fooAlias`` member has a default value for this resource.

.. code-block:: smithy
namespace smithy.example
use aws.cloudformation#cfnDefaultValue
use aws.cloudformation#cfnResource
@cfnResource
resource Foo {
identifiers: {
fooId: String,
},
read: GetFoo,
}
@readonly
@http(method: "GET", uri: "/foos/{fooId}", code: 200)
operation GetFoo {
input: GetFooRequest,
output: GetFooResponse,
}
@input
structure GetFooRequest {
@httpLabel
@required
fooId: String,
fooAlias: String,
}
@output
structure GetFooResponse {
fooId: String,
@cfnDefaultValue
fooAlias: String,
@httpResponseCode
responseCode: Integer,
}
-------------
Example model
Expand All @@ -656,6 +718,7 @@ Given the following model,
namespace smithy.example
use aws.cloudformation#cfnDefaultValue
use aws.cloudformation#cfnAdditionalIdentifier
use aws.cloudformation#cfnExcludeProperty
use aws.cloudformation#cfnMutability
Expand Down Expand Up @@ -727,10 +790,11 @@ Given the following model,
@cfnMutability("read")
updatedAt: Timestamp
fooAlias: String
createProperty: ComplexProperty
mutableProperty: ComplexProperty
readProperty: ComplexProperty
@cfnDefaultValue
fooAlias: String,
createProperty: ComplexProperty,
mutableProperty: ComplexProperty,
readProperty: ComplexProperty,
}
@idempotent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.cloudformation.traits;

import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.AnnotationTrait;

/**
* Indicates that the CloudFormation property generated from this member has a
* default value for this resource.
*/
public final class CfnDefaultValueTrait extends AnnotationTrait {
public static final ShapeId ID = ShapeId.from("aws.cloudformation#cfnDefaultValue");

public CfnDefaultValueTrait(ObjectNode node) {
super(ID, node);
}

public CfnDefaultValueTrait() {
this(Node.objectNode());
}

public static final class Provider extends AnnotationTrait.Provider<CfnDefaultValueTrait> {
public Provider() {
super(ID, CfnDefaultValueTrait::new);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
software.amazon.smithy.aws.cloudformation.traits.CfnDefaultValueTrait$Provider
software.amazon.smithy.aws.cloudformation.traits.CfnAdditionalIdentifierTrait$Provider
software.amazon.smithy.aws.cloudformation.traits.CfnExcludePropertyTrait$Provider
software.amazon.smithy.aws.cloudformation.traits.CfnMutabilityTrait$Provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ string cfnName
conflicts: [
cfnAdditionalIdentifier,
cfnMutability,
cfnDefaultValue
],
breakingChanges: [{change: "add"}]
)
structure cfnExcludeProperty {}

/// Indicates that a structure member has a default value
/// for the property of the CloudFormation resource.
@unstable
@trait(
selector: "resource > operation -[input, output]-> structure > member",
conflicts: [cfnExcludeProperty]
)
structure cfnDefaultValue {}

/// Indicates an explicit CloudFormation mutability of the structure member
/// when part of a CloudFormation resource.
@unstable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.aws.cloudformation.traits;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ShapeId;

public final class CfnDefaultValueTraitTest {

@Test
public void detectsDefaultValue() {
Model result = Model.assembler()
.discoverModels(getClass().getClassLoader())
.addImport(getClass().getResource("test-service.smithy"))
.assemble()
.unwrap();

assertTrue(result.getShape(ShapeId.from("smithy.example#GetFooResponse$fooId"))
.flatMap(shape -> shape.getTrait(CfnDefaultValueTrait.class))
.isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use aws.cloudformation#cfnResource
use aws.cloudformation#cfnAdditionalIdentifier
use aws.cloudformation#cfnExcludeProperty
use aws.cloudformation#cfnMutability
use aws.cloudformation#cfnDefaultValue

service TestService {
version: "2020-07-02",
Expand Down Expand Up @@ -55,6 +56,7 @@ structure GetFooRequest {
}

structure GetFooResponse {
@cfnDefaultValue
fooId: FooId,

fooValidReadProperty: String,
Expand Down

0 comments on commit 2879601

Please sign in to comment.